Skip to content

A free open source IT asset/license management system

Added exception handling

snipe3e101051cdcf
diff --git a/app/Exceptions/InvalidCheckoutRequestTransition.php b/app/Exceptions/InvalidCheckoutRequestTransition.php
new file mode 100644
index 0000000000..702647667b
--- /dev/null
+++ b/app/Exceptions/InvalidCheckoutRequestTransition.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Exceptions;
+
+use App\Enums\CheckoutRequestState;
+use App\Models\CheckoutRequest;
+use Exception;
+
+/**
+ * Thrown by CheckoutRequest state-machine actions
+ * (FulfillCheckoutRequestAction, CancelCheckoutRequestAction,
+ * ExpireCheckoutRequestAction) when the caller tries to move a
+ * request from a state that doesn't permit the target transition:
+ * canceling a fulfilled row, fulfilling an expired row, etc.
+ *
+ * Terminal states (fulfilled, canceled, expired) are irreversible.
+ * The exception carries the current + attempted states so
+ * controllers can map to a useful 422 body instead of a generic
+ * "something went wrong".
+ */
+class InvalidCheckoutRequestTransition extends Exception
+{
+ public function __construct(
+ public readonly CheckoutRequest $request,
+ public readonly CheckoutRequestState $from,
+ public readonly CheckoutRequestState $to,
+ ) {
+ parent::__construct(
+ "Cannot transition CheckoutRequest #{$request->id} from {$from->value} to {$to->value}."
+ );
+ }
+}