Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
allow exception to change the error code from the exception code
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Apr 6, 2018
1 parent ac56957 commit 23afa55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/core/Directus/Application/ErrorHandlers/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,13 +83,18 @@ 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';
} else if (empty($message)) {
$message = 'Unknown Error';
}

if ($exception instanceof Exception) {
$code = $exception->getErrorCode();
}

$httpStatusCode = 500;
if ($exception instanceof BadRequestException) {
$httpStatusCode = 400;
Expand All @@ -101,7 +107,7 @@ public function processException($exception)
}

$data = [
'code' => $exception->getCode(),
'code' => $code,
'message' => $message
];

Expand Down
10 changes: 10 additions & 0 deletions src/core/Directus/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 23afa55

Please sign in to comment.