doushi1996 2014-06-02 00:43
浏览 231
已采纳

Laravel处理用户生成的错误

I'm quite new to Laravel, and in past applications I would generate a user warning or notice, use a custom error handler, which would send an email.

Often the errors are cases where a user enters invalid data that can't be validated, or when a third party API call returns no results.

My trouble is that Laravel halts execution and displays a whoops page for any error, so generating an error probably isn't the way I should handle this, but what is the best (correct?) way?

  • 写回答

1条回答 默认 最新

  • dragon8899 2014-06-02 00:49
    关注

    For development environment it's recommended to use debug => true in app/config/app.php file so you'll get informative error messages. To disable it for production (recommended) just change this value to false, for example:

    /*
    |--------------------------------------------------------------------------
    | Application Debug Mode
    |--------------------------------------------------------------------------
    |
    | When your application is in debug mode, detailed error messages with
    | stack traces will be shown on every error that occurs within your
    | application. If disabled, a simple generic error page is shown.
    |
    */
    
    'debug' => false
    

    To register a custom error handler you may declare that handler in your app/start/global.php file, for example:

    // For 404 not found error
    App::missing(function($e){
        return Redirect::to('/');
    });
    

    This will catch all 404 not found errors but notice another error handler available in that file by default and it's:

    App::error(function(Exception $exception, $code)
    {
    Log::error($exception->getMessage());
    });
    

    This is the most generic error handler in Laravel means that any un-handaled error will be handled by this one and that's why declare your specific error handlers bottom of this error handler because, error handlers bubbles up from the bottom to top and this one is the parent so at last (if any custom handler was not registered or didn't catch the error) this handler will take the responsibility to handle an error if you didn't handle the error using any specific handler and didn't return any response when handling an error.

    For example; to register ModelNotFoundException exception you may try this:

    App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $e)
    {
        return Response::make('Not Found', 404);
    });
    

    Now, since you returned a response from the handler then the parent error handler will not get fired and response will be sent to the browser. If you didn't return a response then the topmost parent handler will be fired like event bubling in JavaScript.


    Read more about Errors & Logging on Laravel website.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图