【问题标题】:Why does the silex error handler not run?为什么 silex 错误处理程序不运行?
【发布时间】:2017-01-18 11:20:02
【问题描述】:

我有一个 silex 应用程序。我有一个注册的通用处理程序:

$app->error(function (\Exception $e) use ($app) {
    return (new JsonResponse(['message' => 'Something truly horrible happened. Sorry.', 500)])->send();
    }
);

然而,当 $app::share() 中出现异常时,它不会触发:

$app->share(function () {
    throw new \Exception('wtf');
}));

这个异常不会被错误处理程序处理,而是直接弹出,就好像根本没有处理程序一样:

exception 'Exception' with message 'wtf' in appinit.php:230
Stack trace: #0 ./vendor/pimple/pimple/lib/Pimple.php(126): Closure$#12() #1 ./vendor/pimple/pimple/lib/Pimple.php(83): Closure$Pimple::share() #2 ./web/appinit.php(234): Pimple->offsetGet() #3 app.php(6): include() #4 {main}

我只是期望一个带有内容的基本 400 Bad Request 响应:

{
  "message": "Something truly horrible happened. Sorry.",
}

为什么我的处理程序没有激活?

【问题讨论】:

  • Silex Exception handler的可能重复
  • 我认为这与您发布的问题无关。您是否使用 ($app['debug'] = true) 上的运行标志运行此代码?如果是这样,你能把它关掉看看会发生什么吗?

标签: php exception silex


【解决方案1】:

在 silex 中,错误处理程序仅处理中间件上发生的异常。所以事实上,错误永远不会被调用,这是设计使然,因为异常发生在:

try {
    $app->run()
} catch(\Exception $e) {
    // this will be caught
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2021-09-24
    • 2011-06-24
    相关资源
    最近更新 更多