Skip to content

Commit

Permalink
handle PDOException separately, send 205 http code if all good on che…
Browse files Browse the repository at this point in the history
…ckout
  • Loading branch information
creme332 committed May 22, 2024
1 parent 7fc6e37 commit f1be763
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/controllers/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Steamy\Controller;

use Exception;
use PDOException;
use Steamy\Core\Controller;
use Steamy\Core\Utility;
use Steamy\Model\Order;
Expand Down Expand Up @@ -110,13 +111,17 @@ private function handleCheckout(): void
// attempt to save order. An exception will be generated in case of any errors.
try {
$success_order = $new_order->save();
} catch (PDOException $e) {
error_log($e->getMessage());
http_response_code(503);
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
return;
} catch (Exception $e) {
error_log($e->getMessage());
http_response_code(500);
http_response_code(400);
echo json_encode(['error' => $e->getMessage()]);
return;
}

// if order was unsuccessfully saved without any exceptions generated
if (!$success_order) {
http_response_code(500);
Expand All @@ -137,6 +142,9 @@ private function handleCheckout(): void
http_response_code(503);
echo json_encode(['error' => "Order was saved but email could not be sent for an unknown reason."]);
}

// if everything is good, tell client to reset the document view
http_response_code(205);
}

public function index(): void
Expand Down

0 comments on commit f1be763

Please sign in to comment.