Once a fatal error occurs during the running of swoole_serverServer, the client connection will not be responded to.
For example, a web server, if there is a fatal error, an HTTP 500 error message should be sent to the client. (Recommended study: swoole video tutorial)
In PHP, fatal errors can be captured through register_shutdown_function error_get_last 2 functions, and the error information is sent to the client connection.
The specific code examples are as follows:
register_shutdown_function('handleFatal'); function handleFatal() { $error = error_get_last(); switch ($error['type'] ?? null) { case E_ERROR : case E_PARSE : case E_CORE_ERROR : case E_COMPILE_ERROR : $message = $error['message'] . PHP_EOL; if (isset($_SERVER['REQUEST_URI'])) { $message .= '[QUERY] ' . $_SERVER['REQUEST_URI']; } // log or send: // error_log($message); // $server->send($fd, $message); break; } }
The above is the detailed content of How to catch php errors with swoole. For more information, please follow other related articles on the PHP Chinese website!