Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lakuapik committed Dec 11, 2023
1 parent ee10a45 commit 538ecc4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 53 deletions.
17 changes: 16 additions & 1 deletion config/api-response.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;

return [

/**
Expand All @@ -13,7 +18,17 @@
* - return ApiResponse::error()..
* - throw new ApiException('error')..
*/
'error_code' => \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
Expand Down
18 changes: 6 additions & 12 deletions src/ApiExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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')) {
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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(),
Expand Down
12 changes: 0 additions & 12 deletions src/ApiResponseContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions src/Facades/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Features/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 538ecc4

Please sign in to comment.