diff --git a/packages/idempotency/src/IdempotencyHandler.ts b/packages/idempotency/src/IdempotencyHandler.ts index 07b0786732..1caeeef6dc 100644 --- a/packages/idempotency/src/IdempotencyHandler.ts +++ b/packages/idempotency/src/IdempotencyHandler.ts @@ -5,7 +5,7 @@ import { IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from './Exceptions'; +} from './errors'; import { BasePersistenceLayer, IdempotencyRecord } from './persistence'; import { IdempotencyConfig } from './IdempotencyConfig'; import { MAX_RETRIES } from './constants'; @@ -80,7 +80,8 @@ export class IdempotencyHandler { ); } catch (e) { throw new IdempotencyPersistenceLayerError( - 'Failed to delete record from idempotency store' + 'Failed to delete record from idempotency store', + e as Error ); } throw e; @@ -92,7 +93,8 @@ export class IdempotencyHandler { ); } catch (e) { throw new IdempotencyPersistenceLayerError( - 'Failed to update success record to idempotency store' + 'Failed to update success record to idempotency store', + e as Error ); } @@ -153,7 +155,10 @@ export class IdempotencyHandler { idempotencyRecord ) as U; } else { - throw new IdempotencyPersistenceLayerError(); + throw new IdempotencyPersistenceLayerError( + 'Failed to save record in progress', + e as Error + ); } } diff --git a/packages/idempotency/src/Exceptions.ts b/packages/idempotency/src/errors.ts similarity index 78% rename from packages/idempotency/src/Exceptions.ts rename to packages/idempotency/src/errors.ts index 650d4419d4..21812ece6a 100644 --- a/packages/idempotency/src/Exceptions.ts +++ b/packages/idempotency/src/errors.ts @@ -31,7 +31,17 @@ class IdempotencyInconsistentStateError extends Error {} /** * Unrecoverable error from the data store */ -class IdempotencyPersistenceLayerError extends Error {} +class IdempotencyPersistenceLayerError extends Error { + public readonly cause: Error | undefined; + + public constructor(message: string, cause?: Error) { + const errorMessage = cause + ? `${message}. This error was caused by: ${cause.message}.` + : message; + super(errorMessage); + this.cause = cause; + } +} /** * Payload does not contain an idempotent key diff --git a/packages/idempotency/src/index.ts b/packages/idempotency/src/index.ts index 02f3b0e01e..a07f3c5017 100644 --- a/packages/idempotency/src/index.ts +++ b/packages/idempotency/src/index.ts @@ -1,4 +1,4 @@ -export * from './Exceptions'; +export * from './errors'; export * from './IdempotencyConfig'; export * from './idempotentDecorator'; export * from './makeFunctionIdempotent'; diff --git a/packages/idempotency/src/middleware/makeHandlerIdempotent.ts b/packages/idempotency/src/middleware/makeHandlerIdempotent.ts index 5af5b2ab23..b0f574cb20 100644 --- a/packages/idempotency/src/middleware/makeHandlerIdempotent.ts +++ b/packages/idempotency/src/middleware/makeHandlerIdempotent.ts @@ -5,7 +5,7 @@ import { IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../Exceptions'; +} from '../errors'; import { IdempotencyRecord } from '../persistence'; import { MAX_RETRIES } from '../constants'; import type { @@ -124,7 +124,8 @@ const makeHandlerIdempotent = ( } } else { throw new IdempotencyPersistenceLayerError( - 'Failed to save in progress record to idempotency store' + 'Failed to save in progress record to idempotency store', + error as Error ); } } @@ -149,7 +150,8 @@ const makeHandlerIdempotent = ( ); } catch (e) { throw new IdempotencyPersistenceLayerError( - 'Failed to update success record to idempotency store' + 'Failed to update success record to idempotency store', + e as Error ); } }; @@ -172,7 +174,8 @@ const makeHandlerIdempotent = ( ); } catch (error) { throw new IdempotencyPersistenceLayerError( - 'Failed to delete record from idempotency store' + 'Failed to delete record from idempotency store', + error as Error ); } }; diff --git a/packages/idempotency/src/persistence/BasePersistenceLayer.ts b/packages/idempotency/src/persistence/BasePersistenceLayer.ts index 3f4a41ae29..94b235c004 100644 --- a/packages/idempotency/src/persistence/BasePersistenceLayer.ts +++ b/packages/idempotency/src/persistence/BasePersistenceLayer.ts @@ -8,7 +8,7 @@ import { BasePersistenceLayerInterface } from './BasePersistenceLayerInterface'; import { IdempotencyItemAlreadyExistsError, IdempotencyValidationError, -} from '../Exceptions'; +} from '../errors'; import { LRUCache } from './LRUCache'; /** diff --git a/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts b/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts index ed98141eee..78d85690f7 100644 --- a/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts +++ b/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts @@ -1,7 +1,7 @@ import { IdempotencyItemAlreadyExistsError, IdempotencyItemNotFoundError, -} from '../Exceptions'; +} from '../errors'; import { IdempotencyRecordStatus } from '../types'; import type { DynamoPersistenceOptions } from '../types'; import { diff --git a/packages/idempotency/src/persistence/IdempotencyRecord.ts b/packages/idempotency/src/persistence/IdempotencyRecord.ts index b2fe7a0528..cec0111270 100644 --- a/packages/idempotency/src/persistence/IdempotencyRecord.ts +++ b/packages/idempotency/src/persistence/IdempotencyRecord.ts @@ -1,6 +1,6 @@ import type { IdempotencyRecordOptions } from '../types'; import { IdempotencyRecordStatus } from '../types'; -import { IdempotencyInvalidStatusError } from '../Exceptions'; +import { IdempotencyInvalidStatusError } from '../errors'; /** * Class representing an idempotency record. diff --git a/packages/idempotency/tests/unit/IdempotencyHandler.test.ts b/packages/idempotency/tests/unit/IdempotencyHandler.test.ts index 5ff0558189..4650e12543 100644 --- a/packages/idempotency/tests/unit/IdempotencyHandler.test.ts +++ b/packages/idempotency/tests/unit/IdempotencyHandler.test.ts @@ -8,7 +8,7 @@ import { IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../../src/Exceptions'; +} from '../../src/errors'; import { IdempotencyRecordStatus } from '../../src/types'; import { BasePersistenceLayer, IdempotencyRecord } from '../../src/persistence'; import { IdempotencyHandler } from '../../src/IdempotencyHandler'; @@ -165,16 +165,20 @@ describe('Class IdempotencyHandler', () => { }); test('when persistences store throws any error, it wraps the error to IdempotencyPersistencesLayerError', async () => { + const innerError = new Error('Some error'); const mockSaveInProgress = jest .spyOn(mockIdempotencyOptions.persistenceStore, 'saveInProgress') - .mockRejectedValue(new Error('Some error')); + .mockRejectedValue(innerError); const mockDetermineResultFromIdempotencyRecord = jest .spyOn(IdempotencyHandler, 'determineResultFromIdempotencyRecord') .mockImplementation(() => 'result'); - await expect(idempotentHandler.processIdempotency()).rejects.toThrow( - IdempotencyPersistenceLayerError + new IdempotencyPersistenceLayerError( + 'Failed to save record in progress', + innerError + ) ); + expect(mockSaveInProgress).toHaveBeenCalledTimes(1); expect(mockDetermineResultFromIdempotencyRecord).toHaveBeenCalledTimes(0); }); @@ -323,7 +327,9 @@ describe('Class IdempotencyHandler', () => { .mockRejectedValue(new Error('Some error')); await expect(idempotentHandler.getFunctionResult()).rejects.toThrow( - IdempotencyPersistenceLayerError + new IdempotencyPersistenceLayerError( + 'Failed to delete record from idempotency store. This error was caused by: Some error.' + ) ); expect(mockDeleteInProgress).toHaveBeenCalledTimes(1); }); diff --git a/packages/idempotency/tests/unit/idempotentDecorator.test.ts b/packages/idempotency/tests/unit/idempotentDecorator.test.ts index 9a1b2dc643..e232ac595b 100644 --- a/packages/idempotency/tests/unit/idempotentDecorator.test.ts +++ b/packages/idempotency/tests/unit/idempotentDecorator.test.ts @@ -13,7 +13,7 @@ import { IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../../src/Exceptions'; +} from '../../src/errors'; import { IdempotencyConfig } from '../../src'; import { Context } from 'aws-lambda'; import { helloworldContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts'; diff --git a/packages/idempotency/tests/unit/makeFunctionIdempotent.test.ts b/packages/idempotency/tests/unit/makeFunctionIdempotent.test.ts index e5fb2f6037..8ae1a1687a 100644 --- a/packages/idempotency/tests/unit/makeFunctionIdempotent.test.ts +++ b/packages/idempotency/tests/unit/makeFunctionIdempotent.test.ts @@ -16,7 +16,7 @@ import { IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../../src/Exceptions'; +} from '../../src/errors'; import { IdempotencyConfig } from '../../src'; import { Context } from 'aws-lambda'; diff --git a/packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts index b36dbaf1be..09c0c22f80 100644 --- a/packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts @@ -12,7 +12,7 @@ import { IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../../src/Exceptions'; +} from '../../src/errors'; import { IdempotencyConfig } from '../../src/'; import middy from '@middy/core'; import { MAX_RETRIES } from '../../src/constants'; @@ -115,7 +115,7 @@ describe('Middleware: makeHandlerIdempotent', () => { // Act && Assess await expect(handler(event, context)).rejects.toThrowError( new IdempotencyPersistenceLayerError( - 'Failed to save in progress record to idempotency store' + 'Failed to save in progress record to idempotency store. This error was caused by: Something went wrong.' ) ); }); @@ -131,7 +131,7 @@ describe('Middleware: makeHandlerIdempotent', () => { // Act && Assess await expect(handler(event, context)).rejects.toThrowError( new IdempotencyPersistenceLayerError( - 'Failed to update success record to idempotency store' + 'Failed to update success record to idempotency store. This error was caused by: Something went wrong.' ) ); }); @@ -149,7 +149,7 @@ describe('Middleware: makeHandlerIdempotent', () => { // Act && Assess await expect(handler(event, context)).rejects.toThrow( new IdempotencyPersistenceLayerError( - 'Failed to delete record from idempotency store' + 'Failed to delete record from idempotency store. This error was caused by: Something went wrong.' ) ); }); diff --git a/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts b/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts index 9cc3dc63f5..848d3258a2 100644 --- a/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts +++ b/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts @@ -12,7 +12,7 @@ import { import { IdempotencyItemAlreadyExistsError, IdempotencyValidationError, -} from '../../../src/Exceptions'; +} from '../../../src/errors'; import type { IdempotencyConfigOptions } from '../../../src/types'; import { IdempotencyRecordStatus } from '../../../src/types'; diff --git a/packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts b/packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts index b8321bd44a..68810e9a35 100644 --- a/packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts +++ b/packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts @@ -7,7 +7,7 @@ import { DynamoDBPersistenceLayer } from '../../../src/persistence/DynamoDBPersi import { IdempotencyItemAlreadyExistsError, IdempotencyItemNotFoundError, -} from '../../../src/Exceptions'; +} from '../../../src/errors'; import { IdempotencyRecord } from '../../../src/persistence'; import type { DynamoPersistenceOptions } from '../../../src/types'; import { IdempotencyRecordStatus } from '../../../src/types'; diff --git a/packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts b/packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts index 52063066d6..c655696caf 100644 --- a/packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts +++ b/packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts @@ -3,7 +3,7 @@ * * @group unit/idempotency/persistence/idempotencyRecord */ -import { IdempotencyInvalidStatusError } from '../../../src/Exceptions'; +import { IdempotencyInvalidStatusError } from '../../../src/errors'; import { IdempotencyRecord } from '../../../src/persistence'; import { IdempotencyRecordStatus } from '../../../src/types';