1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<?php
namespace JsonApi\Errors;
use Neomerx\JsonApi\Exceptions\JsonApiException;
use Neomerx\JsonApi\Schema\Error;
use Neomerx\JsonApi\Schema\ErrorCollection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Slim\App;
use Slim\Exception\HttpException;
use Throwable;
class ErrorHandler
{
/** @var \Slim\App */
private $app;
public function __construct(App $app)
{
$this->app = $app;
}
public function __invoke(
ServerRequestInterface $request,
Throwable $exception,
bool $displayErrorDetails,
bool $logErrors,
bool $logErrorDetails,
?LoggerInterface $logger = null
): ResponseInterface {
if ($logger) {
$logger->error($exception->getMessage());
}
$response = $this->app->getResponseFactory()->createResponse();
$response = $response->withHeader('Content-Type', 'application/json');
$response->getBody()->write($this->determinePayload($exception, $displayErrorDetails));
$code = $this->determineStatusCode($exception, $request->getMethod());
if ($code >= 500) {
error_log($exception);
}
return $response->withStatus($code);
}
protected function determineStatusCode(Throwable $exception, string $method): int
{
if ('OPTIONS' === $method) {
return 200;
}
if ($exception instanceof HttpException) {
return $exception->getCode();
}
if ($exception instanceof JsonApiException) {
return $exception->getHttpCode();
}
return 500;
}
protected function determinePayload(Throwable $exception, bool $displayErrorDetails): string
{
$message = '';
if ($exception instanceof JsonApiException) {
$httpCode = $exception->getHttpCode();
$errors = new ErrorCollection();
foreach ($exception->getErrors() as $error) {
$errors[] = $this->copyError($error, $displayErrorDetails, $exception);
}
} elseif ($exception instanceof HttpException) {
$errors = $this->createErrorCollection(
$exception->getCode(),
$exception->getMessage(),
$exception->getDescription(),
$exception,
$displayErrorDetails
);
} else {
$errors = $this->createErrorCollection(
'500',
$exception->getMessage(),
null,
$exception,
$displayErrorDetails
);
}
if (sizeof($errors)) {
$encoder = $this->app->getContainer()->get('json-api-error-encoder');
return $encoder->encodeErrors($errors);
}
return '';
}
private function createErrorCollection(
string $httpCode,
string $message,
?string $details,
Throwable $exception,
bool $displayErrorDetails
): ErrorCollection {
/** @var \Exception $exception */
$errors = new ErrorCollection();
$errors->add(
new Error(
null,
null,
null,
$httpCode,
null,
$message,
$details,
$displayErrorDetails ? ['backtrace' => explode("\n", $exception->getTraceAsString())] : null
)
);
return $errors;
}
private function copyError(Error $error, bool $displayErrorDetails, JsonApiException $exception): Error
{
$newError = new Error(
$error->getId(),
$error->getLinks(),
$error->getTypeLinks(),
$error->getStatus(),
$error->getCode(),
$error->getTitle(),
$error->getDetail(),
$displayErrorDetails ? ['backtrace' => explode("\n", $exception->getTraceAsString())] : null,
false,
$error->getMeta()
);
return $newError;
}
}
|