Skip to content

Instantly share code, notes, and snippets.

@sajt
Last active June 11, 2020 07:12
Show Gist options
  • Save sajt/740c3704ef32dbcbba7110e15c7584c0 to your computer and use it in GitHub Desktop.
Save sajt/740c3704ef32dbcbba7110e15c7584c0 to your computer and use it in GitHub Desktop.
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function render($request, Exception $exception)
{
if (env('APP_DEBUG')) {
return parent::render($request, $exception);
}
$status = Response::HTTP_INTERNAL_SERVER_ERROR;
if ($exception instanceof HttpResponseException) {
$status = Response::HTTP_INTERNAL_SERVER_ERROR;
} elseif ($exception instanceof ModelNotFoundException) {
$status = 200; // Ez azrt 200, mert különben nem megy át a json.
} elseif ($exception instanceof MethodNotAllowedHttpException) {
$status = Response::HTTP_METHOD_NOT_ALLOWED;
$exception = new MethodNotAllowedHttpException([], 'HTTP_METHOD_NOT_ALLOWED', $exception);
} elseif ($exception instanceof NotFoundHttpException) {
$status = $exception->getStatusCode();
//$exception = new NotFoundHttpException('HTTP_NOT_FOUND', $exception);
} elseif ($exception instanceof AuthorizationException) {
$status = Response::HTTP_FORBIDDEN;
$exception = new AuthorizationException('HTTP_FORBIDDEN', $status);
} elseif ($exception instanceof \Dotenv\Exception\ValidationException && $exception->getResponse()) {
$status = Response::HTTP_BAD_REQUEST;
$exception = new \Dotenv\Exception\ValidationException('HTTP_BAD_REQUEST', $status, $exception);
} elseif ($exception) {
$status = 200;
// $exception = new HttpException($status, 'HTTP_INTERNAL_SERVER_ERROR');
}
return response()->json([
'success' => false,
'status' => $status,
'message' => $exception->getMessage()
], $status); // Itt mindig 200-at kell visszaadni
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment