|
@@ -49,21 +49,23 @@ class Handler extends ExceptionHandler
|
49
|
49
|
});
|
50
|
50
|
|
51
|
51
|
$this->renderable(function (Throwable $e, $request) {
|
52
|
|
- return $this->handleException($request, $e);
|
|
52
|
+ if ($request->is('api/*')) {
|
|
53
|
+ return $this->handleApiException($e);
|
|
54
|
+ }
|
53
|
55
|
});
|
54
|
56
|
}
|
55
|
57
|
|
56
|
|
- public function handleException($request, Throwable $exception)
|
|
58
|
+ public function handleApiException(Throwable $exception)
|
57
|
59
|
{
|
58
|
60
|
// Route Not Found
|
59
|
61
|
if ($exception instanceof RouteNotFoundException) {
|
60
|
|
- return $this->exceptionResponse($request, $exception, Response::HTTP_NOT_FOUND, config('response-message.router_failed'));
|
|
62
|
+ return $this->apiExceptionResponse($exception, Response::HTTP_NOT_FOUND, config('response-message.router_failed'));
|
61
|
63
|
}
|
62
|
64
|
|
63
|
65
|
// Not Found
|
64
|
66
|
if ($exception instanceof NotFoundHttpException) {
|
65
|
|
- $message = $exception->getMessage() ?? config('response-message.not_found');
|
66
|
|
- return $this->exceptionResponse($request, $exception, Response::HTTP_NOT_FOUND, $message);
|
|
67
|
+ $message = $exception->getMessage() ?: config('response-message.not_found');
|
|
68
|
+ return $this->apiExceptionResponse($exception, Response::HTTP_NOT_FOUND, $message);
|
67
|
69
|
}
|
68
|
70
|
|
69
|
71
|
// Model Not Found
|
|
@@ -73,67 +75,60 @@ class Handler extends ExceptionHandler
|
73
|
75
|
|
74
|
76
|
// Authentication
|
75
|
77
|
if ($exception instanceof AuthenticationException) {
|
76
|
|
- return $this->exceptionResponse($request, $exception, Response::HTTP_UNAUTHORIZED, config('response-message.authenticate_failed'));
|
|
78
|
+ return $this->apiExceptionResponse($exception, Response::HTTP_UNAUTHORIZED, config('response-message.authenticate_failed'));
|
77
|
79
|
}
|
78
|
80
|
|
79
|
81
|
// Unauthorized
|
80
|
82
|
if ($exception instanceof UnauthorizedHttpException) {
|
81
|
|
- return $this->exceptionResponse($request, $exception, $exception->getStatusCode(), config('response-message.authorize_failed'));
|
|
83
|
+ return $this->apiExceptionResponse($exception, $exception->getStatusCode(), config('response-message.authorize_failed'));
|
82
|
84
|
}
|
83
|
85
|
|
84
|
86
|
// Http
|
85
|
87
|
if ($exception instanceof HttpException) {
|
86
|
|
- return $this->exceptionResponse($request, $exception, $exception->getStatusCode(), $exception->getMessage());
|
|
88
|
+ return $this->apiExceptionResponse($exception, $exception->getStatusCode(), $exception->getMessage());
|
87
|
89
|
}
|
88
|
90
|
|
89
|
91
|
// Query
|
90
|
92
|
if ($exception instanceof QueryException) {
|
91
|
|
- $this->exceptionResponse($request, $exception, Response::HTTP_INTERNAL_SERVER_ERROR, config('response-message.sql_error'));
|
|
93
|
+ $this->apiExceptionResponse($exception, Response::HTTP_INTERNAL_SERVER_ERROR, config('response-message.sql_error'));
|
92
|
94
|
}
|
93
|
95
|
|
94
|
|
- // Validation
|
|
96
|
+ // Validation 422
|
95
|
97
|
if ($exception instanceof ValidationException) {
|
96
|
98
|
|
97
|
|
- if (!$request->is('api/*')) {
|
98
|
|
- return $this->convertValidationExceptionToResponse($exception, $request);
|
99
|
|
- }
|
100
|
|
-
|
101
|
99
|
$validationData = [];
|
102
|
|
- foreach ($exception->errors() as $key => $item) {
|
103
|
|
- $validationData['errors']['validation'][$key] = $item[0];
|
|
100
|
+ foreach ($exception->errors() as $col => $errorMsg) {
|
|
101
|
+ $validationData['errors']['validation'][$col] = $errorMsg[0];
|
104
|
102
|
}
|
105
|
|
- return $this->exceptionResponse($request, $exception, Response::HTTP_UNPROCESSABLE_ENTITY, config('response-message.validation_failed'), $validationData);
|
|
103
|
+ return $this->apiExceptionResponse($exception, Response::HTTP_UNPROCESSABLE_ENTITY, config('response-message.validation_failed'), $validationData);
|
106
|
104
|
}
|
107
|
105
|
|
108
|
|
- // Others
|
109
|
|
- return $this->exceptionResponse($request, $exception, Response::HTTP_INTERNAL_SERVER_ERROR, 'server error');
|
|
106
|
+ // Others: 500 error
|
|
107
|
+ return $this->apiExceptionResponse($exception, Response::HTTP_INTERNAL_SERVER_ERROR, 'server error');
|
110
|
108
|
}
|
111
|
109
|
|
112
|
|
- public function exceptionResponse($request, $exception, $code, $message, $mergeData = [])
|
|
110
|
+ public function apiExceptionResponse(Throwable $exception, int $code, string $message, array $mergeData = [])
|
113
|
111
|
{
|
114
|
112
|
if ($code === Response::HTTP_INTERNAL_SERVER_ERROR) {
|
115
|
113
|
Log::info($exception->getMessage());
|
116
|
114
|
}
|
117
|
115
|
|
|
116
|
+ $data = [
|
|
117
|
+ 'code' => $code,
|
|
118
|
+ 'message' => $message,
|
|
119
|
+ ];
|
118
|
120
|
|
119
|
|
- if ($request->is('api/*')) {
|
120
|
|
- $data = [
|
121
|
|
- 'code' => $code,
|
122
|
|
- 'message' => $message,
|
|
121
|
+ if (!app()->environment('production')) {
|
|
122
|
+ $data['errors'] = [
|
|
123
|
+ 'message' => $exception->getMessage(),
|
|
124
|
+ 'trace' => $exception->getTrace(),
|
123
|
125
|
];
|
|
126
|
+ }
|
124
|
127
|
|
125
|
|
- if (!app()->environment('production')) {
|
126
|
|
- $data['errors'] = [
|
127
|
|
- 'message' => $exception->getMessage(),
|
128
|
|
- 'trace' => $exception->getTrace(),
|
129
|
|
- ];
|
130
|
|
- }
|
131
|
|
-
|
132
|
|
- if (!empty($mergeData)) {
|
133
|
|
- $data = array_merge($data, $mergeData);
|
134
|
|
- }
|
135
|
|
-
|
136
|
|
- return response()->json($data, $code);
|
|
128
|
+ if (!empty($mergeData)) {
|
|
129
|
+ $data = array_merge($data, $mergeData);
|
137
|
130
|
}
|
|
131
|
+
|
|
132
|
+ return response()->json($data, $code);
|
138
|
133
|
}
|
139
|
134
|
}
|