From 6f77909113a0fe5b8af0418c795270a716db5aa5 Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Wed, 30 Jan 2019 12:14:41 +0100 Subject: [PATCH] Add additional debug information on the exception problem (#3) --- README.md | 6 ++++++ spec/Http/ExceptionApiProblemSpec.php | 25 +++++++++++++++++++++++++ src/Http/ExceptionApiProblem.php | 5 ++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c4f2bd..e07a5f3 100644 --- a/README.md +++ b/README.md @@ -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]: ..." } ] diff --git a/spec/Http/ExceptionApiProblemSpec.php b/spec/Http/ExceptionApiProblemSpec.php index 7e172d2..be96727 100644 --- a/spec/Http/ExceptionApiProblemSpec.php +++ b/spec/Http/ExceptionApiProblemSpec.php @@ -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' => [], ], @@ -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, + ]); + } } diff --git a/src/Http/ExceptionApiProblem.php b/src/Http/ExceptionApiProblem.php index 71f1e5c..3074b01 100644 --- a/src/Http/ExceptionApiProblem.php +++ b/src/Http/ExceptionApiProblem.php @@ -23,7 +23,7 @@ public function __construct(Throwable $exception) : 500; parent::__construct($statusCode, [ - 'detail' => $exception->getMessage(), + 'detail' => $exception->getMessage() ?: \get_class($exception), ]); } @@ -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(), ]; }