Skip to content

Commit

Permalink
fix: error handling in active_campaign (#2843)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpj2292 authored Dec 1, 2023
1 parent 1a8d825 commit a015460
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 68 deletions.
53 changes: 6 additions & 47 deletions src/v0/destinations/active_campaign/transform.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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';
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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');
}
}

Expand Down
35 changes: 14 additions & 21 deletions src/v0/destinations/active_campaign/util.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit a015460

Please sign in to comment.