Skip to content

Commit af35ef0

Browse files
authored
Merge pull request #15 from veewee/non-numeric-error-codes
Return 500 for non numeric exception codes
2 parents 33e0764 + f98f521 commit af35ef0

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

spec/Http/ExceptionApiProblemSpec.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,19 @@ public function it_uses_the_class_of_the_exception_when_no_message_exists(): voi
129129
'detail' => Exception::class,
130130
]);
131131
}
132+
133+
public function it_should_deal_with_string_exception_codes(): void
134+
{
135+
$exception = new class($message = 'hell no') extends Exception {
136+
protected $code = 'nope';
137+
};
138+
$this->beConstructedWith($exception);
139+
140+
$this->toArray()->shouldBe([
141+
'status' => 500,
142+
'type' => HttpApiProblem::TYPE_HTTP_RFC,
143+
'title' => HttpApiProblem::getTitleForStatusCode(500),
144+
'detail' => $message,
145+
]);
146+
}
132147
}

src/Http/ExceptionApiProblem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(Throwable $exception)
1818
{
1919
$this->exception = $exception;
2020
$exceptionCode = $exception->getCode();
21-
$statusCode = $exceptionCode >= 400 && $exceptionCode <= 599
21+
$statusCode = is_int($exception) && $exceptionCode >= 400 && $exceptionCode <= 599
2222
? $exceptionCode
2323
: 500;
2424

0 commit comments

Comments
 (0)