如果我在 API 控制器/路由中抛出异常,它总是返回一个包含堆栈跟踪的对象。我已经设置了 APP_DEBUG=FALSE 和 APP_ENV=production 但我总是得到如下所示的堆栈跟踪......
假设我将其中任何一个放入控制器方法中:
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);
它返回一个像这样的对象:
{
"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
关于为什么返回调试有什么想法吗?
您始终可以捕获这些错误并返回异常消息作为响应。
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);
}
您可以返回这些异常以及状态代码。
我也遇到了类似的问题,特别是因为中止助手非常好。查看该函数的内部结构后,我发现它可以工作。
abort( response()->json(['message'=>'Your message'], 401) );
您可以创建一个特征来处理所有异常,并更新您的Exception/handler.php以调用特征而不是显示跟踪。