Add TAJAXException trait to reuse the throwException method
authorJoshua Rüsweg <josh@bastelstu.be>
Mon, 7 Jan 2019 21:32:57 +0000 (22:32 +0100)
committerJoshua Rüsweg <josh@bastelstu.be>
Mon, 7 Jan 2019 22:01:21 +0000 (23:01 +0100)
See #2825

wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php
wcfsetup/install/files/lib/action/TAJAXException.class.php [new file with mode: 0644]

index a5e1a948219a89db703f686583e5f745faf4cdf9..9769c7396122c4d2b8a89a3ca94fdd4804cebfb6 100644 (file)
@@ -1,15 +1,11 @@
 <?php
 namespace wcf\action;
 use wcf\system\exception\AJAXException;
-use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\ImplementationException;
-use wcf\system\exception\InvalidSecurityTokenException;
-use wcf\system\exception\NamedUserException;
 use wcf\system\exception\ParentClassException;
 use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\SystemException;
 use wcf\system\exception\UserInputException;
-use wcf\system\exception\ValidateActionException;
 use wcf\system\IAJAXInvokeAction;
 use wcf\system\SingletonFactory;
 use wcf\system\WCF;
@@ -25,6 +21,8 @@ use wcf\util\StringUtil;
  * @package    WoltLabSuite\Core\Action
  */
 class AJAXInvokeAction extends AbstractSecureAction {
+       use TAJAXException;
+       
        /**
         * method name
         * @var string
@@ -150,53 +148,6 @@ class AJAXInvokeAction extends AbstractSecureAction {
                exit;
        }
        
-       /**
-        * Throws an previously caught exception while maintaining the propriate stacktrace.
-        * 
-        * @param       \Exception|\Throwable   $e
-        * @throws      AJAXException
-        * @throws      \Exception
-        * @throws      \Throwable
-        */
-       protected function throwException($e) {
-               if ($this->inDebugMode) {
-                       throw $e;
-               }
-               
-               if ($e instanceof InvalidSecurityTokenException) {
-                       throw new AJAXException(WCF::getLanguage()->getDynamicVariable('wcf.ajax.error.sessionExpired'), AJAXException::SESSION_EXPIRED, $e->getTraceAsString());
-               }
-               else if ($e instanceof PermissionDeniedException) {
-                       throw new AJAXException(WCF::getLanguage()->getDynamicVariable('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS, $e->getTraceAsString());
-               }
-               else if ($e instanceof IllegalLinkException) {
-                       throw new AJAXException(WCF::getLanguage()->get('wcf.ajax.error.illegalLink'), AJAXException::ILLEGAL_LINK, $e->getTraceAsString());
-               }
-               else if ($e instanceof UserInputException) {
-                       // repackage as ValidationActionException
-                       $exception = new ValidateActionException($e->getField(), $e->getType(), $e->getVariables());
-                       throw new AJAXException($exception->getMessage(), AJAXException::BAD_PARAMETERS, $e->getTraceAsString(), [
-                               'errorMessage' => $exception->getMessage(),
-                               'errorType' => $e->getType(),
-                               'fieldName' => $exception->getFieldName(),
-                               'realErrorMessage' => $exception->getErrorMessage()
-                       ]);
-               }
-               else if ($e instanceof ValidateActionException) {
-                       throw new AJAXException($e->getMessage(), AJAXException::BAD_PARAMETERS, $e->getTraceAsString(), [
-                               'errorMessage' => $e->getMessage(),
-                               'fieldName' => $e->getFieldName(),
-                               'realErrorMessage' => $e->getErrorMessage()
-                       ]);
-               }
-               else if ($e instanceof NamedUserException) {
-                       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), $e->getPrevious());
-               }
-       }
-       
        /**
         * Returns action response.
         * 
diff --git a/wcfsetup/install/files/lib/action/TAJAXException.class.php b/wcfsetup/install/files/lib/action/TAJAXException.class.php
new file mode 100644 (file)
index 0000000..656caca
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+namespace wcf\action;
+use wcf\system\exception\AJAXException;
+use wcf\system\exception\IllegalLinkException;
+use wcf\system\exception\InvalidSecurityTokenException;
+use wcf\system\exception\NamedUserException;
+use wcf\system\exception\PermissionDeniedException;
+use wcf\system\exception\UserInputException;
+use wcf\system\exception\ValidateActionException;
+
+/**
+ * Default implementation for the AJAXException throw method. 
+ *
+ * @author     Alexander Ebert, Joshua Ruesweg
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\Action
+ * @since       5.2
+ */
+trait TAJAXException {
+       /**
+        * Throws an previously caught exception while maintaining the propriate stacktrace.
+        *
+        * @param       \Exception|\Throwable   $e
+        * @throws      AJAXException
+        * @throws      \Exception
+        * @throws      \Throwable
+        */
+       protected function throwException($e) {
+               if ($e instanceof InvalidSecurityTokenException) {
+                       throw new AJAXException(WCF::getLanguage()->getDynamicVariable('wcf.ajax.error.sessionExpired'), AJAXException::SESSION_EXPIRED, $e->getTraceAsString());
+               }
+               else if ($e instanceof PermissionDeniedException) {
+                       throw new AJAXException(WCF::getLanguage()->getDynamicVariable('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS, $e->getTraceAsString());
+               }
+               else if ($e instanceof IllegalLinkException) {
+                       throw new AJAXException(WCF::getLanguage()->get('wcf.ajax.error.illegalLink'), AJAXException::ILLEGAL_LINK, $e->getTraceAsString());
+               }
+               else if ($e instanceof UserInputException) {
+                       // repackage as ValidationActionException
+                       $exception = new ValidateActionException($e->getField(), $e->getType(), $e->getVariables());
+                       throw new AJAXException($exception->getMessage(), AJAXException::BAD_PARAMETERS, $e->getTraceAsString(), [
+                               'errorMessage' => $exception->getMessage(),
+                               'errorType' => $e->getType(),
+                               'fieldName' => $exception->getFieldName(),
+                               'realErrorMessage' => $exception->getErrorMessage()
+                       ]);
+               }
+               else if ($e instanceof ValidateActionException) {
+                       throw new AJAXException($e->getMessage(), AJAXException::BAD_PARAMETERS, $e->getTraceAsString(), [
+                               'errorMessage' => $e->getMessage(),
+                               'fieldName' => $e->getFieldName(),
+                               'realErrorMessage' => $e->getErrorMessage()
+                       ]);
+               }
+               else if ($e instanceof NamedUserException) {
+                       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), $e->getPrevious());
+               }
+       }
+}