1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Igni\Network\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use ErrorException; |
6
|
|
|
use Igni\Network\Exception\HttpException; |
7
|
|
|
use Igni\Network\Http\Response; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Middleware for error handling. If an exception is thrown and not catch during the request cycle, |
16
|
|
|
* it will appear here. Middleware will catch it and return response with status code (500) and exception message |
17
|
|
|
* as a body. |
18
|
|
|
* |
19
|
|
|
* @package Igni\Http\Middleware |
20
|
|
|
*/ |
21
|
|
|
final class ErrorMiddleware implements MiddlewareInterface |
22
|
|
|
{ |
23
|
|
|
private $errorHandler; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ErrorMiddleware constructor. |
27
|
|
|
* |
28
|
|
|
* @param callable $errorHandler |
29
|
|
|
*/ |
30
|
4 |
|
public function __construct(callable $errorHandler) |
31
|
|
|
{ |
32
|
4 |
|
$this->errorHandler = $errorHandler; |
33
|
4 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @see MiddlewareInterface::process |
37
|
|
|
* |
38
|
|
|
* @param ServerRequestInterface $request |
39
|
|
|
* @param RequestHandlerInterface $next |
40
|
|
|
* @return ResponseInterface |
41
|
|
|
*/ |
42
|
3 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface |
43
|
|
|
{ |
44
|
3 |
|
$this->setErrorHandler(); |
45
|
|
|
|
46
|
|
|
try { |
47
|
3 |
|
$response = $next->handle($request); |
48
|
|
|
|
49
|
3 |
|
} catch (Throwable $exception) { |
50
|
3 |
|
$result = ($this->errorHandler)($exception); |
51
|
3 |
|
if ($result instanceof Throwable) { |
52
|
|
|
$exception = $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
if ($exception instanceof HttpException) { |
|
|
|
|
56
|
1 |
|
$response = $exception->toResponse(); |
57
|
|
|
} else { |
58
|
2 |
|
$response = Response::asText($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
$this->restoreErrorHandler(); |
63
|
|
|
|
64
|
3 |
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
3 |
|
private function setErrorHandler(): void |
69
|
|
|
{ |
70
|
|
|
set_error_handler(function (int $number, string $message, string $file, int $line) { |
71
|
|
|
|
72
|
1 |
|
if (!(error_reporting() & $number)) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
throw new ErrorException($message, 0, $number, $file, $line); |
77
|
3 |
|
}); |
78
|
3 |
|
} |
79
|
|
|
|
80
|
3 |
|
private function restoreErrorHandler(): void |
81
|
|
|
{ |
82
|
3 |
|
restore_error_handler(); |
83
|
3 |
|
} |
84
|
|
|
} |
85
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.