dsj60862 2014-03-08 10:53
浏览 77

Symfony2:Custmize ErrorHandler-> handle()

Follow-up on unanswered questions Custom ExceptionListener picks up 403 & 404, but not 500 errors and Symfony 2.4: Why are 500 errors not caught by kernel.exception listener.

I have implemented a custom exception listerner which works brilliantly for 403 and 404 errors, but does not catch 500 errors. Some feedback I have gotten indicates that some 500 errors occur before Symfony has a chance to run and therefor cannot be caught by any listeners.

What I have noticed is that no matter what type of strange error it is that I generate, whether it is a syntax error, undefined variable, unknown method, method call on a non-object, etc, it seems that ErrorHandler->handle() or ErrorHandler->handleFatalError() is always being called in the development environment (see some samples below) (While the same is not true for the development environment, where I often get a normal PHP error not inside Symfony).

What I am trying to do is that, whenever an error like this occurs in the production environment, I ALWAYS show a user friendly (Oops, something went wrong) error page and ALWAYS send an email with the error specifics. To me it seems that if I could customize ErrorHandler->handle(), then I would be able to accomplish this. The reason for this is simple, I want to be made aware of any errors that occur to my systems that are in production. One would think that this is a relatively common feature and yet I am unable to do it.

I am still learning Symfony and even though I have implemented custom console commands, custom authentication handlers more of the complex security features, I am really struggling with this. I would really appreciate if someone with some extensive Symfony experience could tell me if this is possible or not and, if so, how I would go about doing it.

enter image description here

enter image description here

enter image description here

  • 写回答

2条回答 默认 最新

  • doutang1992 2014-03-12 14:55
    关注

    Actually I had the same problem and came up with a solution. I'm not sure if it is the best one, but it works for me at the moment.

    I have added following lines into the app.php (on top of app.php after the "use"-Statment):

    register_shutdown_function(function()
    {
        $error = error_get_last();
        if ($error != null)
        {
            $pageURL = 'http';
            if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
            $pageURL .= "://";
            if ($_SERVER["SERVER_PORT"] != "80") {
                $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
            } else {
                $pageURL .= $_SERVER["SERVER_NAME"]; 
            }
            header("Location: ".$pageURL."/senderrorrequest?message=".$error['message']);
        }
    });
    

    On this way the user will be redirected to a support-form and the error is logged in the error.log. If anybody has a better solution, I'm happy to read it.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c