Skip to content

Commit

Permalink
Optimize exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed May 24, 2024
1 parent 2548d0d commit f3c445a
Showing 1 changed file with 70 additions and 15 deletions.
85 changes: 70 additions & 15 deletions src/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,33 @@
class Handler
{
/**
* Returns the error view name.
* The error view name.
*
* @var string
*/
protected $errorView = 'Error';

/**
* Returns the flash error message key name.
* The flash error message key name.
*
* @var string
*/
protected $errorMessageKey = 'error';

/**
* Returns the messages transform callback.
* The error messages transform callback.
*
* @var callable|null
*/
protected $messagesCallback;

/**
* The error message transform callback.
*
* @var callable|null
*/
protected $messageCallback;

/**
* Create a new inertia exception handler instance.
*
Expand All @@ -51,9 +58,9 @@ public function handle(Request $request, SymfonyResponse $response, Throwable $e

$messages = $this->resolveMessages($e);

$message = array_key_exists($code, $messages)
? $messages[$code]
: $messages[500];
$messageContext = $this->resolveMessage($code, $messages, $e);
$title = $messageContext['title'];
$message = $messageContext['message'];

if (! $request->isMethod('GET') && in_array($code, [419, 429])) {
return back()
Expand All @@ -63,10 +70,15 @@ public function handle(Request $request, SymfonyResponse $response, Throwable $e

if (! config('app.debug') && array_key_exists($code, $messages)) {
$response = $this->inertia->render($this->errorView);
$response = $this->transformInertiaErrorResponse($response, compact('message'));

$response = $this->transformInertiaErrorResponse($response, [
'title' => $title,
'message' => $message,
]);

return $response
->with('code', $code)
->with('title', $title)
->with('message', $message)
->toResponse($request)
->setStatusCode($code);
Expand Down Expand Up @@ -104,21 +116,22 @@ public function errorMessageKey(string $key)
*
* @return array<int, string>
*/
protected function messages(Throwable $e): array
protected function messages(): array
{
return [
401 => 'Unauthorized',
403 => $e->getMessage() ?: 'Forbidden',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
419 => 'The page expired, please try again.',
429 => $e->getMessage() ?: 'Too Many Requests',
429 => 'Too Many Requests',
500 => 'Server Error',
503 => $e->getMessage() ?: 'Service Unavailable',
503 => 'Service Unavailable',
];
}

/**
* Regsiter the messages transform callback.
* Regsiter the error messages transform callback.
*
* @return $this
*/
Expand All @@ -130,13 +143,25 @@ public function withMessages(callable $callback)
}

/**
* Resolve the error message.
* Regsiter the error message transform callback.
*
* @return $this
*/
public function withMessage(callable $callback)
{
$this->messageCallback = $callback;

return $this;
}

/**
* Resolve the error messages.
*
* @return array<int, string>
*/
protected function resolveMessages(Throwable $e): array
{
$messages = $this->messages($e);
$messages = $this->messages();

if ($this->messagesCallback) {
$messages = call_user_func($this->messagesCallback, $messages, $e);
Expand All @@ -145,13 +170,43 @@ protected function resolveMessages(Throwable $e): array
return $messages;
}

/**
* Resolve the error message.
*
* @param array<int, string> $messages
* @return array<string, string>
*/
protected function resolveMessage(int $code, array $messages, Throwable $e): array
{
if ($code === 403 && $message = $e->getMessage()) {
$message = __($message ?? $messages[403]);
$title = $messages[403];
} elseif (array_key_exists($code, $messages)) {
$message = __($messages[$code]);
$title = $message;
} else {
$message = __($messages[500]);
$title = $message;
}

if ($this->messageCallback) {
$message = call_user_func($this->messageCallback, $message, $code, $e);
$title = $message;
}

return [
'title' => $title,
'message' => $message,
];
}

/**
* Transform the inertia error response.
*/
protected function transformInertiaErrorResponse(InertiaResponse $response, array $params = []): InertiaResponse
{
if (InertiaResponse::hasMacro('title')) {
$response->title($params['message']);
$response->title($params['title']);
}

return $response;
Expand Down

0 comments on commit f3c445a

Please sign in to comment.