mirror of
https://github.com/flarum/framework.git
synced 2025-02-01 05:38:03 +08:00
parent
26229db1fd
commit
0a2bdbaa09
|
@ -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)
|
||||
));
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user