Skip to content

Commit

Permalink
Fix Saga pattern to only compensate in case of a Terminal Error
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanEwen committed Jun 2, 2024
1 parent 6d38af4 commit f222361
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions patterns-use-cases/sagas/sagas-typescript/src/workflow_saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ export default async (ctx: restate.Context, tripID: string) => {
//
await flights.confirm(tripID, flightBooking);
await cars.confirm(tripID, carBooking);
} catch (e: any) {
// undo all the steps up to this point by running the compensations
for (const compensation of compensations.reverse()) {
await compensation();
}

} catch (e) {

// exit with an error
throw new restate.TerminalError(
`Travel reservation failed with an error: ${e.message}`,
{
cause: e,
if (e instanceof restate.TerminalError) {
// undo all the steps up to this point by running the compensations
for (const compensation of compensations.reverse()) {
await compensation();
}
);
}

throw e;
}
};

0 comments on commit f222361

Please sign in to comment.