From: Joshua Rüsweg Date: Mon, 7 Jan 2019 21:32:57 +0000 (+0100) Subject: Add TAJAXException trait to reuse the throwException method X-Git-Tag: 5.2.0_Alpha_1~296^2~57 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=40bed660f5daa1addf76ff41975fcead1f14073f;p=GitHub%2FWoltLab%2FWCF.git Add TAJAXException trait to reuse the throwException method See #2825 --- diff --git a/wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php b/wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php index a5e1a94821..9769c73961 100644 --- a/wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php +++ b/wcfsetup/install/files/lib/action/AJAXInvokeAction.class.php @@ -1,15 +1,11 @@ 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 index 0000000000..656caca3e1 --- /dev/null +++ b/wcfsetup/install/files/lib/action/TAJAXException.class.php @@ -0,0 +1,63 @@ + + * @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()); + } + } +}