Skip to content

feat(idempotency): return existing record in IdempotencyValidationError #2059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/idempotency/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ class IdempotencyInvalidStatusError extends Error {}
/**
* Payload does not match stored idempotency record
*/
class IdempotencyValidationError extends Error {}
class IdempotencyValidationError extends Error {
public existingRecord?: IdempotencyRecord;

public constructor(message?: string, existingRecord?: IdempotencyRecord) {
super(message);
this.existingRecord = existingRecord;
}
}

/**
* State is inconsistent across multiple requests to persistence store
Expand Down
3 changes: 2 additions & 1 deletion packages/idempotency/src/persistence/BasePersistenceLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
const hashedPayload: string = this.getHashedPayload(data);
if (hashedPayload !== record.payloadHash) {
throw new IdempotencyValidationError(
'Payload does not match stored record for this event key'
'Payload does not match stored record for this event key',
record
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,20 @@ describe('Class: BasePersistenceLayer', () => {
payloadValidationJmesPath: 'foo',
}),
});
jest.spyOn(persistenceLayer, '_getRecord').mockReturnValue(
new IdempotencyRecord({
idempotencyKey: 'my-lambda-function#mocked-hash',
status: IdempotencyRecordStatus.INPROGRESS,
payloadHash: 'different-hash',
})
);
const existingRecord = new IdempotencyRecord({
idempotencyKey: 'my-lambda-function#mocked-hash',
status: IdempotencyRecordStatus.INPROGRESS,
payloadHash: 'different-hash',
});
jest
.spyOn(persistenceLayer, '_getRecord')
.mockReturnValue(existingRecord);

// Act & Assess
await expect(persistenceLayer.getRecord({ foo: 'bar' })).rejects.toThrow(
new IdempotencyValidationError(
'Payload does not match stored record for this event key'
'Payload does not match stored record for this event key',
existingRecord
)
);
});
Expand Down