Skip to content

Commit

Permalink
feat: improve exception status code mapping
Browse files Browse the repository at this point in the history
The changes update the exception status code mapping to use a static method for registration and retrieval, improving code readability and maintainability.
  • Loading branch information
vaened committed Oct 6, 2024
1 parent b0eb8f3 commit 3aa5d18
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 47 deletions.
12 changes: 0 additions & 12 deletions config/laravception.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,4 @@
Vaened\Laravception\Handlers\ValidationExceptionHandler::class,
Vaened\Laravception\Handlers\ThrowableHandler::class,
],

/*
|--------------------------------------------------------------------------
| HTTP Exception Status Code Mapping
|--------------------------------------------------------------------------
|
| This option allows you to specify the class responsible for mapping
| exceptions to HTTP status codes. The default class provided handles
| common exception types, but you can customize it to suit your needs.
|
*/
'code_mapper' => Vaened\Laravception\HttpExceptionStatusCodeMapping::class,
];
15 changes: 0 additions & 15 deletions src/ExceptionStatusCodeMapping.php

This file was deleted.

11 changes: 5 additions & 6 deletions src/Handlers/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
use Illuminate\Routing\UrlGenerator;
use Throwable;
use Vaened\Laravception\Decoders\ExceptionNameParser;
use Vaened\Laravception\ExceptionStatusCodeMapping;
use Vaened\Laravception\HttpExceptionStatusCodeMapping;
use Vaened\Laravception\Responses\ErrorResponse;
use Vaened\Laravception\Responses\ErrorResponseFactory;

abstract class ErrorHandler
{
public function __construct(
private readonly ExceptionStatusCodeMapping $exceptionHandler,
private readonly UrlGenerator $url,
private readonly ExceptionNameParser $nameParser,
private readonly ErrorResponseFactory $responseFactory,
private readonly UrlGenerator $url,
private readonly ExceptionNameParser $nameParser,
private readonly ErrorResponseFactory $responseFactory,
)
{
}
Expand All @@ -41,7 +40,7 @@ final protected function createJsonResponse(Throwable $throwable, array $metadat
{
return response()->json(
$this->transformToApplicationResponse($throwable, $metadata)->serialize(),
$this->exceptionHandler->statusCodeFor($throwable)
HttpExceptionStatusCodeMapping::statusCodeFor($throwable)
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/HttpExceptionStatusCodeMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
use Symfony\Component\HttpFoundation\Response;
use Throwable;

final class HttpExceptionStatusCodeMapping implements ExceptionStatusCodeMapping
final class HttpExceptionStatusCodeMapping
{
private const DEFAULT_STATUS_CODE = Response::HTTP_INTERNAL_SERVER_ERROR;

private array $exceptions = [
private static array $exceptions = [
InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
ValidationException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
];

public function register(string $exceptionClass, int $statusCode): void
public static function register(string $exceptionClass, int $statusCode): void
{
$this->exceptions[$exceptionClass] = $statusCode;
self::$exceptions[$exceptionClass] = $statusCode;
}

public function statusCodeFor(Throwable $throwable): int
public static function statusCodeFor(Throwable $throwable): int
{
return $this->exceptions[$throwable::class] ?? self::DEFAULT_STATUS_CODE;
return self::$exceptions[$throwable::class] ?? self::DEFAULT_STATUS_CODE;
}
}
8 changes: 0 additions & 8 deletions src/LaravceptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ public function register(): void
static fn() => new LaravceptionConfig(config('laravception'))
);

$this->app->singleton(
ExceptionStatusCodeMapping::class,
static function (Application $application) {
$config = $application->make(LaravceptionConfig::class);
return $application->make($config->codeMapper());
}
);

$this->app->singleton(
ExceptionNameParser::class,
static function (Application $application) {
Expand Down

0 comments on commit 3aa5d18

Please sign in to comment.