Skip to content

Commit f222361

Browse files
committed
Fix Saga pattern to only compensate in case of a Terminal Error
1 parent 6d38af4 commit f222361

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

patterns-use-cases/sagas/sagas-typescript/src/workflow_saga.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,16 @@ export default async (ctx: restate.Context, tripID: string) => {
6060
//
6161
await flights.confirm(tripID, flightBooking);
6262
await cars.confirm(tripID, carBooking);
63-
} catch (e: any) {
64-
// undo all the steps up to this point by running the compensations
65-
for (const compensation of compensations.reverse()) {
66-
await compensation();
67-
}
63+
64+
} catch (e) {
6865

69-
// exit with an error
70-
throw new restate.TerminalError(
71-
`Travel reservation failed with an error: ${e.message}`,
72-
{
73-
cause: e,
66+
if (e instanceof restate.TerminalError) {
67+
// undo all the steps up to this point by running the compensations
68+
for (const compensation of compensations.reverse()) {
69+
await compensation();
7470
}
75-
);
71+
}
72+
73+
throw e;
7674
}
7775
};

0 commit comments

Comments
 (0)