Skip to content

Commit

Permalink
Add additional debug information on the exception problem (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee authored Jan 30, 2019
1 parent 0a17c92 commit 6f77909
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ new ExceptionApiProblem(new \Exception('message', 500));
"detail": "message",
"exception": {
"message": "message",
"type": "RuntimeException",
"code": 500,
"line": 23,
"file": "exception.php",
"trace": "#0 [internal function]: ...",
"previous": [
{
"message": "previous",
"type": "InvalidArgumentException",
"code": 0,
"line": 20,
"file": "exception.php",
"trace": "#0 [internal function]: ..."
}
]
Expand Down
25 changes: 25 additions & 0 deletions spec/Http/ExceptionApiProblemSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ public function it_can_parse_to_debuggable_array(): void
'title' => HttpApiProblem::getTitleForStatusCode(500),
'detail' => 'message',
'exception' => [
'type' => Exception::class,
'message' => 'message',
'code' => 500,
'line' => $exception->getLine(),
'file' => $exception->getFile(),
'trace' => $exception->getTraceAsString(),
'previous' => [],
],
Expand All @@ -86,22 +89,44 @@ public function it_contains_flattened_previous_exceptions_in_debuggable_output()
'title' => HttpApiProblem::getTitleForStatusCode(500),
'detail' => 'message',
'exception' => [
'type' => Exception::class,
'message' => 'message',
'code' => 0,
'line' => $exception->getLine(),
'file' => $exception->getFile(),
'trace' => $exception->getTraceAsString(),
'previous' => [
[
'type' => Exception::class,
'message' => 'previous',
'code' => 2,
'line' => $previous->getLine(),
'file' => $previous->getFile(),
'trace' => $previous->getTraceAsString(),
],
[
'type' => Exception::class,
'message' => 'first',
'code' => 1,
'line' => $first->getLine(),
'file' => $first->getFile(),
'trace' => $first->getTraceAsString(),
],
],
],
]);
}

public function it_uses_the_class_of_the_exception_when_no_message_exists(): void
{
$exception = new Exception();
$this->beConstructedWith($exception);

$this->toArray()->shouldBe([
'status' => 500,
'type' => HttpApiProblem::TYPE_HTTP_RFC,
'title' => HttpApiProblem::getTitleForStatusCode(500),
'detail' => Exception::class,
]);
}
}
5 changes: 4 additions & 1 deletion src/Http/ExceptionApiProblem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(Throwable $exception)
: 500;

parent::__construct($statusCode, [
'detail' => $exception->getMessage(),
'detail' => $exception->getMessage() ?: \get_class($exception),
]);
}

Expand Down Expand Up @@ -51,8 +51,11 @@ public function toDebuggableArray(): array
private function serializeException(Throwable $throwable): array
{
return [
'type' => \get_class($throwable),
'message' => $throwable->getMessage(),
'code' => $throwable->getCode(),
'line' => $throwable->getLine(),
'file' => $throwable->getFile(),
'trace' => $throwable->getTraceAsString(),
];
}
Expand Down

0 comments on commit 6f77909

Please sign in to comment.