From: Alexander Ebert Date: Fri, 21 Oct 2011 11:11:28 +0000 (+0200) Subject: Fixed exception handling in AJAXProxyAction X-Git-Tag: 2.0.0_Beta_1~1672 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=dc0005eefcda31ec3638bc68576e5ff5b1f8d999;p=GitHub%2FWoltLab%2FWCF.git Fixed exception handling in AJAXProxyAction Whenever you throw a new exception with the original exception as parameter, you cannot access it's stacktrace. In fact the stacktrace will return the stacktrace for the new exception, ignoring the fact you're requesting the stacktrace of the passed exception. Hooray! --- diff --git a/wcfsetup/install/files/lib/action/AJAXProxyAction.class.php b/wcfsetup/install/files/lib/action/AJAXProxyAction.class.php index a446e5995e..140d90af34 100644 --- a/wcfsetup/install/files/lib/action/AJAXProxyAction.class.php +++ b/wcfsetup/install/files/lib/action/AJAXProxyAction.class.php @@ -67,7 +67,7 @@ class AJAXProxyAction extends AbstractSecureAction { throw $e; } else { - throw new AJAXException($e); + $this->throwException($e); } } } @@ -114,7 +114,7 @@ class AJAXProxyAction extends AbstractSecureAction { $this->objectAction->validateAction(); } catch (ValidateActionException $e) { - throw new SystemException("validation failed: ".$e->getMessage()); + $this->throwException($e); } // execute action @@ -122,7 +122,7 @@ class AJAXProxyAction extends AbstractSecureAction { $this->response = $this->objectAction->executeAction(); } catch (\Exception $e) { - throw new SystemException('unknown exception caught: '.$e->getMessage()); + $this->throwException($e); } $this->executed(); @@ -131,4 +131,18 @@ class AJAXProxyAction extends AbstractSecureAction { echo JSON::encode($this->response); exit; } + + /** + * Throws an previously catched exception while maintaing the propriate stacktrace. + * + * @param \Exception $e + */ + protected function throwException(\Exception $e) { + if ($e instanceof SystemException) { + throw new AJAXException($e->getMessage(), $e->__getTraceAsString()); + } + else { + throw new AJAXException($e->getMessage(), $e->getTraceAsString()); + } + } } diff --git a/wcfsetup/install/files/lib/system/exception/AJAXException.class.php b/wcfsetup/install/files/lib/system/exception/AJAXException.class.php index dec8b4a0f9..4897cd5947 100644 --- a/wcfsetup/install/files/lib/system/exception/AJAXException.class.php +++ b/wcfsetup/install/files/lib/system/exception/AJAXException.class.php @@ -16,19 +16,17 @@ class AJAXException extends \Exception { /** * Throws a JSON-encoded error message * - * @param \Exception $exception + * @param string $message + * @param string $stacktrace */ - public function __construct(\Exception $exception) { - $stacktrace = $exception->getTraceAsString(); - if ($exception instanceof SystemException) { - $stacktrace = $exception->__getTraceAsString(); - } + public function __construct($message, $stacktrace = null) { + if ($stacktrace === null) $stacktrace = $this->getTraceAsString(); //header('HTTP/1.0 418 I\'m a Teapot'); header('HTTP/1.0 503 Service Unavailable'); header('Content-type: application/json'); echo JSON::encode(array( - 'message' => $exception->getMessage(), + 'message' => $message, 'stacktrace' => nl2br($stacktrace) )); exit;