1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Middleware; |
4
|
|
|
|
5
|
|
|
use App\Renderer\JsonRenderer; |
6
|
|
|
use DomainException; |
7
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
13
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Slim\Exception\HttpException; |
|
|
|
|
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
final class ExceptionMiddleware implements MiddlewareInterface |
19
|
|
|
{ |
20
|
|
|
private ResponseFactoryInterface $responseFactory; |
21
|
|
|
private JsonRenderer $renderer; |
22
|
|
|
private ?LoggerInterface $logger; |
23
|
|
|
private bool $displayErrorDetails; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
ResponseFactoryInterface $responseFactory, |
27
|
|
|
JsonRenderer $jsonRenderer, |
28
|
|
|
LoggerInterface $logger = null, |
29
|
|
|
bool $displayErrorDetails = false, |
30
|
|
|
) { |
31
|
|
|
$this->responseFactory = $responseFactory; |
32
|
|
|
$this->renderer = $jsonRenderer; |
33
|
|
|
$this->displayErrorDetails = $displayErrorDetails; |
34
|
|
|
$this->logger = $logger; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
|
|
return $handler->handle($request); |
41
|
|
|
} catch (Throwable $exception) { |
42
|
|
|
return $this->render($exception, $request); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function render( |
47
|
|
|
Throwable $exception, |
48
|
|
|
ServerRequestInterface $request, |
49
|
|
|
): ResponseInterface { |
50
|
|
|
$httpStatusCode = $this->getHttpStatusCode($exception); |
51
|
|
|
$response = $this->responseFactory->createResponse($httpStatusCode); |
52
|
|
|
|
53
|
|
|
// Log error |
54
|
|
|
if (isset($this->logger)) { |
55
|
|
|
$this->logger->error( |
|
|
|
|
56
|
|
|
sprintf( |
57
|
|
|
'%s;Code %s;File: %s;Line: %s', |
58
|
|
|
$exception->getMessage(), |
59
|
|
|
$exception->getCode(), |
60
|
|
|
$exception->getFile(), |
61
|
|
|
$exception->getLine() |
62
|
|
|
), |
63
|
|
|
$exception->getTrace() |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Content negotiation |
68
|
|
|
if (str_contains($request->getHeaderLine('Accept'), 'application/json')) { |
69
|
|
|
$response = $response->withAddedHeader('Content-Type', 'application/json'); |
70
|
|
|
|
71
|
|
|
// JSON |
72
|
|
|
return $this->renderJson($exception, $response); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// HTML |
76
|
|
|
return $this->renderHtml($response, $exception); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function renderJson(Throwable $exception, ResponseInterface $response): ResponseInterface |
80
|
|
|
{ |
81
|
|
|
$data = [ |
82
|
|
|
'error' => [ |
83
|
|
|
'message' => $exception->getMessage(), |
84
|
|
|
], |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
return $this->renderer->json($response, $data); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function renderHtml(ResponseInterface $response, Throwable $exception): ResponseInterface |
91
|
|
|
{ |
92
|
|
|
$response = $response->withHeader('Content-Type', 'text/html'); |
93
|
|
|
|
94
|
|
|
$message = sprintf( |
95
|
|
|
"\n<br>Error %s (%s)\n<br>Message: %s\n<br>", |
96
|
|
|
$this->html((string)$response->getStatusCode()), |
|
|
|
|
97
|
|
|
$this->html($response->getReasonPhrase()), |
|
|
|
|
98
|
|
|
$this->html($exception->getMessage()), |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
if ($this->displayErrorDetails) { |
102
|
|
|
$message .= sprintf( |
103
|
|
|
'File: %s, Line: %s', |
104
|
|
|
$this->html($exception->getFile()), |
105
|
|
|
$this->html((string)$exception->getLine()) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$response->getBody()->write($message); |
110
|
|
|
|
111
|
|
|
return $response; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function getHttpStatusCode(Throwable $exception): int |
115
|
|
|
{ |
116
|
|
|
$statusCode = StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR; |
117
|
|
|
|
118
|
|
|
if ($exception instanceof HttpException) { |
119
|
|
|
$statusCode = $exception->getCode(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($exception instanceof DomainException || $exception instanceof InvalidArgumentException) { |
123
|
|
|
$statusCode = StatusCodeInterface::STATUS_BAD_REQUEST; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $statusCode; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function html(string $text): string |
130
|
|
|
{ |
131
|
|
|
return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths