diff --git a/src/cdk/v2/destinations/intercom/utils.js b/src/cdk/v2/destinations/intercom/utils.js index 28b21427cb..af837e9cee 100644 --- a/src/cdk/v2/destinations/intercom/utils.js +++ b/src/cdk/v2/destinations/intercom/utils.js @@ -1,6 +1,10 @@ const md5 = require('md5'); const get = require('get-value'); -const { NetworkError } = require('@rudderstack/integrations-lib'); +const { + NetworkError, + ConfigurationError, + InstrumentationError, +} = require('@rudderstack/integrations-lib'); const tags = require('../../../../v0/util/tags'); const { httpPOST } = require('../../../../adapters/network'); const { @@ -30,6 +34,27 @@ const { TAGS_ENDPOINT, } = require('./config'); +const intercomErrorHandler = (message, processedResponse) => { + const errorMessages = JSON.stringify(processedResponse?.response?.errors); + if (processedResponse?.status === 400) { + throw new InstrumentationError(`${message} : ${errorMessages}`); + } + if (processedResponse?.status === 401) { + throw new ConfigurationError(`${message} : ${errorMessages}`); + } + if (processedResponse?.status === 404) { + throw new InstrumentationError(`${message} : ${errorMessages}`); + } + throw new NetworkError( + `${message} : ${errorMessages}`, + processedResponse?.status, + { + [tags]: getDynamicErrorType(processedResponse?.status), + }, + processedResponse, + ); +}; + /** * Returns destination request headers * @param {*} destination @@ -405,16 +430,7 @@ const attachContactToCompany = async (payload, endpoint, destination) => { const processedResponse = processAxiosResponse(response); if (!isHttpStatusSuccess(processedResponse.status)) { - throw new NetworkError( - `Unable to attach Contact or User to Company due to : ${JSON.stringify( - processedResponse?.response?.errors, - )}`, - processedResponse?.status, - { - [tags]: getDynamicErrorType(processedResponse?.status), - }, - processedResponse, - ); + intercomErrorHandler('Unable to attach Contact or User to Company due to', processedResponse); } }; @@ -461,14 +477,8 @@ const addOrUpdateTagsToCompany = async (message, destination, id) => { ); const processedResponse = processAxiosResponse(response); if (!isHttpStatusSuccess(processedResponse.status)) { - throw new NetworkError( - `Unable to Add or Update the Tag to Company due to : ${JSON.stringify( - processedResponse?.response?.errors, - )}`, - processedResponse?.status, - { - [tags]: getDynamicErrorType(processedResponse?.status), - }, + intercomErrorHandler( + 'Unable to Add or Update the Tag to Company due to', processedResponse, ); } diff --git a/src/cdk/v2/destinations/intercom/utils.test.js b/src/cdk/v2/destinations/intercom/utils.test.js index d494f10c51..65d8bbd1c0 100644 --- a/src/cdk/v2/destinations/intercom/utils.test.js +++ b/src/cdk/v2/destinations/intercom/utils.test.js @@ -837,7 +837,7 @@ describe('attachContactToCompany utility test', () => { ); }); - it('Should throw a network error during attachment', async () => { + it('Should throw error for invalid company during attachment', async () => { const payload = { id: 'company123', }; @@ -868,6 +868,36 @@ describe('attachContactToCompany utility test', () => { ); } }); + + it('Should throw error for faulty payload during attachment', async () => { + const payload = {}; + const endpoint = 'https://api.intercom.io/contacts/contact123/companies'; + const destination = { Config: { apiKey: 'testApiKey', apiServer: 'us', apiVersion: 'v2' } }; + + axios.post.mockRejectedValue({ + response: { + status: 400, + data: { + type: 'error.list', + request_id: '123', + errors: [ + { + code: 'parameter_not_found', + message: 'company not specified', + }, + ], + }, + }, + }); + + try { + await attachContactToCompany(payload, endpoint, destination); + } catch (error) { + expect(error.message).toEqual( + 'Unable to attach Contact or User to Company due to : [{"code":"parameter_not_found","message":"company not specified"}]', + ); + } + }); }); describe('addOrUpdateTagsToCompany utility test', () => { @@ -926,10 +956,10 @@ describe('addOrUpdateTagsToCompany utility test', () => { const id = 'companyId'; axios.post.mockRejectedValue({ - status: 400, + status: 401, data: { type: 'error.list', - request_id: 'request_400', + request_id: 'request_401', errors: [ { code: 'unauthorized', @@ -944,7 +974,42 @@ describe('addOrUpdateTagsToCompany utility test', () => { await addOrUpdateTagsToCompany(message, destination, id); } catch (error) { expect(error.message).toEqual( - `Unable to Add or Update the Tag to Company due to : [{"code":"unauthorized","message":"Access Token Invalid"}]`, + `Unable to Add or Update the Tag to Company due to : [{\"code\":\"unauthorized\",\"message\":\"Access Token Invalid\"}]`, + ); + } + }); + + it('Should throw a network error in case if axios calls returns an error', async () => { + const message = { + context: { + traits: { + tags: ['tag1'], + }, + }, + }; + const destination = { Config: { apiKey: 'testApiKey', apiServer: 'us' } }; + const id = 'companyId'; + + axios.post.mockRejectedValue({ + status: 429, + data: { + type: 'error.list', + request_id: 'request_429', + errors: [ + { + code: 'rate_limit_exceeded', + message: 'You have exceeded the rate limit. Please try again later.', + }, + ], + }, + }); + + try { + axios.post.mockClear(); + await addOrUpdateTagsToCompany(message, destination, id); + } catch (error) { + expect(error.message).toEqual( + `Unable to Add or Update the Tag to Company due to : [{\"code\":\"rate_limit_exceeded\",\"message\":\"You have exceeded the rate limit. Please try again later.\"}]`, ); } });