From 23afa5545ea9f75702a41eb686433c2002f70617 Mon Sep 17 00:00:00 2001 From: Welling Guzman Date: Fri, 6 Apr 2018 08:19:54 -0400 Subject: [PATCH] allow exception to change the error code from the exception code --- .../Application/ErrorHandlers/ErrorHandler.php | 8 +++++++- src/core/Directus/Exception/Exception.php | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/core/Directus/Application/ErrorHandlers/ErrorHandler.php b/src/core/Directus/Application/ErrorHandlers/ErrorHandler.php index 4544fb392a..9018bba398 100644 --- a/src/core/Directus/Application/ErrorHandlers/ErrorHandler.php +++ b/src/core/Directus/Application/ErrorHandlers/ErrorHandler.php @@ -7,6 +7,7 @@ use Directus\Database\Exception\InvalidQueryException; use Directus\Exception\BadRequestException; use Directus\Exception\ErrorException; +use Directus\Exception\Exception; use Directus\Exception\ForbiddenException; use Directus\Exception\NotFoundException; use Directus\Exception\UnauthorizedException; @@ -82,6 +83,7 @@ public function processException($exception) $this->trigger($exception); $message = $exception->getMessage(); + $code = null; // Not showing internal PHP errors (for PHP7) for production if ($productionMode && $this->isError($exception)) { $message = 'Internal Server Error'; @@ -89,6 +91,10 @@ public function processException($exception) $message = 'Unknown Error'; } + if ($exception instanceof Exception) { + $code = $exception->getErrorCode(); + } + $httpStatusCode = 500; if ($exception instanceof BadRequestException) { $httpStatusCode = 400; @@ -101,7 +107,7 @@ public function processException($exception) } $data = [ - 'code' => $exception->getCode(), + 'code' => $code, 'message' => $message ]; diff --git a/src/core/Directus/Exception/Exception.php b/src/core/Directus/Exception/Exception.php index d65dec94e7..7647305d31 100644 --- a/src/core/Directus/Exception/Exception.php +++ b/src/core/Directus/Exception/Exception.php @@ -5,4 +5,14 @@ class Exception extends \Exception { const ERROR_CODE = 0; + + /** + * Allows child class to extend the error code value method + * + * @return int + */ + public function getErrorCode() + { + return static::ERROR_CODE; + } }