Last active
December 1, 2022 03:20
-
-
Save imamnurhy/2c73c6cbdf37ef52a7fe4fda9c83c365 to your computer and use it in GitHub Desktop.
Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Exceptions; | |
| use App\Http\Controllers\Traits\ResponseDefaultTrait; | |
| use Carbon\Carbon; | |
| use Error; | |
| use ErrorException; | |
| use Exception; | |
| use Illuminate\Auth\Access\AuthorizationException; | |
| use Illuminate\Database\Eloquent\ModelNotFoundException; | |
| use Illuminate\Database\QueryException; | |
| use Illuminate\Http\Response; | |
| use Illuminate\Validation\ValidationException; | |
| use Laravel\Lumen\Exceptions\Handler as ExceptionHandler; | |
| use Symfony\Component\HttpKernel\Exception\HttpException; | |
| use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; | |
| use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
| use Throwable; | |
| class Handler extends ExceptionHandler | |
| { | |
| use ResponseDefaultTrait; | |
| /** | |
| * A list of the exception types that should not be reported. | |
| * | |
| * @var array | |
| */ | |
| protected $dontReport = [ | |
| AuthorizationException::class, | |
| HttpException::class, | |
| ModelNotFoundException::class, | |
| ValidationException::class, | |
| ]; | |
| /** | |
| * Report or log an exception. | |
| * | |
| * This is a great spot to send exceptions to Sentry, Bugsnag, etc. | |
| * | |
| * @param \Throwable $exception | |
| * @return void | |
| * | |
| * @throws \Exception | |
| */ | |
| public function report(Throwable $exception) | |
| { | |
| parent::report($exception); | |
| } | |
| /** | |
| * Render an exception into an HTTP response. | |
| * | |
| * @param \Illuminate\Http\Request $request | |
| * @param \Throwable $exception | |
| * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse | |
| * | |
| * @throws \Throwable | |
| */ | |
| public function render($request, Throwable $exception) | |
| { | |
| $message = $exception->getMessage(); | |
| if ($exception instanceof MethodNotAllowedHttpException) { | |
| return $this->responseDefault($message, Response::HTTP_METHOD_NOT_ALLOWED); | |
| } else if ($exception instanceof NotFoundHttpException) { | |
| return $this->responseDefault($message, Response::HTTP_NOT_FOUND); | |
| } else if ($exception instanceof ValidationException) { | |
| return $this->responseDefault([ | |
| 'message' => implode(',', $exception->validator->errors()->all()), | |
| 'errors' => $exception->validator->errors(), | |
| ], Response::HTTP_UNPROCESSABLE_ENTITY); | |
| } else if ($exception instanceof ErrorException) { | |
| return $this->responseDefault($message, Response::HTTP_INTERNAL_SERVER_ERROR); | |
| } else if ($exception instanceof QueryException) { | |
| return $this->responseDefault($message, Response::HTTP_INTERNAL_SERVER_ERROR); | |
| } else if ($exception instanceof Exception) { | |
| return $this->responseDefault($message, Response::HTTP_INTERNAL_SERVER_ERROR); | |
| } else if ($exception instanceof Error) { | |
| return $this->responseDefault($message, Response::HTTP_INTERNAL_SERVER_ERROR); | |
| } else { | |
| return parent::render($request, $exception); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers\Traits; | |
| use Carbon\Carbon; | |
| use Illuminate\Http\JsonResponse; | |
| use Illuminate\Http\Response; | |
| trait ResponseDefaultTrait | |
| { | |
| /** | |
| * @param mixed $content | |
| * @return \Illuminate\Http\JsonResponse | |
| */ | |
| function responseDefault($content, int $statusCode = Response::HTTP_OK): JsonResponse | |
| { | |
| $response = [ | |
| 'code' => $statusCode, | |
| 'status' => Response::$statusTexts[$statusCode], | |
| 'datetime' => Carbon::now(), | |
| 'url' => request()->url(), | |
| 'message' => '', | |
| 'body' => request()->all(), | |
| 'data' => [] | |
| ]; | |
| if (is_array($content)) $response = array_merge($response, $content); // Merge respon array | |
| if (is_string($content)) $response['message'] = $content; | |
| $response = array_filter($response); // Remove array when empty value | |
| return response()->json($response, $statusCode); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment