From 2f9b4e680a8571df5618d3d177f481adb447911a Mon Sep 17 00:00:00 2001 From: Willem Poortman Date: Wed, 20 Apr 2022 14:13:39 +0200 Subject: [PATCH] [main]:+ Custom 419 response code fix --- src/Controller/Post/Livewire.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Controller/Post/Livewire.php b/src/Controller/Post/Livewire.php index 061c59c..0cf6c28 100644 --- a/src/Controller/Post/Livewire.php +++ b/src/Controller/Post/Livewire.php @@ -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 ]); } @@ -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; + } }