-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRegisterExceptionHandler.php
More file actions
149 lines (128 loc) · 3.95 KB
/
RegisterExceptionHandler.php
File metadata and controls
149 lines (128 loc) · 3.95 KB
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
146
147
148
149
<?php
namespace Rareloop\Lumberjack\Bootstrappers;
use Error;
use ErrorException;
use DI\NotFoundException;
use function Http\Response\send;
use Rareloop\Router\Responsable;
use Rareloop\Lumberjack\Application;
use Psr\Http\Message\ResponseInterface;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Rareloop\Lumberjack\Exceptions\HandlerInterface;
use Symfony\Component\Debug\Exception\FatalErrorException;
use Symfony\Component\ErrorHandler\Error\FatalError;
/**
* Determine whether or not we should be in debug mode or not
* If not debug:
* Register exception handlers against a `handle` function
* Get the handler from the container (have an object that implements an interface that the app can extend from)
* Call handle on the resolved object
*/
class RegisterExceptionHandler
{
private $app;
public function bootstrap(Application $app)
{
$this->app = $app;
if (is_admin()) {
return;
}
error_reporting(-1);
set_error_handler([$this, 'handleError']);
set_exception_handler([$this, 'handleException']);
register_shutdown_function([$this, 'handleShutdown']);
}
public function handleException($e)
{
if ($e instanceof Error) {
$e = new ErrorException($e->getMessage(), 0, E_ERROR, $e->getFile(), $e->getLine());
}
$handler = $this->getExceptionHandler();
$handler->report($e);
try {
$request = $this->app->get('request');
} catch (NotFoundException $notFoundException) {
$request = ServerRequestFactory::fromGlobals(
$_SERVER,
$_GET,
$_POST,
$_COOKIE,
$_FILES
);
}
if ($e instanceof Responsable) {
$this->send($e->toResponse($request));
return;
}
$this->send($handler->render($request, $e));
}
public function send(ResponseInterface $response)
{
@(new SapiEmitter())->emit($response);
}
protected function getExceptionHandler(): HandlerInterface
{
return $this->app->get(HandlerInterface::class);
}
/**
* Convert PHP errors to ErrorException instances.
*
* @param int $level
* @param string $message
* @param string $file
* @param int $line
* @param array $context
* @return void
*
* @throws \ErrorException
*/
public function handleError($level, $message, $file = '', $line = 0, $context = [])
{
$exception = new ErrorException($message, 0, $level, $file, $line);
$errorsToReportOnly = $this->app->get('config')->get('app.errors.reportOnly') ?: [
E_USER_NOTICE,
E_USER_DEPRECATED,
E_DEPRECATED
];
if (in_array($level, $errorsToReportOnly)) {
$this->getExceptionHandler()->report($exception);
return;
}
if (error_reporting() & $level) {
throw $exception;
}
}
public function handleShutdown()
{
if (!is_null($error = error_get_last()) && $this->isFatal($error['type'])) {
$this->handleException($this->fatalExceptionFromError($error, 0));
}
}
/**
* Create a new fatal exception instance from an error array.
*
* @param array $error
* @param int|null $traceOffset
* @return \Symfony\Component\ErrorHandler\Error\FatalError
*/
protected function fatalExceptionFromError(array $error, $traceOffset = null)
{
return new FatalError(
$error['message'],
0,
$error,
$traceOffset
);
}
/**
* Determine if the error type is fatal.
*
* @param int $type
* @return bool
*/
protected function isFatal($type)
{
return in_array($type, [E_COMPILE_ERROR, E_CORE_ERROR, E_ERROR, E_PARSE]);
}
}