Browse Source

fix(core): Relax validation of custom process states

The last release caused an error to be thrown on bootstrap
in the case of unreachable states. There are however valid
cases for making states unreachable, so now we will just print
a warning.
Michael Bromley 2 years ago
parent
commit
cf301eb788

+ 1 - 1
packages/core/src/common/finite-state-machine/validate-transition-definition.spec.ts

@@ -74,7 +74,7 @@ describe('FSM validateTransitionDefinition()', () => {
 
         const result = validateTransitionDefinition(valid, 'Start');
 
-        expect(result.valid).toBe(false);
+        expect(result.valid).toBe(true);
         expect(result.error).toBe('The following states are unreachable: Unreachable');
     });
 

+ 11 - 13
packages/core/src/common/finite-state-machine/validate-transition-definition.ts

@@ -53,17 +53,15 @@ export function validateTransitionDefinition<T extends string>(
         };
     }
 
-    if (!allStatesReached()) {
-        return {
-            valid: false,
-            error: `The following states are unreachable: ${Object.entries(result)
-                .filter(([s, v]) => !(v as ValidationResult).reachable)
-                .map(([s]) => s)
-                .join(', ')}`,
-        };
-    } else {
-        return {
-            valid: true,
-        };
-    }
+    const error = !allStatesReached()
+        ? `The following states are unreachable: ${Object.entries(result)
+              .filter(([s, v]) => !(v as ValidationResult).reachable)
+              .map(([s]) => s)
+              .join(', ')}`
+        : undefined;
+
+    return {
+        valid: true,
+        error,
+    };
 }

+ 3 - 0
packages/core/src/service/helpers/fulfillment-state-machine/fulfillment-state-machine.ts

@@ -63,6 +63,9 @@ export class FulfillmentStateMachine {
             Logger.error(`The fulfillment process has an invalid configuration:`);
             throw new Error(validationResult.error);
         }
+        if (validationResult.valid && validationResult.error) {
+            Logger.warn(`Fulfillment process: ${validationResult.error}`);
+        }
         return {
             transitions: allTransitions,
             onTransitionStart: async (fromState, toState, data) => {

+ 3 - 0
packages/core/src/service/helpers/order-state-machine/order-state-machine.ts

@@ -56,6 +56,9 @@ export class OrderStateMachine {
             Logger.error(`The order process has an invalid configuration:`);
             throw new Error(validationResult.error);
         }
+        if (validationResult.valid && validationResult.error) {
+            Logger.warn(`Order process: ${validationResult.error}`);
+        }
         return {
             transitions: allTransitions,
             onTransitionStart: async (fromState, toState, data) => {

+ 3 - 0
packages/core/src/service/helpers/payment-state-machine/payment-state-machine.ts

@@ -58,6 +58,9 @@ export class PaymentStateMachine {
             Logger.error(`The payment process has an invalid configuration:`);
             throw new Error(validationResult.error);
         }
+        if (validationResult.valid && validationResult.error) {
+            Logger.warn(`Payment process: ${validationResult.error}`);
+        }
         return {
             transitions: allTransitions,
             onTransitionStart: async (fromState, toState, data) => {