From 40bed660f5daa1addf76ff41975fcead1f14073f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Mon, 7 Jan 2019 22:32:57 +0100 Subject: [PATCH] Add TAJAXException trait to reuse the throwException method See #2825 --- .../lib/action/AJAXInvokeAction.class.php | 53 +--------------- .../files/lib/action/TAJAXException.class.php | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 wcfsetup/install/files/lib/action/TAJAXException.class.php 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()); + } + } +} -- 2.20.1