Skip to content

Commit

Permalink
catch exception when saving review in createReview
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jul 17, 2024
1 parent 020af46 commit 9411c65
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/controllers/api/Reviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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']);
Expand Down

0 comments on commit 9411c65

Please sign in to comment.