From 9411c654298af7b260dbf1405fc8f88981394f08 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:14:06 +0400 Subject: [PATCH] catch exception when saving review in createReview --- src/controllers/api/Reviews.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/controllers/api/Reviews.php b/src/controllers/api/Reviews.php index ec29b59..c1a9ee7 100644 --- a/src/controllers/api/Reviews.php +++ b/src/controllers/api/Reviews.php @@ -5,9 +5,9 @@ namespace Steamy\Controller\API; use Opis\JsonSchema\{Errors\ErrorFormatter}; +use Exception; use Steamy\Core\Utility; use Steamy\Model\Review; -use Steamy\Model\Product as ProductModel; class Reviews { @@ -96,11 +96,13 @@ public function createReview(): void ); // Save the new review to the database - if ($newReview->save()) { + try { + $newReview->save(); // Review created successfully, return 201 Created http_response_code(201); - echo json_encode(['message' => 'Review created successfully', 'review_id' => $newReview->getReviewID()]); - } else { + echo json_encode(['message' => 'Review created successfully', 'review_id' => $newReview->getReviewID()] + ); + } catch (Exception $e) { // Failed to create review, return 500 Internal Server Error http_response_code(500); echo json_encode(['error' => 'Failed to create review']);