diff --git a/src/v0/destinations/marketo/util.js b/src/v0/destinations/marketo/util.js index ab39aafc15..0de14e2072 100644 --- a/src/v0/destinations/marketo/util.js +++ b/src/v0/destinations/marketo/util.js @@ -19,6 +19,8 @@ const tags = require('../../util/tags'); * https://developers.marketo.com/rest-api/error-codes/ */ +const ERROR_CODE_TO_PASS = ['1015']; + const MARKETO_RETRYABLE_CODES = ['601', '602', '604', '611']; const MARKETO_ABORTABLE_CODES = [ '600', @@ -34,29 +36,28 @@ const MARKETO_ABORTABLE_CODES = [ ]; const MARKETO_THROTTLED_CODES = ['502', '606', '607', '608', '615']; -const RECORD_LEVEL_ABORTBALE_ERRORS = [ - '1001', - '1002', - '1003', - '1004', - '1005', - '1006', - '1007', - '1008', - '1011', - '1013', - '1014', - '1015', - '1016', - '1017', - '1018', - '1021', - '1026', - '1027', - '1028', - '1036', - '1049', -]; +// Keeping here for reference const RECORD_LEVEL_ABORTBALE_ERRORS = [ +// '1001', +// '1002', +// '1003', +// '1004', +// '1005', +// '1006', +// '1007', +// '1008', +// '1011', +// '1013', +// '1014', +// '1016', +// '1017', +// '1018', +// '1021', +// '1026', +// '1027', +// '1028', +// '1036', +// '1049', +// ]; const { DESTINATION } = require('./config'); const logger = require('../../../logger'); @@ -86,7 +87,7 @@ const marketoApplicationErrorHandler = (marketoResponse, sourceMessage, destinat }; /** * this function checks the status of individual responses and throws error if any - * response ststus does not match the expected status + * response status does not match the expected status * doc1: https://developers.marketo.com/rest-api/lead-database/custom-objects/#create_and_update * doc2: https://developers.marketo.com/rest-api/lead-database/#create_and_update * Structure of marketoResponse: { @@ -122,20 +123,32 @@ const marketoApplicationErrorHandler = (marketoResponse, sourceMessage, destinat const nestedResponseHandler = (marketoResponse, sourceMessage) => { const checkStatus = (res) => { const { status } = res; - if (status && status !== 'updated' && status !== 'created' && status !== 'added') { + const allowedStatus = ['updated', 'added', 'removed', 'created']; + if ( + status && + !allowedStatus.includes(status) + // we need to check the case where the id are not in list + ) { const { reasons } = res; let statusCode = 400; - if (reasons && RECORD_LEVEL_ABORTBALE_ERRORS.includes(reasons[0].code)) { - statusCode = 400; - } else if (reasons && MARKETO_ABORTABLE_CODES.includes(reasons[0].code)) { - statusCode = 400; - } else if (reasons && MARKETO_THROTTLED_CODES.includes(reasons[0].code)) { - statusCode = 429; - } else if (reasons && MARKETO_RETRYABLE_CODES.includes(reasons[0].code)) { - statusCode = 500; + if (reasons) { + const errorCodesFromDest = reasons.map((reason) => reason.code); + const filteredErrorCode = errorCodesFromDest.find( + (errorCode) => !ERROR_CODE_TO_PASS.includes(errorCode), + ); + if (!filteredErrorCode) { + return; + } + if (MARKETO_THROTTLED_CODES.includes(filteredErrorCode)) { + statusCode = 429; + } else if (MARKETO_RETRYABLE_CODES.includes(filteredErrorCode)) { + statusCode = 500; + } } throw new InstrumentationError( - `Request failed during: ${sourceMessage}, error: ${JSON.stringify(reasons)}`, + `Request failed during: ${sourceMessage}, error: ${ + JSON.stringify(reasons) || 'Reason(s) not Found' + }`, statusCode, { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(statusCode), diff --git a/test/__mocks__/data/marketo_static_list/proxy_response.json b/test/__mocks__/data/marketo_static_list/proxy_response.json index 5c00ced941..2a58d99da2 100644 --- a/test/__mocks__/data/marketo_static_list/proxy_response.json +++ b/test/__mocks__/data/marketo_static_list/proxy_response.json @@ -1,4 +1,31 @@ { + "https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=110&id=111&id=112": { + "data": { + "requestId": "b6d1#18a8d2c10e7", + "result": [ + { + "id": 110, + "status": "skipped", + "reasons": [ + { + "code": "1015", + "message": "Lead not in list" + } + ] + }, + { + "id": 111, + "status": "removed" + }, + { + "id": 112, + "status": "removed" + } + ], + "success": true + }, + "status": 200 + }, "https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=1&id=2&id=3": { "data": { "requestId": "68d8#1846058ee27", @@ -60,14 +87,13 @@ "status": "skipped", "reasons": [ { - "code": "1004", - "message": "Lead not found" + "code": "1015", + "message": "Lead not in list" } ] }, - "success": true }, "status": 200 } -} +} \ No newline at end of file diff --git a/test/__tests__/data/marketo_static_list_proxy_input.json b/test/__tests__/data/marketo_static_list_proxy_input.json index 85142d68d7..6f84e7416d 100644 --- a/test/__tests__/data/marketo_static_list_proxy_input.json +++ b/test/__tests__/data/marketo_static_list_proxy_input.json @@ -1,4 +1,24 @@ [ + { + "type": "REST", + "endpoint": "https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=110&id=111&id=112", + "method": "POST", + "userId": "", + "headers": { + "Authorization": "Bearer Incorrect_token", + "Content-Type": "application/json" + }, + "body": { + "FORM": {}, + "JSON": {}, + "JSON_ARRAY": {}, + "XML": {} + }, + "files": {}, + "params": { + "destination": "marketo_static_list" + } + }, { "type": "REST", "endpoint": "https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=1&id=2&id=3", diff --git a/test/__tests__/data/marketo_static_list_proxy_output.json b/test/__tests__/data/marketo_static_list_proxy_output.json index 2dfbee0e8e..8ac482b5be 100644 --- a/test/__tests__/data/marketo_static_list_proxy_output.json +++ b/test/__tests__/data/marketo_static_list_proxy_output.json @@ -1,4 +1,37 @@ [ + { + "output": { + "message": "Request Processed Successfully", + "destinationResponse": { + "response": { + "requestId": "b6d1#18a8d2c10e7", + "result": [ + { + "id": 110, + "status": "skipped", + "reasons": [ + { + "code": "1015", + "message": "Lead not in list" + } + ] + }, + { + "id": 111, + "status": "removed" + }, + { + "id": 112, + "status": "removed" + } + ], + "success": true + }, + "status": 200 + }, + "status": 200 + } + }, { "output": { "status": 500, @@ -72,21 +105,25 @@ }, { "output": { - "destinationResponse": "", - "message": "Request failed during: during Marketo Static List Response Handling, error: [{\"code\":\"1004\",\"message\":\"Lead not found\"}]", - "statTags": { - "destType": "MARKETO_STATIC_LIST", - "errorCategory": "dataValidation", - "destinationId": "Non-determininable", - "workspaceId": "Non-determininable", - "errorType": "instrumentation", - "feature": "dataDelivery", - "implementation": "native", - "destinationId": "Non-determininable", - "workspaceId": "Non-determininable", - "module": "destination" - }, - "status": 400 + "status": 200, + "message": "Request Processed Successfully", + "destinationResponse": { + "response": { + "requestId": "12d3c#1846057dce2", + "result": { + "id": 5, + "status": "skipped", + "reasons": [ + { + "code": "1015", + "message": "Lead not in list" + } + ] + }, + "success": true + }, + "status": 200 + } } } ] \ No newline at end of file