diff --git a/spec/Http/ExceptionApiProblemSpec.php b/spec/Http/ExceptionApiProblemSpec.php index c4757bd..c9e1b8b 100644 --- a/spec/Http/ExceptionApiProblemSpec.php +++ b/spec/Http/ExceptionApiProblemSpec.php @@ -144,4 +144,17 @@ public function it_should_deal_with_string_exception_codes(): void 'detail' => $message, ]); } + + public function it_should_use_code_as_status_code_when_valid_http_status_code_error(): void + { + $message = 'an honest error'; + $this->beConstructedWith(new \InvalidArgumentException($message, 400)); + + $this->toArray()->shouldBe([ + 'status' => 400, + 'type' => HttpApiProblem::TYPE_HTTP_RFC, + 'title' => HttpApiProblem::getTitleForStatusCode(400), + 'detail' => $message, + ]); + } } diff --git a/src/Http/ExceptionApiProblem.php b/src/Http/ExceptionApiProblem.php index 4cd9b7c..669e68e 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 = is_int($exception) && $exceptionCode >= 400 && $exceptionCode <= 599 + $statusCode = is_int($exceptionCode) && $exceptionCode >= 400 && $exceptionCode <= 599 ? $exceptionCode : 500;