From d8ebc9b9d7e4be65ae50034020eb453bfad2c1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Wed, 19 Jun 2024 18:36:52 +0200 Subject: [PATCH] Log http messages --- .../ProcessUploadOrderRequestHandler.php | 59 ++++++++++++++++--- src/Model/UploadOrderRequest.php | 36 +++++++++++ src/Model/UploadOrderRequestInterface.php | 12 ++++ src/Processor/UploadOrderRequestProcessor.php | 2 - 4 files changed, 100 insertions(+), 9 deletions(-) diff --git a/src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php b/src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php index af7c375..97020a8 100644 --- a/src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php +++ b/src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php @@ -5,6 +5,8 @@ namespace Setono\SyliusPeakWMSPlugin\Message\CommandHandler; use Doctrine\Persistence\ManagerRegistry; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; use Setono\Doctrine\ORMTrait; use Setono\PeakWMS\Client\ClientInterface; use Setono\PeakWMS\DataTransferObject\SalesOrder\SalesOrder; @@ -34,7 +36,9 @@ public function __construct( public function __invoke(ProcessUploadOrderRequest $message): void { - $uploadOrderRequest = $this->getManager($this->uploadOrderRequestClass)->find($this->uploadOrderRequestClass, $message->uploadOrderRequest); + $manager = $this->getManager($this->uploadOrderRequestClass); + + $uploadOrderRequest = $manager->find($this->uploadOrderRequestClass, $message->uploadOrderRequest); if (!$uploadOrderRequest instanceof UploadOrderRequestInterface) { throw new UnrecoverableMessageHandlingException(sprintf('Upload order request with id %d does not exist', $message->uploadOrderRequest)); } @@ -43,18 +47,59 @@ public function __invoke(ProcessUploadOrderRequest $message): void throw new UnrecoverableMessageHandlingException(sprintf('Upload order request with id %d has been updated since it was tried to be processed', $message->uploadOrderRequest)); } - // todo try catch exceptions and log errors - $order = $uploadOrderRequest->getOrder(); if (null === $order) { throw new UnrecoverableMessageHandlingException(sprintf('The upload order request with id %d does not have an associated order', $message->uploadOrderRequest)); } - $salesOrder = new SalesOrder(); - $this->salesOrderDataMapper->map($order, $salesOrder); + try { + $salesOrder = new SalesOrder(); + $this->salesOrderDataMapper->map($order, $salesOrder); + + $this->peakWMSClient->salesOrder()->create($salesOrder); + + $this->uploadOrderRequestWorkflow->apply($order, UploadOrderRequestWorkflow::TRANSITION_UPLOAD); + } catch (\Throwable $e) { + $uploadOrderRequest->setError($e->getMessage()); + + $this->uploadOrderRequestWorkflow->apply($order, UploadOrderRequestWorkflow::TRANSITION_FAIL); + + throw new UnrecoverableMessageHandlingException( + message: sprintf('Failed to process upload order request with id %d', $message->uploadOrderRequest), + previous: $e, + ); + } finally { + $uploadOrderRequest->setRequest(self::stringifyMessage($this->peakWMSClient->getLastRequest())); + $uploadOrderRequest->setResponse(self::stringifyMessage($this->peakWMSClient->getLastResponse())); + $manager->flush(); + } + } + + private static function stringifyMessage(RequestInterface|ResponseInterface|null $message): ?string + { + if (null === $message) { + return null; + } + + $result = ''; + if ($message instanceof RequestInterface) { + $result = sprintf( + "%s %s HTTP/%s\n", + $message->getMethod(), + $message->getUri(), + $message->getProtocolVersion(), + ); + } + + foreach ($message->getHeaders() as $name => $values) { + $result .= sprintf("%s: %s\n", $name, implode(', ', $values)); + } - $this->peakWMSClient->salesOrder()->create($salesOrder); + $body = trim((string) $message->getBody()); + if ('' !== $body) { + $result .= "\n\n" . $body; + } - $this->uploadOrderRequestWorkflow->apply($order, UploadOrderRequestWorkflow::TRANSITION_UPLOAD); + return $result; } } diff --git a/src/Model/UploadOrderRequest.php b/src/Model/UploadOrderRequest.php index 2e06c08..72c5c2e 100644 --- a/src/Model/UploadOrderRequest.php +++ b/src/Model/UploadOrderRequest.php @@ -14,6 +14,12 @@ class UploadOrderRequest implements UploadOrderRequestInterface protected ?OrderInterface $order = null; + protected ?string $request = null; + + protected ?string $response = null; + + protected ?string $error = null; + public function getId(): ?int { return $this->id; @@ -48,4 +54,34 @@ public function setOrder(?OrderInterface $order): void { $this->order = $order; } + + public function getRequest(): ?string + { + return $this->request; + } + + public function setRequest(?string $request): void + { + $this->request = $request; + } + + public function getResponse(): ?string + { + return $this->response; + } + + public function setResponse(?string $response): void + { + $this->response = $response; + } + + public function getError(): ?string + { + return $this->error; + } + + public function setError(?string $error): void + { + $this->error = $error; + } } diff --git a/src/Model/UploadOrderRequestInterface.php b/src/Model/UploadOrderRequestInterface.php index cd320ba..2d410b4 100644 --- a/src/Model/UploadOrderRequestInterface.php +++ b/src/Model/UploadOrderRequestInterface.php @@ -26,4 +26,16 @@ public function setState(string $state): void; public function getOrder(): ?OrderInterface; public function setOrder(?OrderInterface $order): void; + + public function getRequest(): ?string; + + public function setRequest(?string $request): void; + + public function getResponse(): ?string; + + public function setResponse(?string $response): void; + + public function getError(): ?string; + + public function setError(?string $error): void; } diff --git a/src/Processor/UploadOrderRequestProcessor.php b/src/Processor/UploadOrderRequestProcessor.php index ecf1b69..5ce7ad1 100644 --- a/src/Processor/UploadOrderRequestProcessor.php +++ b/src/Processor/UploadOrderRequestProcessor.php @@ -29,8 +29,6 @@ public function __construct( public function process(): void { - // todo: check for eligibility - foreach ($this->preQualifiedUploadableOrdersProvider->getUploadOrderRequests() as $uploadOrderRequest) { try { $this->uploadOrderRequestWorkflow->apply($uploadOrderRequest, UploadOrderRequestWorkflow::TRANSITION_PROCESS);