Skip to content

Commit 5a45a29

Browse files
committed
converted error type to IdempotencyRecord
1 parent 6c7e687 commit 5a45a29

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

packages/idempotency/src/IdempotencyHandler.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,11 @@ export class IdempotencyHandler<Func extends AnyFunction> {
313313
);
314314
} catch (e) {
315315
if (e instanceof IdempotencyItemAlreadyExistsError) {
316-
const idempotencyRecord: IdempotencyRecord =
317-
await this.#persistenceStore.getRecord(
318-
this.#functionPayloadToBeHashed
319-
);
316+
const idempotencyRecord: IdempotencyRecord = e.existingRecord;
317+
// const idempotencyRecord: IdempotencyRecord =
318+
// await this.#persistenceStore.getRecord(
319+
// this.#functionPayloadToBeHashed
320+
// );
320321

321322
return IdempotencyHandler.determineResultFromIdempotencyRecord(
322323
idempotencyRecord

packages/idempotency/src/errors.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import { AttributeValue } from '@aws-sdk/client-dynamodb';
2-
//import { IdempotencyRecord } from './persistence';
1+
import { IdempotencyRecord } from './persistence';
32
/**
43
* Item attempting to be inserted into persistence store already exists and is not expired
54
*/
65
class IdempotencyItemAlreadyExistsError extends Error {
7-
public existingRecord: Record<string, AttributeValue> | undefined;
6+
public existingRecord: IdempotencyRecord;
87

9-
public constructor(
10-
message: string,
11-
existingRecord: Record<string, AttributeValue> | undefined
12-
) {
8+
public constructor(message: string, existingRecord: IdempotencyRecord) {
139
super(message);
1410
this.existingRecord = existingRecord;
1511
}

packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,22 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
202202
);
203203
} catch (error) {
204204
if (error instanceof DynamoDBServiceException) {
205-
if (error.name === 'ConditionalCheckFailedException') {
206-
if (error instanceof ConditionalCheckFailedException) {
207-
throw new IdempotencyItemAlreadyExistsError(
208-
`Failed to put record for already existing idempotency key: ${record.idempotencyKey}`,
209-
error.Item
210-
);
205+
if (error instanceof ConditionalCheckFailedException) {
206+
if (!error.Item) {
207+
throw new Error('Item is undefined');
211208
}
209+
const Item = unmarshall(error.Item);
210+
throw new IdempotencyItemAlreadyExistsError(
211+
`Failed to put record for already existing idempotency key: ${record.idempotencyKey}`,
212+
new IdempotencyRecord({
213+
idempotencyKey: Item[this.keyAttr],
214+
status: Item[this.statusAttr],
215+
expiryTimestamp: Item[this.expiryAttr],
216+
inProgressExpiryTimestamp: Item[this.inProgressExpiryAttr],
217+
responseData: Item[this.dataAttr],
218+
payloadHash: Item[this.validationKeyAttr],
219+
})
220+
);
212221
}
213222
}
214223

0 commit comments

Comments
 (0)