Home  >  Article  >  PHP Framework  >  How to catch php errors with swoole

How to catch php errors with swoole

(*-*)浩
(*-*)浩Original
2019-12-16 09:35:121790browse

How to catch php errors with swoole

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn