exceptionlaravel-5throwabort

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


If I throw an exception in my API controllers/routes it always returns an object including the stack trace. I have set APP_DEBUG=FALSE and APP_ENV=production yet I always get a stack trace like below...

Say I throw any one of these in a controllers method:

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

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

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

It returns an object like this:

{
    "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}()"...

I can't turn it off or figure out why it's adding it. Any help would be appreciated. Yes, I have dumped my .env variables and my config file to see what the system thinks the value is and invariably it's: "debug":false

Any ideas on why this is returning debug?


Solution

  • You can always catch these errors and return exception messages as responses.

    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); 
     }
    

    You can return these exceptions along with status codes.

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