Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thearyanahmed/617e8e2ef7df01244474305dca0ded7f to your computer and use it in GitHub Desktop.
Save thearyanahmed/617e8e2ef7df01244474305dca0ded7f to your computer and use it in GitHub Desktop.
//
// In the App\Exceptions\Handler.php class,update the `render` function as following
$exceptionCode = null;
if(config('app.env') !== 'production') {
$res = [
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'previous' => $exception->getPrevious(),
'stack_trace_string' => $exception->getTraceAsString(),
'exception_code' => $exception->getCode(),
];
}
if($exception instanceof ValidationException) {
$errors = $exception->errors();
$res['errors'] = [];
foreach($errors as $error) {
array_push($res['errors'],implode('',$error));
}
$exceptionCode = 406;
} else {
$exceptionCode = $exception->getCode();
}
$res['message'] = $exception->getMessage();
return response()->json($res,$exceptionCode);
// Works for `throw new $exception` and `form valdiations/requests`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment