From 6ab189137ae6efb8f84e82d200c3b576128166d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Fri, 16 Aug 2024 11:17:18 +0200 Subject: [PATCH] Output the error when failing to update the order --- composer.json | 1 + src/Controller/EditOrderAction.php | 23 ++++++++++++---------- src/Resources/translations/flashes.en.yaml | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index aa5555c..52dd4d6 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "symfony/messenger": "^5.4 || ^6.4 || ^7.0", "symfony/options-resolver": "^5.4 || ^6.4 || ^7.0", "symfony/routing": "^5.4 || ^6.4 || ^7.0", + "symfony/translation": "^5.4 || ^6.4 || ^7.0", "webmozart/assert": "^1.11" }, "require-dev": { diff --git a/src/Controller/EditOrderAction.php b/src/Controller/EditOrderAction.php index 038b685..67c674f 100644 --- a/src/Controller/EditOrderAction.php +++ b/src/Controller/EditOrderAction.php @@ -12,6 +12,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Translation\TranslatableMessage; final class EditOrderAction { @@ -28,38 +29,40 @@ public function __invoke(Request $request, int $id): Response $this->orderUpdater->update($request, $id); } catch (NewOrderWrongTotalException) { return $this->addFlashAndRedirect( - 'error', - 'setono_sylius_order_edit.order_update.total_error', 'sylius_admin_order_update', $id, + 'error', + 'setono_sylius_order_edit.order_update.total_error', ); - } catch (\Throwable) { + } catch (\Throwable $e) { return $this->addFlashAndRedirect( - 'error', - 'setono_sylius_order_edit.order_update.general_error', 'sylius_admin_order_update', $id, + 'error', + 'setono_sylius_order_edit.order_update.general_error', + ['%error%' => $e->getMessage()], ); } return $this->addFlashAndRedirect( - 'success', - 'setono_sylius_order_edit.order_update.success', 'sylius_admin_order_show', $id, + 'success', + 'setono_sylius_order_edit.order_update.success', ); } private function addFlashAndRedirect( - string $type, - string $message, string $route, int $orderId, + string $type, + string $message, + array $messageParameters = [], ): RedirectResponse { $session = $this->requestStack->getSession(); if ($session instanceof Session) { - $session->getFlashBag()->add($type, $message); + $session->getFlashBag()->add($type, new TranslatableMessage($message, $messageParameters, 'flashes')); } return new RedirectResponse($this->router->generate($route, ['id' => $orderId])); diff --git a/src/Resources/translations/flashes.en.yaml b/src/Resources/translations/flashes.en.yaml index aa7c27d..f7a74c8 100644 --- a/src/Resources/translations/flashes.en.yaml +++ b/src/Resources/translations/flashes.en.yaml @@ -1,5 +1,5 @@ setono_sylius_order_edit: order_update: - general_error: 'An error occurred while updating the order' + general_error: 'An error occurred while updating the order: %error%' total_error: 'Order should not have bigger total than before editing' success: 'Order has been successfully updated'