From b48a145e7ff31f27d1b1a3e3214c7e1c699188b1 Mon Sep 17 00:00:00 2001 From: Sayan Das Date: Wed, 2 Oct 2024 13:38:52 +0530 Subject: [PATCH] Improved error handling --- .../src/destinations/braze/utils.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/destination-actions/src/destinations/braze/utils.ts b/packages/destination-actions/src/destinations/braze/utils.ts index 39a0a91485..77dc135a12 100644 --- a/packages/destination-actions/src/destinations/braze/utils.ts +++ b/packages/destination-actions/src/destinations/braze/utils.ts @@ -566,6 +566,18 @@ async function handleBrazeAPIResponse( } } catch (error) { if (error instanceof HTTPError) { + const errorResponse = error.response as ModifiedResponse + + // Iterate over the errors reported by Braze and store them at the original payload index + const parsedErrors: Set[] = new Array(payloads.length).fill(new Set()) + + if (errorResponse.data.errors && Array.isArray(errorResponse.data.errors)) { + errorResponse.data.errors.forEach((error) => { + const indexInOriginalPayload = validPayloadIndicesBitmap[error.index] + parsedErrors[indexInOriginalPayload].add(error.type) + }) + } + for (let i = 0; i < multiStatusResponse.length(); i++) { // Skip if the index is already marked as an error in pre-validation if (multiStatusResponse.isErrorResponseAtIndex(i)) { @@ -576,7 +588,10 @@ async function handleBrazeAPIResponse( multiStatusResponse.setErrorResponseAtIndex(i, { status: error.response.status, errortype: 'PAYLOAD_VALIDATION_FAILED', - errormessage: (error?.response as ModifiedResponse)?.data?.message ?? error.message + errormessage: + (error?.response as ModifiedResponse)?.data?.message ?? error.message, + sent: payloads[i], + body: parsedErrors[i].size > 0 ? Array.from(parsedErrors[i]).join(', ') : undefined }) } } else {