public function handle(Exception $e)
{
if ($e instanceof JsonApiSerializableInterface) {
$status = $e->getStatusCode();
$errors = $e->getErrors();
} elseif ($e instanceof ValidationException) {
$status = 422;
$errors = $e->errors()->toArray();
$errors = array_map(function ($field, $messages) {
return ['detail' => implode("\n", $messages), 'source' => ['pointer' => '/data/attributes/' . $field]];
}, array_keys($errors), $errors);
} elseif ($e instanceof ModelNotFoundException) {
$status = 404;
$errors = [];
} else {
$status = 500;
$error = ['code' => $status, 'title' => 'Internal Server Error'];
if ($this->app->inDebugMode()) {
$error['detail'] = (string) $e;
}
$errors = [$error];
}
$document = new Document();
$document->setErrors($errors);
return new JsonApiResponse($document, $status);
}