douping3891 2016-04-09 03:28
浏览 67
已采纳

我可以在Silex中禁用错误​​/异常处理吗?

I'm building an app based on Silex 1.3. This is my first encounter with Silex, so I'm not very familiar with it.

I'd like to use my own error/exception handler, which is basically a class that registers itself and then will catch all errors, fatal errors, and uncaught exceptions and handle them, either with Whoops in development, or a graceful handler in production.

However, once I'm inside a silex controller, middleware, whatever, Silex will take over and use it's own error handling. Mine will still catch fatal errors, since Silex apparently doesn't hook into shutdown, but everything else is replaced with the default "Something went wrong" page from Silex.

I do understand that I can use $app->error() to override HOW Silex handles errors, but I haven't found a way to set things back to the original ErrorHandler from there, or to override WHETHER Silex handles errors.

So, does anyone know how to either a) tell Silex to use my error handler, using $app->error() or some other way, b) just disable error handling in Silex entirely, or c) as a last resort, get Silex to catch fatal errors so I can handle all three types from within $app->error()?

Since this is my first time using Silex, feel free to correct me or show me how you handle errors in Silex if there's a better way, but please also answer the question if you can.

Some example code:

// This will register itself and then handle all errors.
$handler = new ErrorHandler();

// These are all handled appropriately.
nonexistentfunction();            // Correctly caught by ErrorHandler::handleFatalError
trigger_error("example");         // Correctly caught by ErrorHandler::handlePhpError
throw new \Exception("example");  // Correctly caught by ErrorHandler::handleException

$app = new \Silex\Application();
$app->get('/', function () use ($app) {

    // This is still handled correctly.
    nonexistentfunction();            // Correctly caught by ErrorHandler::handleFatalError

    // However, these are now overridden by Silex.
    trigger_error("example");         // INCORRECTLY DISPLAYS SILEX ERROR PAGE.
    throw new \Exception("example");  // INCORRECTLY DISPLAYS SILEX ERROR PAGE.

});
$app->run();

And a very simplified ErrorHandler for reference:

Class ErrorHandler
{
    public function __construct()
    {
        $this->register();
    }

    private function register()
    {
        register_shutdown_function( array($this, "handleFatalError") );
        set_error_handler(array($this, "handlePhpError"));
        set_exception_handler(array($this, "handleException"));
    }

    // Etc.

}
  • 写回答

4条回答 默认 最新

  • douqian1835 2016-04-10 19:49
    关注

    I know the option the (b) you can entirely disable Silex app error handler and after that, your custom error handler should work fine as you defined it.

    Entirely disabled Silex error handler:

    $app['exception_handler']->disable();
    

    So, It will be like:

    require_once  'Exception.php'; # Load the class
    $handler = new ErrorHandler(); # Initialize/Register it
    
    $app = new \Silex\Application();
    $app->get('/', function () use ($app) {
    
    
        nonexistentfunction();  
        trigger_error("example");
        throw new \Exception("example");
    
    });
    $app->run();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败