1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Framework; |
10
|
|
|
|
11
|
|
|
use Spiral\Exceptions\AbstractHandler; |
12
|
|
|
use Spiral\Exceptions\ConsoleHandler; |
13
|
|
|
use Spiral\Exceptions\HtmlHandler; |
14
|
|
|
use Spiral\Framework\Exceptions\FatalException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ExceptionHandler is responsible for global error handling (outside of dispatchers). Handler usually used in case |
18
|
|
|
* of bootload errors. |
19
|
|
|
*/ |
20
|
|
|
final class ExceptionHandler |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Enable global exception handling. |
24
|
|
|
*/ |
25
|
|
|
public static function register() |
26
|
|
|
{ |
27
|
|
|
register_shutdown_function([self::class, 'handleShutdown']); |
28
|
|
|
set_error_handler([self::class, 'handleError']); |
29
|
|
|
set_exception_handler([self::class, 'handleException']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Handle php shutdown and search for fatal errors. |
35
|
|
|
*/ |
36
|
|
|
public static function handleShutdown() |
37
|
|
|
{ |
38
|
|
|
if (!empty($error = error_get_last())) { |
39
|
|
|
self::handleException( |
40
|
|
|
new FatalException($error['message'], $error['type'], 0, $error['file'], $error['line']) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Convert application error into exception. |
47
|
|
|
* |
48
|
|
|
* @param int $code |
49
|
|
|
* @param string $message |
50
|
|
|
* @param string $filename |
51
|
|
|
* @param int $line |
52
|
|
|
* |
53
|
|
|
* @throws \ErrorException |
54
|
|
|
*/ |
55
|
|
|
public static function handleError($code, $message, $filename = '', $line = 0) |
56
|
|
|
{ |
57
|
|
|
throw new \ErrorException($message, $code, 0, $filename, $line); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Handle exception and output error to the user. |
62
|
|
|
* |
63
|
|
|
* @param \Throwable $e |
64
|
|
|
*/ |
65
|
|
|
public static function handleException(\Throwable $e) |
66
|
|
|
{ |
67
|
|
|
if (php_sapi_name() == 'cli') { |
68
|
|
|
$handler = new ConsoleHandler(STDERR); |
|
|
|
|
69
|
|
|
} else { |
70
|
|
|
$handler = new HtmlHandler(HtmlHandler::INVERTED); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// we are safe to handle global exceptions (system level) with maximum verbosity |
74
|
|
|
fwrite(STDERR, $handler->renderException($e, AbstractHandler::VERBOSITY_VERBOSE)); |
75
|
|
|
} |
76
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: