Skip to content

Commit

Permalink
fix(idempotency): include cause in idempotency persistence layer error
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Aug 12, 2024
1 parent 4a87213 commit d4f18e7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 52 deletions.
2 changes: 0 additions & 2 deletions packages/idempotency/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ class IdempotencyInconsistentStateError extends IdempotencyUnknownError {
* Unrecoverable error from the data store
*/
class IdempotencyPersistenceLayerError extends IdempotencyUnknownError {
public readonly cause: Error | undefined;

public constructor(message: string, options?: ErrorOptions) {
super(message, options);
this.name = 'IdempotencyPersistenceLayerError';
Expand Down
27 changes: 13 additions & 14 deletions packages/idempotency/tests/unit/IdempotencyHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Test Idempotency Handler
*
* @group unit/idempotency/IdempotencyHandler
*/
import { IdempotencyRecord } from '../../src/persistence/index.js';
import { IdempotencyHandler } from '../../src/IdempotencyHandler.js';
import { IdempotencyRecordStatus, MAX_RETRIES } from '../../src/constants.js';
import {
IdempotencyConfig,
IdempotencyAlreadyInProgressError,
IdempotencyConfig,
IdempotencyInconsistentStateError,
IdempotencyItemAlreadyExistsError,
IdempotencyPersistenceLayerError,
} from '../../src/index.js';
import { MAX_RETRIES, IdempotencyRecordStatus } from '../../src/constants.js';
/**
* Test Idempotency Handler
*
* @group unit/idempotency/IdempotencyHandler
*/
import { IdempotencyRecord } from '../../src/persistence/index.js';
import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils.js';

const mockFunctionToMakeIdempotent = jest.fn();
Expand Down Expand Up @@ -198,12 +198,11 @@ describe('Class IdempotencyHandler', () => {
.mockRejectedValue(new Error('Some error'));

// Act & Assess
await expect(idempotentHandler.getFunctionResult()).rejects.toThrow(
new IdempotencyPersistenceLayerError(
'Failed to delete record from idempotency store',
new Error('Some error')
)
);
await expect(idempotentHandler.getFunctionResult()).rejects.toThrow({
name: 'IdempotencyPersistenceLayerError',
message: 'Failed to delete record from idempotency store',
cause: new Error('Some error'),
});
expect(mockDeleteInProgress).toHaveBeenCalledTimes(1);
});

Expand Down
33 changes: 15 additions & 18 deletions packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ describe('Middleware: makeHandlerIdempotent', () => {
.mockRejectedValue(new Error('Something went wrong'));

// Act && Assess
await expect(handler(event, context)).rejects.toThrowError(
new IdempotencyPersistenceLayerError(
'Failed to save in progress record to idempotency store',
new Error('Something went wrong')
)
);
await expect(handler(event, context)).rejects.toThrow({
name: 'IdempotencyPersistenceLayerError',
message: 'Failed to save in progress record to idempotency store',
cause: new Error('Something went wrong'),
});
});
it('thows an error if the persistence layer throws an error when saving a successful operation', async () => {
// Prepare
Expand All @@ -129,12 +128,11 @@ describe('Middleware: makeHandlerIdempotent', () => {
.mockRejectedValue(new Error('Something went wrong'));

// Act && Assess
await expect(handler(event, context)).rejects.toThrowError(
new IdempotencyPersistenceLayerError(
'Failed to update success record to idempotency store',
new Error('Something went wrong')
)
);
await expect(handler(event, context)).rejects.toThrow({
name: 'IdempotencyPersistenceLayerError',
message: 'Failed to update success record to idempotency store',
cause: new Error('Something went wrong'),
});
});
it('thows an error if the persistence layer throws an error when deleting a record', async () => {
// Prepare
Expand All @@ -148,12 +146,11 @@ describe('Middleware: makeHandlerIdempotent', () => {
.mockRejectedValue(new Error('Something went wrong'));

// Act && Assess
await expect(handler(event, context)).rejects.toThrow(
new IdempotencyPersistenceLayerError(
'Failed to delete record from idempotency store',
new Error('Something went wrong')
)
);
await expect(handler(event, context)).rejects.toThrow({
name: 'IdempotencyPersistenceLayerError',
message: 'Failed to delete record from idempotency store',
cause: new Error('Something went wrong'),
});
});
it('returns the stored response if the operation has already been executed', async () => {
// Prepare
Expand Down
33 changes: 15 additions & 18 deletions packages/idempotency/tests/unit/makeIdempotent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,11 @@ describe('Function: makeIdempotent', () => {
.mockRejectedValue(new Error('Something went wrong'));

// Act && Assess
await expect(handler(event, context)).rejects.toThrowError(
new IdempotencyPersistenceLayerError(
'Failed to save in progress record to idempotency store',
new Error('Something went wrong')
)
);
await expect(handler(event, context)).rejects.toThrow({
name: 'IdempotencyPersistenceLayerError',
message: 'Failed to save in progress record to idempotency store',
cause: new Error('Something went wrong'),
});
});
it('thows an error if the persistence layer throws an error when saving a successful operation', async () => {
// Prepare
Expand All @@ -139,12 +138,11 @@ describe('Function: makeIdempotent', () => {
.mockRejectedValue(new Error('Something went wrong'));

// Act && Assess
await expect(handler(event, context)).rejects.toThrowError(
new IdempotencyPersistenceLayerError(
'Failed to update success record to idempotency store',
new Error('Something went wrong')
)
);
await expect(handler(event, context)).rejects.toThrow({
name: 'IdempotencyPersistenceLayerError',
message: 'Failed to update success record to idempotency store',
cause: new Error('Something went wrong'),
});
});
it('thows an error if the persistence layer throws an error when deleting a record', async () => {
// Prepare
Expand All @@ -162,12 +160,11 @@ describe('Function: makeIdempotent', () => {
.mockRejectedValue(new Error('Something went wrong'));

// Act && Assess
await expect(handler(event, context)).rejects.toThrow(
new IdempotencyPersistenceLayerError(
'Failed to delete record from idempotency store',
new Error('Something went wrong')
)
);
await expect(handler(event, context)).rejects.toThrow({
name: 'IdempotencyPersistenceLayerError',
message: 'Failed to delete record from idempotency store',
cause: new Error('Something went wrong'),
});
});
it('returns the stored response if the operation has already been executed', async () => {
// Prepare
Expand Down

0 comments on commit d4f18e7

Please sign in to comment.