Debug mode: Include stacktrace in JSON-API errors

Refs #1843, #1865.
This commit is contained in:
Franz Liedke 2019-09-04 23:35:32 +02:00
parent 26229db1fd
commit 0a2bdbaa09
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
2 changed files with 19 additions and 8 deletions

View File

@ -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)
));

View File

@ -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