Skip to content

Return 500 for non numeric exception codes #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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