mirror of
https://github.com/flarum/framework.git
synced 2025-02-08 06:41:00 +08:00
parent
26229db1fd
commit
0a2bdbaa09
|
@ -52,7 +52,7 @@ class ApiServiceProvider extends AbstractServiceProvider
|
||||||
|
|
||||||
$pipe->pipe(new HttpMiddleware\HandleErrors(
|
$pipe->pipe(new HttpMiddleware\HandleErrors(
|
||||||
$app->make(Registry::class),
|
$app->make(Registry::class),
|
||||||
$app->make(JsonApiFormatter::class),
|
new JsonApiFormatter($app->inDebugMode()),
|
||||||
$app->tagged(Reporter::class)
|
$app->tagged(Reporter::class)
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,13 @@ use Tobscure\JsonApi\Document;
|
||||||
*/
|
*/
|
||||||
class JsonApiFormatter implements HttpFormatter
|
class JsonApiFormatter implements HttpFormatter
|
||||||
{
|
{
|
||||||
|
private $includeTrace;
|
||||||
|
|
||||||
|
public function __construct($includeTrace = false)
|
||||||
|
{
|
||||||
|
$this->includeTrace = $includeTrace;
|
||||||
|
}
|
||||||
|
|
||||||
public function format(HandledError $error, Request $request): Response
|
public function format(HandledError $error, Request $request): Response
|
||||||
{
|
{
|
||||||
$document = new Document;
|
$document = new Document;
|
||||||
|
@ -30,20 +37,24 @@ class JsonApiFormatter implements HttpFormatter
|
||||||
if ($error->hasDetails()) {
|
if ($error->hasDetails()) {
|
||||||
$document->setErrors($this->withDetails($error));
|
$document->setErrors($this->withDetails($error));
|
||||||
} else {
|
} else {
|
||||||
$document->setErrors($this->simple($error));
|
$document->setErrors($this->default($error));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonApiResponse($document, $error->getStatusCode());
|
return new JsonApiResponse($document, $error->getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function simple(HandledError $error): array
|
private function default(HandledError $error): array
|
||||||
{
|
{
|
||||||
return [
|
$default = [
|
||||||
[
|
'status' => (string) $error->getStatusCode(),
|
||||||
'status' => (string) $error->getStatusCode(),
|
'code' => $error->getType(),
|
||||||
'code' => $error->getType(),
|
|
||||||
]
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($this->includeTrace) {
|
||||||
|
$default['detail'] = (string) $error->getException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return [$default];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function withDetails(HandledError $error): array
|
private function withDetails(HandledError $error): array
|
||||||
|
|
Loading…
Reference in New Issue
Block a user