Refactor how errors are handled

This commit is contained in:
Nabeel Shahzad 2018-02-22 10:14:12 -06:00
parent 5d2ef3a9d3
commit 487b7480af
2 changed files with 53 additions and 31 deletions

View File

@ -8,6 +8,8 @@ use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Log; use Log;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
{ {
@ -37,66 +39,84 @@ class Handler extends ExceptionHandler
parent::report($exception); parent::report($exception);
} }
/**
* Create an error message
* @param $status_code
* @param $message
* @return array
*/
protected function createError($status_code, $message)
{
return [
'error' => [
'status' => $status_code,
'message' => $message,
]
];
}
/** /**
* Render an exception into an HTTP response. * Render an exception into an HTTP response.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \Exception $exception * @param \Exception $exception
* @return \Illuminate\Http\Response * @return mixed
*/ */
public function render($request, Exception $exception) public function render($request, Exception $exception)
{ {
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException
&& $exception->getStatusCode() == 403) {
return redirect()->guest('login');
}
if($request->is('api/*')) { if($request->is('api/*')) {
$error = [ $headers = [];
'error' => [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
]
];
$status = 400; Log::error('API Error', $exception->getTrace());
$http_code = $exception->getCode();
Log::error($exception->getMessage()); if($exception instanceof ModelNotFoundException ||
$exception instanceof NotFoundHttpException) {
if ($this->isHttpException($exception)) { $error = $this->createError(404, $exception->getMessage());
$status = $exception->getStatusCode();
$http_code = $exception->getStatusCode();
} }
if($exception instanceof ModelNotFoundException) { # These are application errors. Custom exceptions should
$status = 404; # be extending HttpException
$http_code = 404; elseif ($exception instanceof HttpException) {
$error = $this->createError(
$exception->getStatusCode(),
$exception->getMessage()
);
$headers = $exception->getHeaders();
} }
if($exception instanceof ValidationException) { # Create the detailed errors from the validation errors
$status = 400; elseif($exception instanceof ValidationException) {
$http_code = 400;
$errors = $exception->errors();
$error_messages = []; $error_messages = [];
$errors = $exception->errors();
foreach($errors as $field => $error) { foreach($errors as $field => $error) {
$error_messages[] = implode(', ', $error); $error_messages[] = implode(', ', $error);
} }
$error['error']['message'] = implode(', ', $error_messages); $message = implode(', ', $error_messages);
$error = $this->createError(400, $message);
$error['error']['errors'] = $errors; $error['error']['errors'] = $errors;
Log::error('Validation errors', $errors); Log::error('Validation errors', $errors);
} }
else {
$error = $this->createError(400, $exception->getMessage());
}
# Only add trace if in dev # Only add trace if in dev
if(config('app.env') === 'dev') { if(config('app.env') === 'dev') {
$error['error']['trace'] = $exception->getTrace()[0]; $error['error']['trace'] = $exception->getTrace()[0];
} }
$error['error']['http_code'] = $http_code; return response()->json($error, $error['error']['status'], $headers);
return response()->json($error, $status); }
if ($exception instanceof HttpException
&& $exception->getStatusCode() === 403) {
return redirect()->guest('login');
} }
return parent::render($request, $exception); return parent::render($request, $exception);

View File

@ -5,7 +5,9 @@
namespace App\Exceptions; namespace App\Exceptions;
class SettingNotFound extends \Exception use Symfony\Component\HttpKernel\Exception\HttpException;
class SettingNotFound extends HttpException
{ {
} }