Skip to content

Commit ab1ec71

Browse files
authored
docs(worker): Prefer try-catch over set_exception_handler (#1897)
1 parent 219a540 commit ab1ec71

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

docs/worker.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ $myApp->boot();
7878

7979
// Handler outside the loop for better performance (doing less work)
8080
$handler = static function () use ($myApp) {
81-
// Called when a request is received,
82-
// superglobals, php://input and the like are reset
83-
echo $myApp->handle($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
81+
try {
82+
// Called when a request is received,
83+
// superglobals, php://input and the like are reset
84+
echo $myApp->handle($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
85+
} catch (\Throwable $exception) {
86+
// `set_exception_handler` is called only when the worker script ends,
87+
// which may not be what you expect, so catch and handle exceptions here
88+
(new \MyCustomExceptionHandler)->handleException($exception);
89+
}
8490
};
8591

8692
$maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 0);

0 commit comments

Comments
 (0)