1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr7Middlewares\Utils; |
6
|
|
|
use Psr7Middlewares\Middleware; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Whoops\Run; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Middleware to handle php errors and exceptions. |
13
|
|
|
*/ |
14
|
|
|
class ErrorHandler |
15
|
|
|
{ |
16
|
|
|
const KEY = 'EXCEPTION'; |
17
|
|
|
|
18
|
|
|
use Utils\HandlerTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Run|null To handle errors using whoops |
22
|
|
|
*/ |
23
|
|
|
protected $whoops; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool Whether or not catch exceptions |
27
|
|
|
*/ |
28
|
|
|
protected $catchExceptions = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns the exception throwed. |
32
|
|
|
* |
33
|
|
|
* @param ServerRequestInterface $request |
34
|
|
|
* |
35
|
|
|
* @return \Exception|null |
36
|
|
|
*/ |
37
|
|
|
public static function getException(ServerRequestInterface $request) |
38
|
|
|
{ |
39
|
|
|
return Middleware::getAttribute($request, self::KEY); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set an instance of Whoops. |
44
|
|
|
* |
45
|
|
|
* @param Run $whoops |
46
|
|
|
* |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
|
|
public function whoops(Run $whoops) |
50
|
|
|
{ |
51
|
|
|
$this->whoops = $whoops; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Configure the catchExceptions. |
58
|
|
|
* |
59
|
|
|
* @param bool $catch |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
|
public function catchExceptions($catch = true) |
64
|
|
|
{ |
65
|
|
|
$this->catchExceptions = (boolean) $catch; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Execute the middleware. |
72
|
|
|
* |
73
|
|
|
* @param ServerRequestInterface $request |
74
|
|
|
* @param ResponseInterface $response |
75
|
|
|
* @param callable $next |
76
|
|
|
* |
77
|
|
|
* @return ResponseInterface |
78
|
|
|
*/ |
79
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
80
|
|
|
{ |
81
|
|
|
if ($this->whoops) { |
82
|
|
|
$this->whoops->pushHandler(function ($exception) use ($request, $response) { |
|
|
|
|
83
|
|
|
try { |
84
|
|
|
echo $this->executeHandler(Middleware::setAttribute($request, self::KEY, $exception), $response)->getBody(); |
85
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
86
|
|
|
//ignored |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
ob_start(); |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
$response = $next($request, $response); |
95
|
|
|
} catch (\Exception $exception) { |
96
|
|
|
if (!$this->catchExceptions) { |
97
|
|
|
throw $exception; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$request = Middleware::setAttribute($request, self::KEY, $exception); |
101
|
|
|
$response = $response->withStatus(500); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
ob_end_clean(); |
105
|
|
|
|
106
|
|
|
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) { |
107
|
|
|
try { |
108
|
|
|
return $this->executeHandler($request, $response); |
109
|
|
|
} catch (\Exception $exception) { |
110
|
|
|
//ignored |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->whoops) { |
115
|
|
|
$this->whoops->popHandler(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $response; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.