Skip to content

Commit

Permalink
Merge pull request #1747 from specialtactics/bugfix/deal-with-weird-e…
Browse files Browse the repository at this point in the history
…xceptions

Deal with bad actors who throw http exceptions with invalid codes
  • Loading branch information
specialtactics authored Sep 9, 2020
2 parents 42c5b78 + 5a51774 commit 3813eb9
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Exception/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected function genericResponse(Throwable $exception)

$response = $this->newResponseArray();

array_walk_recursive($response, function (&$value, $key) use ($exception, $replacements) {
array_walk_recursive($response, function (&$value, $key) use ($replacements) {
if (Str::startsWith($value, ':') && isset($replacements[$value])) {
$value = $replacements[$value];
}
Expand All @@ -203,11 +203,23 @@ protected function genericResponse(Throwable $exception)
*/
protected function getStatusCode(Throwable $exception)
{
$statusCode = null;

if ($exception instanceof ValidationException) {
return $exception->status;
$statusCode = $exception->status;
} elseif ($exception instanceof HttpExceptionInterface) {
$statusCode = $exception->getStatusCode();
} else {
// By default throw 500
$statusCode = 500;
}

// Be extra defensive
if ($statusCode < 100 || $statusCode > 500) {
$statusCode = 500;
}

return $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
return $statusCode;
}

/**
Expand Down

0 comments on commit 3813eb9

Please sign in to comment.