Thin structure ignoring custom error handler in Fatal Error

For the backend of my new SPA, I want to easily transfer errors to the interface using special error and exception handlers.

I can add a custom error handler to the slim framework, for example:

$app->error(function (Exception $e) use ($app) {
   // do stuff
}

This, however, does not lead to fatal errors. I also define a default global error handler that captures fatal errors if they occur outside the Slim context.

set_error_handler(function() {
    echo "Huston we got a problem";
    die();
});

This handler receives a fine message when I do this:

require "asdasd.php"; // trigger fatal error
$app->get("/test", $authenticate, function () use ($app) {
    // useful stuff
});

but not when I do this:

$app->get("/test", $authenticate, function () use ($app) {
    require "asdasd.php"; // trigger fatal error
    // useful stuff
});

The last demo actually seems to start the default php error handler.

- , Slim, , forplevel, Slims ?

+4
1

:

use Slim\Slim;

$app = new Slim();

register_shutdown_function('fatal_handler');

function fatal_handler()
{
    if(! is_null(error_get_last()))
    {
        echo('There is a fatal error!');
    }
}

require 'xxxx';

$app->get('/test', function () use ($app)
{
    echo('This is a test');
});

$app->run();
0

Source: https://habr.com/ru/post/1538372/


All Articles