Skip to content

Commit 800d6ae

Browse files
committed
Better error messages in reducer
1 parent e88d178 commit 800d6ae

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/reducer.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ export const appearStartedState = stateFunc(StateID.AppearStarted, "appear");
190190
export const enterStartedState = stateFunc(StateID.EnterStarted, "enter");
191191
export const leaveStartedState = stateFunc(StateID.LeaveStarted, "leave");
192192

193+
const invalidTransitionError = (stateID: StateID, actionID: ActionID) => {
194+
return new Error(`invalid state transition from ${StateID[stateID]} with ${ActionID[actionID]}`);
195+
};
196+
193197
export type Reducer = (stateID: StateID, action: Action) =>
194198
{ state: TransitionState, pending?: ActionID, completed?: boolean };
195199

@@ -236,7 +240,7 @@ export const reducer: Reducer = (stateID, action) => {
236240
nextState = appearInitState(props);
237241
break;
238242
default:
239-
throw new Error(`invalid state transition from ${StateID[stateID]}`);
243+
throw invalidTransitionError(stateID, action.kind);
240244
};
241245
return { state: nextState, pending: ActionID.TransitionPrepare };
242246
case ActionID.TransitionPrepare:
@@ -254,7 +258,7 @@ export const reducer: Reducer = (stateID, action) => {
254258
nextState = appearPrepareState(props);
255259
break;
256260
default:
257-
throw new Error(`invalid state transition from ${StateID[stateID]}`);
261+
throw invalidTransitionError(stateID, action.kind);
258262
};
259263
return { state: nextState, pending: ActionID.TransitionTrigger };
260264
case ActionID.TransitionStart:
@@ -281,7 +285,7 @@ export const reducer: Reducer = (stateID, action) => {
281285
case StateID.LeaveStarted:
282286
return { state: defaultState(props), completed: true };
283287
default:
284-
throw new Error(`invalid state transition from ${StateID[stateID]}`);
288+
throw invalidTransitionError(stateID, action.kind);
285289
}
286290
case ActionID.TransitionTrigger:
287291
switch (stateID) {
@@ -318,7 +322,7 @@ export const reducer: Reducer = (stateID, action) => {
318322
case StateID.LeaveStarted:
319323
return { state: enterStartedState(props) };
320324
default:
321-
throw new Error(`invalid state transition from ${StateID[stateID as any]}`);
325+
throw invalidTransitionError(stateID, action.kind);
322326
}
323327
case ActionID.Timeout:
324328
switch (stateID) {
@@ -331,7 +335,7 @@ export const reducer: Reducer = (stateID, action) => {
331335
case StateID.LeaveStarted:
332336
return { state: defaultState(props), completed: true };
333337
default:
334-
throw new Error(`invalid state transition from ${StateID[stateID]}`);
338+
throw invalidTransitionError(stateID, action.kind);
335339
}
336340
default:
337341
}

0 commit comments

Comments
 (0)