【问题标题】:How to catch all exceptions in Laravel如何在 Laravel 中捕获所有异常
【发布时间】:2023-03-16 07:00:01
【问题描述】:

我想知道是否有办法捕获 laravel 应用程序中抛出的所有异常并将它们存储在数据库中?

我一直在查看一些软件包,但找不到任何说明在何处以及如何捕获异常的内容。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    要捕获所有错误并记录它们,您需要像这样编辑App\Exceptions\Handler 文件

     public function render($request, Exception $exception)
    {
        if ($exception){
            // log the error
            return response()->json([
                'status' => $exception->getStatusCode(),
                'error' => $exception->getMessage()
            ]);
       }
        return parent::render($request, $exception);
    }
    

    【讨论】:

      【解决方案2】:

      Docs 中所述, 你需要自定义App\Exceptions\Handlerrender()方法。

      编辑app/Exceptions/Handler.php

      public function render($request, Exception $e)
      {
          $error =$e->getMessage();
      
          //do your stuff with the error message 
      
          return parent::render($request, $exception);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 2010-09-11
        • 1970-01-01
        相关资源
        最近更新 更多