From 9224bcef5be1455da2af4d95f1adc70648a7d76c Mon Sep 17 00:00:00 2001 From: Muhammad Fajar Date: Mon, 31 Oct 2022 07:08:03 +0700 Subject: [PATCH] fix handler response --- app/Exceptions/Handler.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index cf283605..9dc538c1 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -7,6 +7,7 @@ use Illuminate\Auth\AuthenticationException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Validation\ValidationException; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; @@ -59,49 +60,54 @@ class Handler extends ExceptionHandler if ($exception instanceof AuthenticationException) { return response()->json([ 'status' => 'error', + 'statusCode' => Response::HTTP_UNAUTHORIZED, 'message' => 'Unauthenticated', 'errors' => [ 'Unauthenticated' ] - ], 401); + ], Response::HTTP_UNAUTHORIZED); } if ($exception instanceof AuthorizationException) { return response()->json([ 'status' => 'error', + 'statusCode' => Response::HTTP_FORBIDDEN, 'message' => 'This action is unauthorized.', 'errors' => [ 'This action is unauthorized.' ] - ], 403); + ], Response::HTTP_FORBIDDEN); } if ($exception instanceof NotFoundHttpException) { return response()->json([ 'status' => 'error', + 'statusCode' => Response::HTTP_NOT_FOUND, 'message' => 'Route Not Found', 'errors' => [ 'Route Not Found' ] - ], 404); + ], Response::HTTP_NOT_FOUND); } if ($exception instanceof ModelNotFoundException) { return response()->json([ 'status' => 'error', + 'statusCode' => Response::HTTP_NOT_FOUND, 'message' => 'Record for ' . str_replace('App', '', str_replace('\\Models\\', '', $exception->getModel())) . ' not found', 'errors' => [ 'Record for ' . str_replace('App', '', str_replace('\\Models\\', '', $exception->getModel())) . ' not found' ] - ], 404); + ], Response::HTTP_NOT_FOUND); } if ($exception instanceof ValidationException) { return response()->json([ 'status' => 'error', + 'statusCode' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'The given data was invalid.', 'errors' => collect($exception->errors())->flatten() - ], 422); + ], Response::HTTP_UNPROCESSABLE_ENTITY); } }