Fixed exception handling in AJAXProxyAction
authorAlexander Ebert <ebert@woltlab.com>
Fri, 21 Oct 2011 11:11:28 +0000 (13:11 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 21 Oct 2011 11:11:28 +0000 (13:11 +0200)
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!

wcfsetup/install/files/lib/action/AJAXProxyAction.class.php
wcfsetup/install/files/lib/system/exception/AJAXException.class.php

index a446e5995e039aefa80da1a71723f5563702a799..140d90af34462a4e3d261eaedc2aaf50df82b3b2 100644 (file)
@@ -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());
+               }
+       }
 }
index dec8b4a0f90d2028631afd09c909927168a721a2..4897cd5947210a7ea2ace38cbf5b66dc54b38fda 100644 (file)
@@ -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;