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条)

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导