求助,Laravel 重新接管 dingoApi 錯誤

figurant發表於2019-08-10

因為在專案上線後要遮蔽sql方面的報錯,所有按網上教的我讓laravel重新接管的錯誤處理
我在app/Providers/AppServiceProviderboot 方法里加上了

// 將所有的 Exception 全部交給 App\Exceptions\Handler 來處理\
app('api.exception')->register(function (\Exception $exception) {\
    $request = Request::capture();\
    return app('App\Exceptions\Handler')->render($request, $exception);\
});

然後修改 app/Exceptions/Handle 中的 render 方法

public function render($request, Exception $exception)\
{
    $class = get_class($exception);
    if ((!env('API_DEBUG'))
        && in_array($class, self::SHIELD_ERR)) {
        return [
            'status_code' => 500,
            'message' => '服務異常',
            'data' => []
        ];
    }
}
// 遮蔽報錯列表\
const SHIELD_ERR = [
    'Illuminate\Database\QueryException'
];

上線後再日誌裡看到這個報錯

Call to a member function send() on null {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function send() on null at /www/wwwroot/MeijietuWebApi/public/index.php:58)
[stacktrace]
#0 {main}
"}              
Uncaught Error: Call to a member function send() on null
in /www/wwwroot/MeijietuWebApi/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:112
Stack trace:
#0 /www/wwwroot/MeijietuWebApi/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(89): Illuminate\Foundation\Bootstrap\HandleExceptions->renderHttpResponse(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#1 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleException(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#2 {main}
  thrown {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 1): Uncaught Error: Call to a member function send() on null in /www/wwwroot/MeijietuWebApi/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:112
Stack trace:
#0 /www/wwwroot/MeijietuWebApi/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(89): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->renderHttpResponse(Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
#1 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleException(Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
#2 {main}
  thrown at /www/wwwroot/MeijietuWebApi/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:112)
[stacktrace]
#0 {main}
"}   

目前沒發現會有什麼影響,但還是想修好,就是看不懂

Maybe we are frend.

相關文章