From dc0005eefcda31ec3638bc68576e5ff5b1f8d999 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 21 Oct 2011 13:11:28 +0200 Subject: [PATCH] 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! --- .../lib/action/AJAXProxyAction.class.php | 20 ++++++++++++++++--- .../system/exception/AJAXException.class.php | 12 +++++------ 2 files changed, 22 insertions(+), 10 deletions(-) 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; -- 2.20.1