diff --git a/src/v0/destinations/active_campaign/transform.js b/src/v0/destinations/active_campaign/transform.js index e00808622d..973a928472 100644 --- a/src/v0/destinations/active_campaign/transform.js +++ b/src/v0/destinations/active_campaign/transform.js @@ -1,11 +1,7 @@ /* eslint-disable array-callback-return */ /* eslint-disable no-empty */ const get = require('get-value'); -const { - InstrumentationError, - TransformationError, - NetworkError, -} = require('@rudderstack/integrations-lib'); +const { InstrumentationError, TransformationError } = require('@rudderstack/integrations-lib'); const { EventType } = require('../../../constants'); const { CONFIG_CATEGORIES, MAPPING_CONFIG, getHeader } = require('./config'); const { @@ -17,8 +13,6 @@ const { } = require('../../util'); const { errorHandler } = require('./util'); const { httpGET, httpPOST } = require('../../../adapters/network'); -const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); -const tags = require('../../util/tags'); const TOTAL_RECORDS_KEY = 'response.data.meta.total'; const EVENT_DATA_KEY = 'properties.eventData'; @@ -72,14 +66,7 @@ const syncContact = async (contactPayload, category, destination) => { } const createdContact = get(res, 'response.data.contact'); // null safe if (!createdContact) { - throw new NetworkError( - 'Unable to Create Contact', - res.response?.status, - { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(res.response?.status), - }, - res.response, - ); + errorHandler(res.response, 'Failed to create new contact'); } return createdContact.id; }; @@ -421,14 +408,7 @@ const screenRequestHandler = async (message, category, destination) => { } if (res?.response?.status !== 200) { - throw new NetworkError( - 'Unable to create event', - res.response?.status, - { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(res.response?.status), - }, - res.response, - ); + errorHandler(res.response, 'Unable to create event'); } const storedEventsArr = res.response?.data?.eventTrackingEvents; @@ -455,14 +435,7 @@ const screenRequestHandler = async (message, category, destination) => { } if (res.response.status !== 201) { - throw new NetworkError( - 'Unable to create event', - res.response.status, - { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(res.response.status), - }, - res?.response, - ); + errorHandler(res.response, 'Unable to create event'); } } // Previous operations successfull then @@ -499,14 +472,7 @@ const trackRequestHandler = async (message, category, destination) => { } if (res.response.status !== 200) { - throw new NetworkError( - 'Unable to fetch events. Aborting', - res.response.status, - { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(res.response.status), - }, - res?.response, - ); + errorHandler(res.response, 'Unable to fetch events. Aborting'); } const storedEventsArr = res.response?.data?.eventTrackingEvents; @@ -529,14 +495,7 @@ const trackRequestHandler = async (message, category, destination) => { feature: 'transformation', }); if (res.response?.status !== 201) { - throw new NetworkError( - 'Unable to create event. Aborting', - res.response.status, - { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(res.response.status), - }, - res.response, - ); + errorHandler(res.response, 'Unable to create event. Aborting'); } } diff --git a/src/v0/destinations/active_campaign/util.js b/src/v0/destinations/active_campaign/util.js index 6e1f5a3683..457413c3e2 100644 --- a/src/v0/destinations/active_campaign/util.js +++ b/src/v0/destinations/active_campaign/util.js @@ -1,31 +1,24 @@ const { NetworkError } = require('@rudderstack/integrations-lib'); const { - nodeSysErrorToStatus, getDynamicErrorType, + processAxiosResponse, } = require('../../../adapters/utils/networkUtils'); const tags = require('../../util/tags'); -const errorHandler = (err, message) => { - if (err.response) { - throw new NetworkError( - `${message} (${err.response?.statusText},${JSON.stringify(err.response?.data)})`, - err.status, - { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(err.status), - }, - err, - ); - } else { - const httpError = nodeSysErrorToStatus(err.code); - throw new NetworkError( - `${message} ${httpError.message}`, - httpError.status, - { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(httpError.status), - }, - err, - ); +const errorHandler = (httpCallError, message) => { + const {response, status} = processAxiosResponse(httpCallError); + let msg = message; + if (response) { + msg = `${message} (${httpCallError.response?.statusText},${JSON.stringify(response)})`; } + throw new NetworkError( + msg, + status, + { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), + }, + response, + ); }; const offsetLimitVarPath = 'response.data.meta.total';