From b1f78c3a0f2275d5ad45fe339fbc91efd85c2572 Mon Sep 17 00:00:00 2001 From: Warren Parad Date: Wed, 27 Nov 2024 17:44:32 +0100 Subject: [PATCH] Prevent returning a full object when that object exceeds the max DDB size, because it will get passed to the caller and that will cause unnecessary extra complexity for the caller. --- src/dynamoDbSafe.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/dynamoDbSafe.js b/src/dynamoDbSafe.js index 57d7b39..c59f1f6 100644 --- a/src/dynamoDbSafe.js +++ b/src/dynamoDbSafe.js @@ -116,7 +116,12 @@ class DynamoDB extends DynamoDbOriginal.DocumentClient { const capturedStack = { name: 'DynamoDB.update() Error:' }; Error.captureStackTrace(capturedStack); const resultAsync = super.put(params).promise().catch(error => { - const wrappedError = new DynamoDbError({ message: error.message, method: 'Put', parameters: originalParams, dynamoDbStack: error.stack }, error.code); + // Do not log items that are too bit, it will just become a huge problem for the caller. + const loggedParameters = error.code === 'ValidationException' && error.message === 'Item size has exceeded the maximum allowed size' + ? { calculatedItemSize: JSON.stringify(originalParams).length } + : originalParams + + const wrappedError = new DynamoDbError({ message: error.message, method: 'Put', parameters: loggedParameters, dynamoDbStack: error.stack }, error.code); wrappedError.stack = capturedStack; throw wrappedError; }); @@ -159,7 +164,12 @@ class DynamoDB extends DynamoDbOriginal.DocumentClient { const capturedStack = { name: 'DynamoDB.update() Error:' }; Error.captureStackTrace(capturedStack); const resultAsync = super.update(params).promise().catch(error => { - const wrappedError = new DynamoDbError({ message: error.message, method: 'Update', parameters: originalParams, dynamoDbStack: error.stack }, error.code); + // Do not log items that are too bit, it will just become a huge problem for the caller. + const loggedParameters = error.code === 'ValidationException' && error.message === 'Item size has exceeded the maximum allowed size' + ? { calculatedItemSize: JSON.stringify(originalParams).length } + : originalParams + + const wrappedError = new DynamoDbError({ message: error.message, method: 'Update', parameters: loggedParameters, dynamoDbStack: error.stack }, error.code); wrappedError.stack = capturedStack; throw wrappedError; });