@@ -55,20 +55,19 @@ const base64HashedRegex = /^[A-Za-z0-9+/]*={1,2}$/i
5555const validEmailRegex = / ^ \S + @ \S + \. \S + $ / i
5656
5757export async function processPayload ( input : ProcessPayloadInput ) : Promise < MultiStatusResponse > {
58- let crmID
5958 if ( ! input . payloads [ 0 ] . external_id ) {
6059 throw new PayloadValidationError ( `No external_id found in payload.` )
61- } else {
62- crmID = input . payloads [ 0 ] . external_id
6360 }
6461
62+ const crmID = input . payloads [ 0 ] . external_id
63+
6564 const multiStatusResponse = new MultiStatusResponse ( )
6665
67- const payload = input . payloads
68- payload . forEach ( ( _ , index ) => {
66+ // Initially mark all payloads as successful
67+ input . payloads . forEach ( ( payload , index ) => {
6968 multiStatusResponse . setSuccessResponseAtIndex ( index , {
7069 status : 200 ,
71- sent : { ...payload [ index ] } ,
70+ sent : { ...payload } ,
7271 body : 'Successfully Uploaded to S3'
7372 } )
7473 } )
@@ -104,14 +103,12 @@ export async function processPayload(input: ProcessPayloadInput): Promise<MultiS
104103
105104 return multiStatusResponse
106105 } catch ( error ) {
107- payload . forEach ( ( _ , i ) => {
106+ input . payloads . forEach ( ( payload , i ) => {
108107 if ( multiStatusResponse . isSuccessResponseAtIndex ( i ) ) {
109108 multiStatusResponse . setErrorResponseAtIndex ( i , {
110109 status : 500 ,
111110 errortype : 'RETRYABLE_ERROR' ,
112- errormessage : `Failed to upload to The Trade Desk Drop Endpoint: ${ ( error as Error ) . message } ` ,
113- sent : { ...input . payloads [ i ] } ,
114- body : `The Trade Desk Drop Endpoint upload failed: ${ ( error as Error ) . message } `
111+ errormessage : `Failed to upload to The Trade Desk Drop Endpoint: ${ ( error as Error ) . message } `
115112 } )
116113 }
117114 } )
@@ -137,18 +134,25 @@ export async function processPayload(input: ProcessPayloadInput): Promise<MultiS
137134 }
138135 } )
139136 } catch ( error ) {
137+ // Set the default error to Bad Request
138+ let httpStatusCode = 400
139+
140+ // If this is an AWS error, extract the status code
141+ if ( error ?. $metadata ?. httpStatusCode ) {
142+ httpStatusCode = error . $metadata . httpStatusCode
143+ }
144+
140145 // Mark all remaining success payloads as failed if AWS upload fails
141- payload . forEach ( ( _ , index ) => {
142- if ( multiStatusResponse . isSuccessResponseAtIndex ( index ) ) {
143- multiStatusResponse . setErrorResponseAtIndex ( index , {
144- status : 500 ,
145- errortype : 'RETRYABLE_ERROR' ,
146- errormessage : `Failed to upload to AWS: ${ ( error as Error ) . message } ` ,
147- sent : { ...payload [ index ] } ,
148- body : `AWS upload failed: ${ ( error as Error ) . message } `
149- } )
146+ for ( let i = 0 ; i < input . payloads . length ; i ++ ) {
147+ if ( multiStatusResponse . isErrorResponseAtIndex ( i ) ) {
148+ continue
150149 }
151- } )
150+
151+ multiStatusResponse . setErrorResponseAtIndex ( i , {
152+ status : httpStatusCode ,
153+ errormessage : `Failed to upload payload to Integrations Outbound Controller`
154+ } )
155+ }
152156 }
153157 return multiStatusResponse
154158 }
@@ -163,9 +167,7 @@ function extractUsers(payloads: Payload[], multiStatusResponse: MultiStatusRespo
163167 multiStatusResponse . setErrorResponseAtIndex ( index , {
164168 status : 400 ,
165169 errortype : 'PAYLOAD_VALIDATION_FAILED' ,
166- errormessage : `Invalid email: ${ payload . email } ` ,
167- sent : { ...payload } ,
168- body : `Invalid email: ${ payload . email } `
170+ errormessage : `Invalid email: ${ payload . email } `
169171 } )
170172 return
171173 }
0 commit comments