diff --git a/spec/Http/ExceptionApiProblemSpec.php b/spec/Http/ExceptionApiProblemSpec.php index 3ee61d3..c4757bd 100644 --- a/spec/Http/ExceptionApiProblemSpec.php +++ b/spec/Http/ExceptionApiProblemSpec.php @@ -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, + ]); + } } diff --git a/src/Http/ExceptionApiProblem.php b/src/Http/ExceptionApiProblem.php index a7c4829..4cd9b7c 100644 --- a/src/Http/ExceptionApiProblem.php +++ b/src/Http/ExceptionApiProblem.php @@ -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;