Commit eb0fbfa6 authored by Viral Solani's avatar Viral Solani

- Add common error handeling function

- Refactoring
parent 5210f03f
...@@ -74,49 +74,29 @@ class Handler extends ExceptionHandler ...@@ -74,49 +74,29 @@ class Handler extends ExceptionHandler
if($exception instanceof MethodNotAllowedHttpException) if($exception instanceof MethodNotAllowedHttpException)
{ {
return response()->json((object) [ return $this->setStatusCode(403)->respondWithError('Please check HTTP Request Method. - MethodNotAllowedHttpException');
'status' => false,
'errorCode' => 'METHOD_NOT_ALLOWED',
'message' => 'Please check HTTP Request Method. - MethodNotAllowedHttpException'
], 403);
} }
if($exception instanceof NotFoundHttpException) if($exception instanceof NotFoundHttpException)
{ {
return response()->json((object) [ return $this->setStatusCode(403)->respondWithError('Please check your URL to make sure request is formatted properly. - NotFoundHttpException');
'status' => false,
'errorCode' => 'URL_NOT_FOUND',
'message' => 'Please check your URL to make sure request is formatted properly. - NotFoundHttpException'
], 403);
} }
if($exception instanceof GeneralException) if($exception instanceof GeneralException)
{ {
return response()->json((object) [ return $this->setStatusCode(403)->respondWithError($exception->getMessage());
'status' => false,
'errorCode' => 'EXCEPTION',
'message' => $exception->getMessage()
], 403);
} }
if($exception instanceof ModelNotFoundException) if($exception instanceof ModelNotFoundException)
{ {
return response()->json((object) [ return $this->setStatusCode(403)->respondWithError('Item could not be found. Please check identifier.');
'status' => false,
'errorCode' => 'ITEM_NOT_FOUND',
'message' => 'Item could not be found. Please check identifier.'
], 403);
} }
if($exception instanceof ValidationException) if($exception instanceof ValidationException)
{ {
\Log::debug("API Validation Exception - " . json_encode($exception->validator->messages())); \Log::debug("API Validation Exception - " . json_encode($exception->validator->messages()));
return response()->json((object) [ return $this->setStatusCode(422)->respondWithError($exception->validator->messages());
'status' => false,
'errorCode' => 'VALIDATION_EXCEPTION',
'messages' => $exception->validator->messages()
], 403);
} }
} }
...@@ -139,4 +119,58 @@ class Handler extends ExceptionHandler ...@@ -139,4 +119,58 @@ class Handler extends ExceptionHandler
return redirect()->guest(route('frontend.auth.login')); return redirect()->guest(route('frontend.auth.login'));
} }
/**
* get the status code.
*
* @return statuscode
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* set the status code.
*
* @param [type] $statusCode [description]
*
* @return statuscode
*/
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
/**
* respond with error.
*
* @param $message
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithError($message)
{
return $this->respond([
'error' => [
'message' => $message,
'status_code' => $this->getStatusCode(),
],
]);
}
/**
* Respond.
*
* @param array $data
* @param array $headers
*
* @return \Illuminate\Http\JsonResponse
*/
public function respond($data, $headers = [])
{
return response()->json($data, $this->getStatusCode(), $headers);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment