Skip to content

Commit 6f77909

Browse files
authored
Add additional debug information on the exception problem (#3)
1 parent 0a17c92 commit 6f77909

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,18 @@ new ExceptionApiProblem(new \Exception('message', 500));
5959
"detail": "message",
6060
"exception": {
6161
"message": "message",
62+
"type": "RuntimeException",
6263
"code": 500,
64+
"line": 23,
65+
"file": "exception.php",
6366
"trace": "#0 [internal function]: ...",
6467
"previous": [
6568
{
6669
"message": "previous",
70+
"type": "InvalidArgumentException",
6771
"code": 0,
72+
"line": 20,
73+
"file": "exception.php",
6874
"trace": "#0 [internal function]: ..."
6975
}
7076
]

spec/Http/ExceptionApiProblemSpec.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ public function it_can_parse_to_debuggable_array(): void
6565
'title' => HttpApiProblem::getTitleForStatusCode(500),
6666
'detail' => 'message',
6767
'exception' => [
68+
'type' => Exception::class,
6869
'message' => 'message',
6970
'code' => 500,
71+
'line' => $exception->getLine(),
72+
'file' => $exception->getFile(),
7073
'trace' => $exception->getTraceAsString(),
7174
'previous' => [],
7275
],
@@ -86,22 +89,44 @@ public function it_contains_flattened_previous_exceptions_in_debuggable_output()
8689
'title' => HttpApiProblem::getTitleForStatusCode(500),
8790
'detail' => 'message',
8891
'exception' => [
92+
'type' => Exception::class,
8993
'message' => 'message',
9094
'code' => 0,
95+
'line' => $exception->getLine(),
96+
'file' => $exception->getFile(),
9197
'trace' => $exception->getTraceAsString(),
9298
'previous' => [
9399
[
100+
'type' => Exception::class,
94101
'message' => 'previous',
95102
'code' => 2,
103+
'line' => $previous->getLine(),
104+
'file' => $previous->getFile(),
96105
'trace' => $previous->getTraceAsString(),
97106
],
98107
[
108+
'type' => Exception::class,
99109
'message' => 'first',
100110
'code' => 1,
111+
'line' => $first->getLine(),
112+
'file' => $first->getFile(),
101113
'trace' => $first->getTraceAsString(),
102114
],
103115
],
104116
],
105117
]);
106118
}
119+
120+
public function it_uses_the_class_of_the_exception_when_no_message_exists(): void
121+
{
122+
$exception = new Exception();
123+
$this->beConstructedWith($exception);
124+
125+
$this->toArray()->shouldBe([
126+
'status' => 500,
127+
'type' => HttpApiProblem::TYPE_HTTP_RFC,
128+
'title' => HttpApiProblem::getTitleForStatusCode(500),
129+
'detail' => Exception::class,
130+
]);
131+
}
107132
}

src/Http/ExceptionApiProblem.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(Throwable $exception)
2323
: 500;
2424

2525
parent::__construct($statusCode, [
26-
'detail' => $exception->getMessage(),
26+
'detail' => $exception->getMessage() ?: \get_class($exception),
2727
]);
2828
}
2929

@@ -51,8 +51,11 @@ public function toDebuggableArray(): array
5151
private function serializeException(Throwable $throwable): array
5252
{
5353
return [
54+
'type' => \get_class($throwable),
5455
'message' => $throwable->getMessage(),
5556
'code' => $throwable->getCode(),
57+
'line' => $throwable->getLine(),
58+
'file' => $throwable->getFile(),
5659
'trace' => $throwable->getTraceAsString(),
5760
];
5861
}

0 commit comments

Comments
 (0)