Handler.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace App\Exceptions;
use BadMethodCallException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Response;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Throwable
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render1($request, Throwable $exception)
{
// dump(1);
// dd($exception);
// 捕获异常 No query results for model [App\\Models\
// if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
// //捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
// return Response::json(
// [
// 'code' => 1,
// 'msg' => '该数据不存在!',
// 'data' => null,
// ]
// );
// }
// 捕获异常:路由不存在 No query results for model [App\\Models\
if ($exception instanceof NotFoundHttpException) {
//捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
return Response::json(
[
'code' => 1,
'msg' => '该路由不存在!',
'data' => null,
]
);
}
// 捕获异常 BadMethodCallException
if ($exception instanceof BadMethodCallException) {
//捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
return Response::json(
[
'code' => 1,
'msg' => $exception->getMessage(),
'data' => null,
]
);
}
// 捕获异常 FatalThrowableError
if ($exception instanceof FatalThrowableError) {
//捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
return Response::json(
[
'code' => 1,
'msg' => $exception->getMessage(),
'data' => null,
]
);
}
// 捕获异常 MethodNotAllowedHttpException
if ($exception instanceof MethodNotAllowedHttpException) {
//捕获路由方法不对的问题
return Response::json(
[
'code' => 1,
'msg' => $exception->getMessage(),
'data' => null,
]
);
}
// .. 继续补充其他异常
return parent::render($request, $exception);
}
}