Skip to content

Commit

Permalink
Merge pull request #15 from veewee/non-numeric-error-codes
Browse files Browse the repository at this point in the history
Return 500 for non numeric exception codes
  • Loading branch information
veewee authored May 21, 2021
2 parents 33e0764 + f98f521 commit af35ef0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions spec/Http/ExceptionApiProblemSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,19 @@ public function it_uses_the_class_of_the_exception_when_no_message_exists(): voi
'detail' => Exception::class,
]);
}

public function it_should_deal_with_string_exception_codes(): void
{
$exception = new class($message = 'hell no') extends Exception {
protected $code = 'nope';
};
$this->beConstructedWith($exception);

$this->toArray()->shouldBe([
'status' => 500,
'type' => HttpApiProblem::TYPE_HTTP_RFC,
'title' => HttpApiProblem::getTitleForStatusCode(500),
'detail' => $message,
]);
}
}
2 changes: 1 addition & 1 deletion src/Http/ExceptionApiProblem.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(Throwable $exception)
{
$this->exception = $exception;
$exceptionCode = $exception->getCode();
$statusCode = $exceptionCode >= 400 && $exceptionCode <= 599
$statusCode = is_int($exception) && $exceptionCode >= 400 && $exceptionCode <= 599
? $exceptionCode
: 500;

Expand Down

0 comments on commit af35ef0

Please sign in to comment.