|
2 | 2 |
|
3 | 3 | namespace AsyncInterop\Promise;
|
4 | 4 |
|
5 |
| -use AsyncInterop\Promise; |
6 |
| - |
7 | 5 | /**
|
8 | 6 | * Global error handler for promises.
|
9 | 7 | *
|
@@ -50,6 +48,8 @@ public static function set(callable $onError = null)
|
50 | 48 | *
|
51 | 49 | * This method MUST be called by every promise implementation if a callback passed to `Promise::when()` throws upon
|
52 | 50 | * invocation. It MUST NOT be called otherwise.
|
| 51 | + * |
| 52 | + * @param \Exception|\Throwable $error Exception that occurred. |
53 | 53 | */
|
54 | 54 | public static function notify($error)
|
55 | 55 | {
|
@@ -97,43 +97,16 @@ private static function triggerErrorHandler($message, $error) {
|
97 | 97 | // We're already a last chance handler, throwing doesn't make sense, so use E_USER_ERROR.
|
98 | 98 | // E_USER_ERROR is recoverable by a handler set via set_error_handler, which might throw, too.
|
99 | 99 |
|
100 |
| - try { |
101 |
| - \trigger_error( |
102 |
| - $message . "\n\n" . (string) $error, |
103 |
| - E_USER_ERROR |
104 |
| - ); |
105 |
| - } catch (\Exception $e) { |
106 |
| - self::panic($e); |
107 |
| - } catch (\Throwable $e) { |
108 |
| - self::panic($e); |
109 |
| - } |
110 |
| - } |
111 |
| - |
112 |
| - private static function panic($error) { |
113 |
| - // The set error handler did throw or not exist, PHP's error handler threw, no chance to handle the error |
114 |
| - // gracefully at this time. PANIC! |
115 |
| - |
116 |
| - // Print error information to STDERR so the reason for the program abortion can be found, but do not expose |
117 |
| - // exception message and trace, as they might contain sensitive information and we do not know whether STDERR |
118 |
| - // might be available to an untrusted user. |
119 |
| - |
120 |
| - // Exit with the same code as if PHP exits because of an uncaught exception. |
| 100 | + $message .= "\n\n" . (string) $error; |
121 | 101 |
|
122 | 102 | try {
|
123 |
| - // fputs might fail due to a closed pipe |
124 |
| - // no STDERR, because it doesn't exist on piped STDIN |
125 |
| - // no finally, because PHP 5.4 |
126 |
| - \fputs(\fopen("php://stderr", "w"), \sprintf( |
127 |
| - "Fatal error: Uncaught exception '%s' while trying to report a throwing AsyncInterop\\Promise::when()" |
128 |
| - . " handler gracefully." . \PHP_EOL, |
129 |
| - \get_class($error) |
130 |
| - )); |
131 |
| - |
132 |
| - exit(255); |
| 103 | + \trigger_error($message, E_USER_ERROR); |
133 | 104 | } catch (\Exception $e) {
|
134 |
| - exit(255); |
| 105 | + \set_error_handler(null); |
| 106 | + \trigger_error($message, E_USER_ERROR); |
135 | 107 | } catch (\Throwable $e) {
|
136 |
| - exit(255); |
| 108 | + \set_error_handler(null); |
| 109 | + \trigger_error($message, E_USER_ERROR); |
137 | 110 | }
|
138 | 111 | }
|
139 | 112 | }
|
0 commit comments