From 8c45d3506eac24ed2c4c2bb3e750d139e2403dc1 Mon Sep 17 00:00:00 2001 From: Lucas dos Santos Abreu Date: Thu, 8 Jul 2021 15:04:50 -0300 Subject: [PATCH] (fix): exception will never be a int --- spec/Http/ExceptionApiProblemSpec.php | 13 +++++++++++++ src/Http/ExceptionApiProblem.php | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) 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;