Skip to content

Commit a617ac4

Browse files
authored
Merge pull request #45 from async-interop/error-handler-fatal
Simplify error handler
2 parents 55926cf + 93bce0c commit a617ac4

File tree

2 files changed

+8
-69
lines changed

2 files changed

+8
-69
lines changed

src/Promise/ErrorHandler.php

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace AsyncInterop\Promise;
44

5-
use AsyncInterop\Promise;
6-
75
/**
86
* Global error handler for promises.
97
*
@@ -50,6 +48,8 @@ public static function set(callable $onError = null)
5048
*
5149
* This method MUST be called by every promise implementation if a callback passed to `Promise::when()` throws upon
5250
* invocation. It MUST NOT be called otherwise.
51+
*
52+
* @param \Exception|\Throwable $error Exception that occurred.
5353
*/
5454
public static function notify($error)
5555
{
@@ -97,43 +97,16 @@ private static function triggerErrorHandler($message, $error) {
9797
// We're already a last chance handler, throwing doesn't make sense, so use E_USER_ERROR.
9898
// E_USER_ERROR is recoverable by a handler set via set_error_handler, which might throw, too.
9999

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;
121101

122102
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);
133104
} catch (\Exception $e) {
134-
exit(255);
105+
\set_error_handler(null);
106+
\trigger_error($message, E_USER_ERROR);
135107
} catch (\Throwable $e) {
136-
exit(255);
108+
\set_error_handler(null);
109+
\trigger_error($message, E_USER_ERROR);
137110
}
138111
}
139112
}

test/phpt/error_handler_007.phpt

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)