diff --git a/src/Api/ApiServiceProvider.php b/src/Api/ApiServiceProvider.php index 807b0ff19..3aa568911 100644 --- a/src/Api/ApiServiceProvider.php +++ b/src/Api/ApiServiceProvider.php @@ -52,7 +52,7 @@ class ApiServiceProvider extends AbstractServiceProvider $pipe->pipe(new HttpMiddleware\HandleErrors( $app->make(Registry::class), - $app->make(JsonApiFormatter::class), + new JsonApiFormatter($app->inDebugMode()), $app->tagged(Reporter::class) )); diff --git a/src/Foundation/ErrorHandling/JsonApiFormatter.php b/src/Foundation/ErrorHandling/JsonApiFormatter.php index f67d067f5..d1abe8153 100644 --- a/src/Foundation/ErrorHandling/JsonApiFormatter.php +++ b/src/Foundation/ErrorHandling/JsonApiFormatter.php @@ -23,6 +23,13 @@ use Tobscure\JsonApi\Document; */ class JsonApiFormatter implements HttpFormatter { + private $includeTrace; + + public function __construct($includeTrace = false) + { + $this->includeTrace = $includeTrace; + } + public function format(HandledError $error, Request $request): Response { $document = new Document; @@ -30,20 +37,24 @@ class JsonApiFormatter implements HttpFormatter if ($error->hasDetails()) { $document->setErrors($this->withDetails($error)); } else { - $document->setErrors($this->simple($error)); + $document->setErrors($this->default($error)); } return new JsonApiResponse($document, $error->getStatusCode()); } - private function simple(HandledError $error): array + private function default(HandledError $error): array { - return [ - [ - 'status' => (string) $error->getStatusCode(), - 'code' => $error->getType(), - ] + $default = [ + 'status' => (string) $error->getStatusCode(), + 'code' => $error->getType(), ]; + + if ($this->includeTrace) { + $default['detail'] = (string) $error->getException(); + } + + return [$default]; } private function withDetails(HandledError $error): array