Skip to content

Commit

Permalink
refactor: always return undefined errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Aug 4, 2023
1 parent cf3e072 commit 8ccc660
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

Undefined for Laravel takes care of formatting exceptions and errors into nicely formatted JSON responses.

> Note: This package is still a Work in Progress and things may change.
> **Warning**
> Package is in early stages of development. It means there may be bugs and API is very likely to change. Create an
> issue if you spot a bug. Ideas are welcome.
## Installation

Expand Down
14 changes: 5 additions & 9 deletions src/Renderables/AccessDeniedHttpExceptionRenderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ class AccessDeniedHttpExceptionRenderable
{
public function __invoke(AccessDeniedHttpException $exception, Request $request): ErrorResponse|null
{
if ($request->expectsJson()) {
return new ErrorResponse(
errorType: ErrorTypeEnum::InvalidRequestError,
message: 'The provided key does not have the required permissions for this endpoint.',
status: 401,
);
}

return null;
return new ErrorResponse(
errorType: ErrorTypeEnum::InvalidRequestError,
message: 'The provided key does not have the required permissions for this endpoint.',
status: 401,
);
}
}
8 changes: 6 additions & 2 deletions src/Renderables/AuthenticationExceptionRenderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ class AuthenticationExceptionRenderable
{
public function __invoke(AuthenticationException $exception, Request $request): ErrorResponse|null
{
if ($request->expectsJson()) {
if (! $request->bearerToken()) {
return new ErrorResponse(
errorType: ErrorTypeEnum::InvalidRequestError,
message: 'You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. `Authorization: Bearer YOUR_SECRET_KEY`).',
status: 401,
);
}

return null;
return new ErrorResponse(
errorType: ErrorTypeEnum::InvalidRequestError,
message: "Invalid API Key provided: {$request->bearerToken()}",
status: 401,
);
}
}
14 changes: 6 additions & 8 deletions src/Renderables/NotFoundHttpExceptionRenderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ class NotFoundHttpExceptionRenderable
{
public function __invoke(NotFoundHttpException $exception, Request $request): ErrorResponse|null
{
if ($request->expectsJson()) {
return new ErrorResponse(
errorType: ErrorTypeEnum::InvalidRequestError,
message: "Unrecognised request URL ({$request->method()} /{$request->path()}).",
status: 404,
);
}
$path = $request->path() !== '/' ? "/{$request->path()}" : '/';

return null;
return new ErrorResponse(
errorType: ErrorTypeEnum::InvalidRequestError,
message: "Unrecognised request URL ({$request->method()} $path).",
status: 404,
);
}
}
48 changes: 22 additions & 26 deletions src/Renderables/ValidationExceptionRenderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,29 @@ class ValidationExceptionRenderable
{
public function __invoke(ValidationException $exception, Request $request): ErrorResponse|null
{
if ($request->expectsJson()) {
/** @var ValidationData $validationError */
$validationError = collect($exception->errors())
->flatMap(function (array $errors, string $parameter) use ($exception) {
return collect($errors)
->map(function ($message) use ($exception, $parameter) {
/** @var string $rule */
$rule = array_keys($exception->validator->failed()[$parameter])[0];
/** @var ValidationData $validationError */
$validationError = collect($exception->errors())
->flatMap(function (array $errors, string $parameter) use ($exception) {
return collect($errors)
->map(function ($message) use ($exception, $parameter) {
/** @var string $rule */
$rule = array_keys($exception->validator->failed()[$parameter])[0];

return new ValidationData(
parameter: $parameter,
rule: mb_strtolower($rule),
message: $message,
);
});
})
->first();
return new ValidationData(
parameter: $parameter,
rule: mb_strtolower($rule),
message: $message,
);
});
})
->first();

return new ErrorResponse(
errorType: ErrorTypeEnum::InvalidRequestError,
message: $validationError->message,
code: "parameter_{$validationError->rule}",
param: $validationError->parameter,
status: 422,
);
}

return null;
return new ErrorResponse(
errorType: ErrorTypeEnum::InvalidRequestError,
message: $validationError->message,
code: "parameter_{$validationError->rule}",
param: $validationError->parameter,
status: 422,
);
}
}

0 comments on commit 8ccc660

Please sign in to comment.