From 227cda57b198a04b5630ad547dddd4c85c232e56 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 18 Nov 2016 17:51:05 +0100 Subject: [PATCH] Fixed AJAX exception handling --- .../files/js/WoltLabSuite/Core/Ajax/Request.js | 9 +++++++-- .../lib/action/AJAXInvokeAction.class.php | 2 +- .../system/exception/AJAXException.class.php | 18 ++++++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js index 50659b34ae..6bab12a618 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js @@ -272,10 +272,15 @@ define(['Core', 'Language', 'Dom/ChangeListener', 'Dom/Util', 'Ui/Dialog', 'Wolt var message = ''; if (data !== null) { - if (data.stacktrace) details = '

Stacktrace:

' + data.stacktrace + '

'; - else if (data.exceptionID) details = '

Exception ID: ' + data.exceptionID + '

'; + if (data.stacktrace) details = '

Stacktrace:

' + data.stacktrace + '

'; + else if (data.exceptionID) details = '

Exception ID: ' + data.exceptionID + '

'; message = data.message; + + data.previous.forEach(function(previous) { + details += '

' + previous.message + '

'; + details += '

Stacktrace

' + previous.stacktrace + '

'; + }); } else { message = xhr.responseText; diff --git a/wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php b/wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php index ff1674a5d0..41de8d4127 100644 --- a/wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php +++ b/wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php @@ -202,7 +202,7 @@ class AJAXInvokeAction extends AbstractSecureAction { throw new AJAXException($e->getMessage(), AJAXException::BAD_PARAMETERS, $e->getTraceAsString()); } else { - throw new AJAXException($e->getMessage(), AJAXException::INTERNAL_ERROR, $e->getTraceAsString(), [], \wcf\functions\exception\logThrowable($e)); + throw new AJAXException($e->getMessage(), AJAXException::INTERNAL_ERROR, $e->getTraceAsString(), [], \wcf\functions\exception\logThrowable($e), $e->getPrevious()); } } diff --git a/wcfsetup/install/files/lib/system/exception/AJAXException.class.php b/wcfsetup/install/files/lib/system/exception/AJAXException.class.php index 6f1a7490b5..461df24867 100644 --- a/wcfsetup/install/files/lib/system/exception/AJAXException.class.php +++ b/wcfsetup/install/files/lib/system/exception/AJAXException.class.php @@ -56,23 +56,37 @@ class AJAXException extends LoggedException { * @param string $stacktrace * @param mixed[] $returnValues * @param string $exceptionID + * @param \Exception|\Throwable $previous */ - public function __construct($message, $errorType = self::INTERNAL_ERROR, $stacktrace = null, $returnValues = [], $exceptionID = '') { + public function __construct($message, $errorType = self::INTERNAL_ERROR, $stacktrace = null, $returnValues = [], $exceptionID = '', $previous = null) { if ($stacktrace === null) $stacktrace = $this->getTraceAsString(); $responseData = [ 'code' => $errorType, 'message' => $message, + 'previous' => [], 'returnValues' => $returnValues ]; // include a stacktrace if: // - debug mode is enabled // - within ACP and a SystemException was thrown - if (WCF::debugModeIsEnabled(false) || WCF::debugModeIsEnabled() && self::INTERNAL_ERROR) { + $includeStacktrace = (WCF::debugModeIsEnabled(false) || WCF::debugModeIsEnabled() && self::INTERNAL_ERROR); + + if ($includeStacktrace) { $responseData['stacktrace'] = nl2br($stacktrace, false); } + if ($includeStacktrace) { + while ($previous) { + $data = ['message' => $previous->getMessage()]; + $data['stacktrace'] = nl2br($previous->getTraceAsString(), false); + + $responseData['previous'][] = $data; + $previous = $previous->getPrevious(); + } + } + $statusHeader = ''; switch ($errorType) { case self::MISSING_PARAMETERS: -- 2.20.1