Skip to content

Commit 12d762f

Browse files
authored
Merge pull request #7 from manojSRawat/error-handling
Enhance error handling
2 parents 9600c4d + c810efa commit 12d762f

File tree

1 file changed

+160
-94
lines changed

1 file changed

+160
-94
lines changed

src/Exceptions/Handler.php

Lines changed: 160 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class Handler extends ExceptionHandler
1616
{
17+
protected $errorResponse;
18+
protected $statusCode;
1719
/**
1820
* A list of the exception types that should not be reported.
1921
*
@@ -45,126 +47,49 @@ public function report(\Throwable $exception)
4547
/**
4648
* Render an exception into an HTTP response.
4749
*
48-
* @param \Illuminate\Http\Request $request
50+
* @param \Illuminate\Http\Request $request
4951
* @param \Throwable $exception
5052
* @return \Illuminate\Http\Response
5153
*/
5254
public function render($request, \Throwable $exception)
5355
{
54-
$errorResponse = null;
55-
$statusCode = 500;
56-
5756
if ($exception instanceof AppException) {
58-
$errorResponse = [
59-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Internal Server Error",
60-
'type' => ErrorConstants::TYPE_INTERNAL_SERVER_ERROR,
61-
'errorDetails' => $exception->getTrace()
62-
];
63-
$statusCode = $exception->getCode();
57+
$this->appExceptionHandler($exception);
6458
} else if ($exception instanceof ValidationException) {
65-
$errorDetails = [];
66-
$error = $exception->getMessage();
67-
if (UtilityService::is_json($exception->getMessage())) {
68-
$err = json_decode($exception->getMessage());
69-
$error = $err->error;
70-
$errorDetails = $err->errorDetails;
71-
}
72-
$errorResponse = [
73-
'status' => 'fail',
74-
'message' => array_merge([$error], $errorDetails),
75-
'data' => null,
76-
'type' => ErrorConstants::TYPE_VALIDATION_ERROR
77-
];
78-
$statusCode = 400;
59+
$this->validationExceptionHandler($exception);
7960
} else if ($exception instanceof NotFoundHttpException) {
80-
$errorResponse = [
81-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Resouce not found",
82-
'type' => ErrorConstants::TYPE_RESOURCE_NOT_FOUND_ERROR,
83-
'errorDetails' => "Resource not found"
84-
];
85-
$statusCode = 404;
61+
$this->notFoundHttpExceptionHandler($exception);
8662
} else if ($exception instanceof MethodNotAllowedHttpException) {
87-
$errorResponse = [
88-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Method not allowed",
89-
'type' => ErrorConstants::TYPE_METHOD_NOT_ALLOWED_ERROR,
90-
'errorDetails' => "Method not allowed"
91-
];
92-
$statusCode = 405;
63+
$this->methodNotAllowedHttpExceptionHandler($exception);
9364
} else if ($exception instanceof InvalidCredentialsException) {
94-
$errorResponse = [
95-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Invalid Credentials",
96-
'type' => ErrorConstants::TYPE_INVALID_CREDENTIALS_ERROR,
97-
'errorDetails' => $exception->getMessage()
98-
];
99-
$statusCode = 401;
65+
$this->invalidCredentialsExceptionHandler($exception);
10066
} else if ($exception instanceof AuthorizationException) {
101-
$errorResponse = [
102-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Authorization Failed",
103-
'type' => ErrorConstants::TYPE_AUTHORIZATION_ERROR,
104-
'errorDetails' => $exception->getMessage()
105-
];
106-
$statusCode = 403;
67+
$this->authorizationExceptionHandler($exception);
10768
} else if ($exception instanceof ForbiddenException) {
108-
$errorResponse = [
109-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Forbidden from performing action",
110-
'type' => ErrorConstants::TYPE_FORBIDDEN_ERROR,
111-
'errorDetails' => $exception->getMessage()
112-
];
113-
$statusCode = 403;
69+
$this->forbiddenExceptionHandler($exception);
11470
} else if ($exception instanceof ServiceNotImplementedException) {
115-
$errorResponse = [
116-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Service Not Implemented",
117-
'type' => ErrorConstants::TYPE_SERVICE_NOT_IMPLEMENTED_ERROR,
118-
'errorDetails' => $exception->getMessage()
119-
];
120-
$statusCode = 501;
71+
$this->serviceNotImplementedExceptionHandler($exception);
12172
} else if ($exception instanceof BusinessLogicException) {
122-
$message = $exception->getMessage();
123-
if (UtilityService::is_json($exception->getMessage())) {
124-
$message = json_decode($exception->getMessage());
125-
};
126-
$errorResponse = [
127-
'error' => !empty($message) ? $message : "Business Logic Error",
128-
'errorDetails' => $exception->getMessage(),
129-
'type' => ErrorConstants::TYPE_BUSINESS_LOGIC_ERROR
130-
];
131-
$statusCode = $exception->getCode();
73+
$this->businessLogicExceptionHandler($exception);
13274
} else if ($exception instanceof BadRequestHttpException) {
133-
$errorResponse = [
134-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Bad Request",
135-
'type' => ErrorConstants::TYPE_BAD_REQUEST_ERROR,
136-
'errorDetails' => "Bad Request"
137-
];
138-
$statusCode = 400;
75+
$this->badRequestHttpExceptionHandler($exception);
13976
} else if ($exception instanceof ThrottleRequestsException) {
140-
$errorResponse = [
141-
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Bad Request",
142-
'type' => ErrorConstants::TYPE_TOO_MANY_REQUEST_ERROR,
143-
'errorDetails' => "Too many requests"
144-
];
145-
$statusCode = 429;
77+
$this->throttleRequestsExceptionHandler($exception);
14678
} else {
147-
$errorResponse = [
148-
'error' => $exception->getMessage(),
149-
'type' => ErrorConstants::TYPE_INTERNAL_SERVER_ERROR,
150-
'errorDetails' => $exception->getTrace()
151-
];
152-
$statusCode = 500;
79+
$this->exceptionHandler($exception);
15380
}
15481

155-
if (!env('APP_DEBUG')) {
156-
unset($errorResponse['errorDetails']);
157-
}
82+
$this->handleLogTrace();
15883

159-
return response()->json($errorResponse, $statusCode);
84+
return response()->json($this->errorResponse, $this->statusCode);
16085
}
16186

16287

16388
/**
16489
* Convert an authentication exception into an unauthenticated response.
16590
*
166-
* @param \Illuminate\Http\Request $request
167-
* @param \Illuminate\Auth\AuthenticationException $exception
91+
* @param \Illuminate\Http\Request $request
92+
* @param \Illuminate\Auth\AuthenticationException $exception
16893
* @return \Illuminate\Http\Response
16994
*/
17095
protected function unauthenticated($request, AuthenticationException $exception)
@@ -175,4 +100,145 @@ protected function unauthenticated($request, AuthenticationException $exception)
175100

176101
return redirect()->guest(route('login'));
177102
}
103+
104+
public function appExceptionHandler($exception)
105+
{
106+
$this->statusCode = $exception->getCode();
107+
$this->errorResponse = [
108+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Internal Server Error",
109+
'type' => ErrorConstants::TYPE_INTERNAL_SERVER_ERROR,
110+
'errorDetails' => $exception->getTrace()
111+
];
112+
}
113+
114+
public function validationExceptionHandler($exception)
115+
{
116+
$errorDetails = [];
117+
$error = $exception->getMessage();
118+
if (UtilityService::is_json($exception->getMessage())) {
119+
$err = json_decode($exception->getMessage());
120+
$error = $err->error;
121+
$errorDetails = $err->errorDetails;
122+
}
123+
124+
$this->statusCode = 400;
125+
$this->errorResponse = [
126+
'status' => 'fail',
127+
'message' => array_merge([$error], $errorDetails),
128+
'data' => null,
129+
'type' => ErrorConstants::TYPE_VALIDATION_ERROR
130+
];
131+
}
132+
133+
public function notFoundHttpExceptionHandler($exception)
134+
{
135+
$this->statusCode = 404;
136+
$this->errorResponse = [
137+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Resouce not found",
138+
'type' => ErrorConstants::TYPE_RESOURCE_NOT_FOUND_ERROR,
139+
'errorDetails' => "Resource not found"
140+
];
141+
}
142+
143+
public function methodNotAllowedHttpExceptionHandler($exception)
144+
{
145+
$this->statusCode = 405;
146+
$this->errorResponse = [
147+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Method not allowed",
148+
'type' => ErrorConstants::TYPE_METHOD_NOT_ALLOWED_ERROR,
149+
'errorDetails' => "Method not allowed"
150+
];
151+
}
152+
153+
public function invalidCredentialsExceptionHandler($exception)
154+
{
155+
$this->statusCode = 401;
156+
$this->errorResponse = [
157+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Invalid Credentials",
158+
'type' => ErrorConstants::TYPE_INVALID_CREDENTIALS_ERROR,
159+
'errorDetails' => $exception->getMessage()
160+
];
161+
}
162+
163+
public function authorizationExceptionHandler($exception)
164+
{
165+
$this->statusCode = 403;
166+
$this->errorResponse = [
167+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Authorization Failed",
168+
'type' => ErrorConstants::TYPE_AUTHORIZATION_ERROR,
169+
'errorDetails' => $exception->getMessage()
170+
];
171+
}
172+
173+
public function forbiddenExceptionHandler($exception)
174+
{
175+
$this->statusCode = 403;
176+
$this->errorResponse = [
177+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Forbidden from performing action",
178+
'type' => ErrorConstants::TYPE_FORBIDDEN_ERROR,
179+
'errorDetails' => $exception->getMessage()
180+
];
181+
}
182+
183+
public function serviceNotImplementedExceptionHandler($exception)
184+
{
185+
$this->statusCode = 501;
186+
$this->errorResponse = [
187+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Service Not Implemented",
188+
'type' => ErrorConstants::TYPE_SERVICE_NOT_IMPLEMENTED_ERROR,
189+
'errorDetails' => $exception->getMessage()
190+
];
191+
}
192+
193+
public function businessLogicExceptionHandler($exception)
194+
{
195+
$message = $exception->getMessage();
196+
if (UtilityService::is_json($exception->getMessage())) {
197+
$message = json_decode($exception->getMessage());
198+
}
199+
200+
$this->statusCode = $exception->getCode();
201+
$this->errorResponse = [
202+
'error' => !empty($message) ? $message : "Business Logic Error",
203+
'errorDetails' => $exception->getMessage(),
204+
'type' => ErrorConstants::TYPE_BUSINESS_LOGIC_ERROR
205+
];
206+
}
207+
208+
public function badRequestHttpExceptionHandler($exception)
209+
{
210+
$this->statusCode = 400;
211+
$this->errorResponse = [
212+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Bad Request",
213+
'type' => ErrorConstants::TYPE_BAD_REQUEST_ERROR,
214+
'errorDetails' => "Bad Request"
215+
];
216+
}
217+
218+
public function throttleRequestsExceptionHandler($exception)
219+
{
220+
$this->statusCode = 429;
221+
$this->errorResponse = [
222+
'error' => !empty($exception->getMessage()) ? $exception->getMessage() : "Bad Request",
223+
'type' => ErrorConstants::TYPE_TOO_MANY_REQUEST_ERROR,
224+
'errorDetails' => "Too many requests"
225+
];
226+
}
227+
228+
public function exceptionHandler($exception)
229+
{
230+
$this->statusCode = 500;
231+
$this->errorResponse = [
232+
'error' => $exception->getMessage(),
233+
'type' => ErrorConstants::TYPE_INTERNAL_SERVER_ERROR,
234+
'errorDetails' => $exception->getTrace()
235+
];
236+
}
237+
238+
public function handleLogTrace()
239+
{
240+
if (!env('APP_DEBUG')) {
241+
unset($this->errorResponse['errorDetails']);
242+
}
243+
}
178244
}

0 commit comments

Comments
 (0)