Skip to content

Commit

Permalink
[main]:+ Custom 419 response code fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Poortman committed Apr 20, 2022
1 parent bbe5c52 commit 2f9b4e6
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Controller/Post/Livewire.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,19 @@ public function execute(): Json
]);
} catch (Exception $exception) {
$code = $exception instanceof HttpException ? $exception->getStatusCode() : $exception->getCode();
$statuses = $this->getHttpResponseStatuses();

$code = Response::$statusTexts[$code] ?? Response::HTTP_INTERNAL_SERVER_ERROR;
$phrase = Response::$statusTexts[$code] . ': ' . $exception->getMessage();
// Try and grep the status from the available stack or get 500 when it's unavailable.
$code = $statuses[$code] ? $code : Response::HTTP_INTERNAL_SERVER_ERROR;
// Set the status header with the returned code and belonging response phrase.
$result->setStatusHeader($code, AbstractMessage::VERSION_11, $statuses[$code]);

$result->setStatusHeader($code, AbstractMessage::VERSION_11, $phrase);
$this->logger->critical('Magewire: ' . $phrase);
if ($code === 500) {
$this->logger->critical($exception->getMessage());
}

return $result->setData([
'message' => $phrase,
'message' => $exception->getMessage(),
'code' => $code
]);
}
Expand Down Expand Up @@ -175,4 +179,15 @@ public function validateForUpdateRequest(): void
throw new HttpException(419, 'Form key expired. Please refresh and try again.');
}
}

/**
* @return array
*/
public function getHttpResponseStatuses(): array
{
$statuses = Response::$statusTexts;
$statuses[419] = 'Form key expired';

return $statuses;
}
}

0 comments on commit 2f9b4e6

Please sign in to comment.