在laravel中默認(rèn)返回的異常是html的形式展現(xiàn)在頁(yè)面上面渔伯,當(dāng)我們寫api接口時(shí)候希望返回json給前端希望能夠不會(huì)出現(xiàn)神奇的bug迅诬。
找到文件Exceptions\Handler.php重寫render方法
重寫代碼
/**
* 重寫render
* @param $request
* @param Throwable $e
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response|void
* @throws Throwable
*/
public function render($request, Throwable $e)
{
if (!$request->is('api/*')) {
return parent::render($request, $e);
}
$error = $this->convertExceptionToResponse($e);
$api_e = FlattenException::create($e);
$debug['line'] = $api_e->getLine();
$debug['file'] = $api_e->getFile();
$debug['class'] = $api_e->getClass();
$debug['trace'] = $api_e->getTrace();
return response()->json([
'status' => false,
'code'=>$error->getStatusCode(),
'message'=>$api_e->getMessage(),
'data'=>$debug,
], $error->getStatusCode());
}
返回內(nèi)容
{
"status": false,
"code": 404,
"message": "",
"data": {
"line": 43,
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php",
"class": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php",
"line": 43,
"args": []
},
{
"namespace": "Illuminate\\Routing",
"short_class": "AbstractRouteCollection",
"class": "Illuminate\\Routing\\AbstractRouteCollection",
"type": "->",
"function": "handleMatchedRoute",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
"line": 162,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"null",
null
]
]
},
{
"namespace": "Illuminate\\Routing",
"short_class": "RouteCollection",
"class": "Illuminate\\Routing\\RouteCollection",
"type": "->",
"function": "match",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 673,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Routing",
"short_class": "Router",
"class": "Illuminate\\Routing\\Router",
"type": "->",
"function": "findRoute",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 662,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Routing",
"short_class": "Router",
"class": "Illuminate\\Routing\\Router",
"type": "->",
"function": "dispatchToRoute",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 651,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Routing",
"short_class": "Router",
"class": "Illuminate\\Routing\\Router",
"type": "->",
"function": "dispatch",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 167,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http",
"short_class": "Kernel",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->",
"function": "Illuminate\\Foundation\\Http\\{closure}",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 128,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Pipeline",
"short_class": "Pipeline",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->",
"function": "Illuminate\\Pipeline\\{closure}",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http\\Middleware",
"short_class": "TransformsRequest",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->",
"function": "handle",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php",
"line": 31,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http\\Middleware",
"short_class": "ConvertEmptyStringsToNull",
"class": "Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull",
"type": "->",
"function": "handle",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Pipeline",
"short_class": "Pipeline",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->",
"function": "Illuminate\\Pipeline\\{closure}",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http\\Middleware",
"short_class": "TransformsRequest",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->",
"function": "handle",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php",
"line": 40,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http\\Middleware",
"short_class": "TrimStrings",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings",
"type": "->",
"function": "handle",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Pipeline",
"short_class": "Pipeline",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->",
"function": "Illuminate\\Pipeline\\{closure}",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
"line": 27,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http\\Middleware",
"short_class": "ValidatePostSize",
"class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
"type": "->",
"function": "handle",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Pipeline",
"short_class": "Pipeline",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->",
"function": "Illuminate\\Pipeline\\{closure}",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php",
"line": 86,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http\\Middleware",
"short_class": "PreventRequestsDuringMaintenance",
"class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance",
"type": "->",
"function": "handle",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Pipeline",
"short_class": "Pipeline",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->",
"function": "Illuminate\\Pipeline\\{closure}",
"file": "/project_path/vendor/fruitcake/laravel-cors/src/HandleCors.php",
"line": 52,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Fruitcake\\Cors",
"short_class": "HandleCors",
"class": "Fruitcake\\Cors\\HandleCors",
"type": "->",
"function": "handle",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Pipeline",
"short_class": "Pipeline",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->",
"function": "Illuminate\\Pipeline\\{closure}",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php",
"line": 39,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Http\\Middleware",
"short_class": "TrustProxies",
"class": "Illuminate\\Http\\Middleware\\TrustProxies",
"type": "->",
"function": "handle",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 167,
"args": [
[
"object",
"Illuminate\\Http\\Request"
],
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Pipeline",
"short_class": "Pipeline",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->",
"function": "Illuminate\\Pipeline\\{closure}",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 103,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Pipeline",
"short_class": "Pipeline",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->",
"function": "then",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 142,
"args": [
[
"object",
"Closure"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http",
"short_class": "Kernel",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->",
"function": "sendRequestThroughRouter",
"file": "/project_path/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 111,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "Illuminate\\Foundation\\Http",
"short_class": "Kernel",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->",
"function": "handle",
"file": "/project_path/public/index.php",
"line": 52,
"args": [
[
"object",
"Illuminate\\Http\\Request"
]
]
},
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "require_once",
"file": "/project_path/server.php",
"line": 21,
"args": [
[
"string",
"/project_path/public/index.php"
]
]
}
]
}
}