Silex Exception handler

2019-06-14 14:40发布

I have big problem I don't know why exceptions are not catch by silex exception handler ?

My simple Code looks like this:

<?php

use Silex\Application;

use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\FormServiceProvider;
use Symfony\Component\HttpFoundation\Request;

$app = new Application();

// SPL Logic Exceptions
// Handle other exception as 500 errors
$app->error(function (\Exception $e, $code) {
exit('asd');
});
throw new Exception('test');
return $app;

And the result is:

Fatal error: Uncaught exception 'Exception' with message 'test'

1条回答
神经病院院长
2楼-- · 2019-06-14 15:13

The error listener is only able to catch exceptions thrown from within a controller or a before middleware. Here's an example that works:

$app = new Silex\Application();

$app->error(function (\Exception $e, $code) {
    exit('asd');
});

$app->before(function ($request) {
    throw new Exception('test');
});

$app->run();
查看更多
登录 后发表回答