Laravel 中止和异常添加 debug/stack 跟踪到 API 响应 | APP_DEBUG=假

Laravel Abort and Exceptions adding debug/stack trace to API response | APP_DEBUG=FALSE

如果我在 API controllers/routes 中抛出异常,它总是 returns 包含堆栈跟踪的对象。我已经设置了 APP_DEBUG=FALSE 和 APP_ENV=production 但我总是得到如下所示的堆栈跟踪...

假设我在控制器方法中抛出其中任何一个:

throw new HttpException(410, 'Http Exception is gettting a stack trace.');

abort(404, 'Please tell me debug is not found!');

throw new UpdateResourceFailedException('Even my custom exception! How?', 422);

它returns一个像这样的对象:

{
    "message": "Message",
    "status_code": 410,
    "debug": {
        "line": 412,
        "file": "/var/www/example.com/app/Http/Controllers/OrderController.php",
        "class": "Symfony\Component\HttpKernel\Exception\HttpException",
        "trace": [
            "#0 [internal function]: App\Http\Controllers\OrderController->show()",
            "#1 /var/www/example.com/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array()",
            "#2 /var/www/example.com/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction()",
            "#3 /var/www/example.com/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch()",
            "#4 /var/www/example.com/vendor/laravel/framework/src/Illuminate/Routing/Route.php(160): Illuminate\Routing\Route->runController()",
            "#5 /var/www/example.com/vendor/laravel/framework/src/Illuminate/Routing/Router.php(572): Illuminate\Routing\Route->run()",
            "#6 /var/www/example.com/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}()",
            "#7 /var/www/example.com/vendor/dingo/api/src/Http/Middleware/Auth.php(55): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}()",
            "#8 /var/www/example.com/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Dingo\Api\Http\Middleware\Auth->handle()",
            "#9 /var/www/example.com/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()"...

我无法关闭它或弄清楚为什么要添加它。任何帮助,将不胜感激。是的,我已经转储了我的 .env 变量和我的配置文件,以查看系统认为值是什么,并且始终是: "debug":false

关于返回调试的原因有什么想法吗?

您始终可以捕获这些错误和 return 异常消息作为响应。

try{
    //throw exception here
     throw new UpdateResourceFailedException('Even my custom exception! How?', 422);
 }catch(UpdateResourceFailedException $ex){
   // when you want http response
   // return response(['custom_exception'=>$ex->getMessage()], 4xx); 
   // when you want resposne as json
     return response()->json(['custom_exception'=>$ex->getMessage()], 4xx); 
 }

您可以 return 这些异常以及状态代码。

https://www.php.net/manual/en/throwable.getmessage.php

您可以创建一个特征来处理所有异常并更新您的 exception/handler。php 以调用特征而不是显示跟踪。