From 538ecc4deacb82cf48ac1479f2fa14334cd8f7e1 Mon Sep 17 00:00:00 2001 From: David Adi Nugroho Date: Mon, 11 Dec 2023 15:45:21 +0700 Subject: [PATCH] wip --- config/api-response.php | 17 ++++++++++++++- src/ApiExceptionHandler.php | 18 ++++++---------- src/ApiResponse.php | 16 +++++++------- src/ApiResponseContract.php | 12 ----------- src/Exceptions/ApiException.php | 4 ++-- src/Facades/ApiResponse.php | 34 +++++++++++++++--------------- tests/Features/ApiResponseTest.php | 2 +- 7 files changed, 50 insertions(+), 53 deletions(-) diff --git a/config/api-response.php b/config/api-response.php index 0f3e0bf..1034edc 100644 --- a/config/api-response.php +++ b/config/api-response.php @@ -1,5 +1,10 @@ \Illuminate\Http\Response::HTTP_INTERNAL_SERVER_ERROR, + 'error_status_code' => \Illuminate\Http\Response::HTTP_INTERNAL_SERVER_ERROR, + + /** + * List of exception status codes. + * Override the default status code with custom one. + */ + 'exception_status_codes' => [ + AuthenticationException::class => Response::HTTP_UNAUTHORIZED, + ModelNotFoundException::class => Response::HTTP_NOT_FOUND, + ValidationException::class => Response::HTTP_UNPROCESSABLE_ENTITY, + ], /** * Debugging options diff --git a/src/ApiExceptionHandler.php b/src/ApiExceptionHandler.php index 4440957..816aec1 100644 --- a/src/ApiExceptionHandler.php +++ b/src/ApiExceptionHandler.php @@ -2,9 +2,7 @@ namespace KodePandai\ApiResponse; -use Illuminate\Auth\AuthenticationException; use Illuminate\Contracts\Support\Responsable; -use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Http\Request; use Illuminate\Validation\ValidationException; @@ -18,12 +16,6 @@ class ApiExceptionHandler { - public static array $defaultStatusCodes = [ - AuthenticationException::class => Response::HTTP_UNAUTHORIZED, - ModelNotFoundException::class => Response::HTTP_NOT_FOUND, - ValidationException::class => Response::HTTP_UNPROCESSABLE_ENTITY, - ]; - /** * @return Response|JsonResponse */ @@ -80,8 +72,10 @@ protected function getTitle(Throwable $e, ?Request $request = null): string protected function getStatusCode(Throwable $e, ?Request $request = null): int { - if (isset(static::$defaultStatusCodes[get_class($e)])) { - return static::$defaultStatusCodes[get_class($e)]; + $codes = config('api-response.exception_status_codes', []); + + if (isset($codes[get_class($e)])) { + return $codes[get_class($e)]; } if ($e instanceof HttpExceptionInterface || method_exists($e, 'getStatusCode')) { @@ -97,14 +91,14 @@ protected function getStatusCode(Throwable $e, ?Request $request = null): int /** @var ValidationException $e as example */ return method_exists($e->getResponse(), 'getStatusCode') ? $e->getResponse()->getStatusCode() - : config('api-response.error_code'); + : config('api-response.error_status_code'); } if (! empty(@Response::$statusTexts[$e->getCode()])) { return $e->getCode(); } - return config('api-response.error_code'); + return config('api-response.error_status_code'); } protected function getMessage(Throwable $e, ?Request $request = null): string diff --git a/src/ApiResponse.php b/src/ApiResponse.php index 2a045ec..75b162d 100644 --- a/src/ApiResponse.php +++ b/src/ApiResponse.php @@ -66,7 +66,7 @@ public function error(mixed $errors = [], ?int $statusCode = null): static ->title(__('api-response::trans.error')) ->message(__('api-response::trans.something_went_wrong')) ->errors($errors) - ->statusCode($statusCode ?: config('api-response.error_code')); + ->statusCode($statusCode ?: config('api-response.error_status_code')); } public function notFound(mixed $errors = []): static @@ -131,12 +131,12 @@ public function notSuccessful(): static return $this->setIsSuccess(false); } - public function getTitle(): string + protected function getTitle(): string { return $this->title; } - public function setTitle(string $title): static + protected function setTitle(string $title): static { $this->title = $title; @@ -148,12 +148,12 @@ public function title(string $title): static return $this->setTitle($title); } - public function getMessage(): string + protected function getMessage(): string { return $this->message; } - public function setMessage(string $message): static + protected function setMessage(string $message): static { $this->message = $message; @@ -165,12 +165,12 @@ public function message(string $message): static return $this->setMessage($message); } - public function getErrors(): array + protected function getErrors(): array { return $this->errors; } - public function setErrors(array $errors): static + protected function setErrors(array $errors): static { $this->errors = $errors; @@ -182,7 +182,7 @@ public function errors(array $errors): static return $this->setErrors($errors); } - public function synchronizeData(): static + protected function synchronizeData(): static { return parent::setData([ 'success' => $this->getIsSuccess(), diff --git a/src/ApiResponseContract.php b/src/ApiResponseContract.php index f74bcf1..0c2143f 100644 --- a/src/ApiResponseContract.php +++ b/src/ApiResponseContract.php @@ -43,22 +43,10 @@ public function successful(): static; public function notSuccessful(): static; - public function getTitle(): string; - - public function setTitle(string $title): static; - public function title(string $title): static; - public function getMessage(): string; - - public function setMessage(string $message): static; - public function message(string $message): static; - public function getErrors(): array; - - public function setErrors(array $errors): static; - public function errors(array $errors): static; public function getOriginalData(): mixed; diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/ApiException.php index ea6e02a..c847615 100644 --- a/src/Exceptions/ApiException.php +++ b/src/Exceptions/ApiException.php @@ -13,7 +13,7 @@ class ApiException extends Exception implements Responsable public function __construct(string $message = '', string $title = '', ?int $statusCode = null) { - $defaultCode = config('api-response.error_code', 500); + $defaultCode = config('api-response.error_status_code', 500); /** @var ApiResponse $response */ $response = app('api-response'); @@ -72,7 +72,7 @@ public function statusCode(int $statusCode): static public static function error(string $message = '', string $title = '', ?int $statusCode = null): self { - return new self($message, $title, $statusCode ?: config('api-response.error_code')); + return new self($message, $title, $statusCode ?: config('api-response.error_status_code')); } public static function notFound(string $message = '', string $title = ''): self diff --git a/src/Facades/ApiResponse.php b/src/Facades/ApiResponse.php index 5980398..384d732 100644 --- a/src/Facades/ApiResponse.php +++ b/src/Facades/ApiResponse.php @@ -5,23 +5,23 @@ use Illuminate\Support\Facades\Facade; /** - * @method static ApiResponse create(mixed $data = []) - * @method static ApiResponse success(mixed $data = []) - * @method static ApiResponse error(mixed $errors = [], int $statusCode = null) - * @method static ApiResponse notFound(mixed $errors = []) - * @method static ApiResponse unprocessable(mixed $errors = []) - * @method static ApiResponse unauthorized(mixed $errors = []) - * @method static ApiResponse forbidden(mixed $errors = []) - * @method static ApiResponse badRequest(mixed $errors = []) - * @method ApiResponse statusCode(int $code) - * @method ApiResponse successful() - * @method ApiResponse notSuccessful() - * @method ApiResponse title(string $title) - * @method ApiResponse message(string $message) - * @method ApiResponse errors(array $errors) - * @method ApiResponse data(mixed $data) - * @method ApiResponse addHeader(string $key, string $value) - * @method ApiResponse addHeaders(array $headers) + * @method static \KodePandai\ApiResponse\ApiResponse create(mixed $data = []) + * @method static \KodePandai\ApiResponse\ApiResponse success(mixed $data = []) + * @method static \KodePandai\ApiResponse\ApiResponse error(mixed $errors = [], int $statusCode = null) + * @method static \KodePandai\ApiResponse\ApiResponse notFound(mixed $errors = []) + * @method static \KodePandai\ApiResponse\ApiResponse unprocessable(mixed $errors = []) + * @method static \KodePandai\ApiResponse\ApiResponse unauthorized(mixed $errors = []) + * @method static \KodePandai\ApiResponse\ApiResponse forbidden(mixed $errors = []) + * @method static \KodePandai\ApiResponse\ApiResponse badRequest(mixed $errors = []) + * @method \KodePandai\ApiResponse\ApiResponse statusCode(int $code) + * @method \KodePandai\ApiResponse\ApiResponse successful() + * @method \KodePandai\ApiResponse\ApiResponse notSuccessful() + * @method \KodePandai\ApiResponse\ApiResponse title(string $title) + * @method \KodePandai\ApiResponse\ApiResponse message(string $message) + * @method \KodePandai\ApiResponse\ApiResponse errors(array $errors) + * @method \KodePandai\ApiResponse\ApiResponse data(mixed $data) + * @method \KodePandai\ApiResponse\ApiResponse addHeader(string $key, string $value) + * @method \KodePandai\ApiResponse\ApiResponse addHeaders(array $headers) * * @see \KodePandai\ApiResponse\ApiResponse * @see \KodePandai\ApiResponse\ApiResponseContract diff --git a/tests/Features/ApiResponseTest.php b/tests/Features/ApiResponseTest.php index ee32a77..a6c0630 100644 --- a/tests/Features/ApiResponseTest.php +++ b/tests/Features/ApiResponseTest.php @@ -52,7 +52,7 @@ ->assertHeader('content-type', 'application/json'); getJson('api-error') - ->assertStatus(config('api-response.error_code')) + ->assertStatus(config('api-response.error_status_code')) ->assertHeader('content-type', 'application/json'); getJson('api-puck')