diff --git a/src/cdk/v2/destinations/intercom/utils.js b/src/cdk/v2/destinations/intercom/utils.js index af837e9cee..7e609d5c96 100644 --- a/src/cdk/v2/destinations/intercom/utils.js +++ b/src/cdk/v2/destinations/intercom/utils.js @@ -34,22 +34,34 @@ const { TAGS_ENDPOINT, } = require('./config'); +/** + * method to handle error during api call + * ref docs: https://developers.intercom.com/docs/references/rest-api/errors/error-codes/ + * https://developers.intercom.com/docs/references/rest-api/errors/error-objects/ + * https://developers.intercom.com/docs/references/rest-api/errors/http-responses/ + * e.g. + * 400 - code: parameter_not_found (or parameter_invalid), message: company not specified + * 401 - code: unauthorized, message: Access Token Invalid + * 404 - code: company_not_found, message: Company Not Found + * @param {*} message + * @param {*} processedResponse + */ const intercomErrorHandler = (message, processedResponse) => { - const errorMessages = JSON.stringify(processedResponse?.response?.errors); - if (processedResponse?.status === 400) { + const errorMessages = JSON.stringify(processedResponse.response); + if (processedResponse.status === 400) { throw new InstrumentationError(`${message} : ${errorMessages}`); } - if (processedResponse?.status === 401) { + if (processedResponse.status === 401) { throw new ConfigurationError(`${message} : ${errorMessages}`); } - if (processedResponse?.status === 404) { + if (processedResponse.status === 404) { throw new InstrumentationError(`${message} : ${errorMessages}`); } throw new NetworkError( `${message} : ${errorMessages}`, - processedResponse?.status, + processedResponse.status, { - [tags]: getDynamicErrorType(processedResponse?.status), + [tags]: getDynamicErrorType(processedResponse.status), }, processedResponse, ); @@ -411,6 +423,12 @@ const attachContactToCompany = async (payload, endpoint, destination) => { if (apiVersion === 'v1') { endpointPath = '/users'; } + const commonStatTags = { + destType: 'intercom', + feature: 'transformation', + requestMethod: 'POST', + module: 'router', + }; const headers = getHeaders(destination, apiVersion); const finalPayload = JSON.stringify(removeUndefinedAndNullValues(payload)); const response = await httpPOST( @@ -420,11 +438,8 @@ const attachContactToCompany = async (payload, endpoint, destination) => { headers, }, { - destType: 'intercom', - feature: 'transformation', + ...commonStatTags, endpointPath, - requestMethod: 'POST', - module: 'router', }, ); diff --git a/src/cdk/v2/destinations/intercom/utils.test.js b/src/cdk/v2/destinations/intercom/utils.test.js index 65d8bbd1c0..2f7dac2583 100644 --- a/src/cdk/v2/destinations/intercom/utils.test.js +++ b/src/cdk/v2/destinations/intercom/utils.test.js @@ -864,7 +864,7 @@ describe('attachContactToCompany utility test', () => { await attachContactToCompany(payload, endpoint, destination); } catch (error) { expect(error.message).toEqual( - 'Unable to attach Contact or User to Company due to : [{"code":"company_not_found","message":"Company Not Found"}]', + 'Unable to attach Contact or User to Company due to : {"type":"error.list","request_id":"123","errors":[{"code":"company_not_found","message":"Company Not Found"}]}', ); } }); @@ -894,7 +894,7 @@ describe('attachContactToCompany utility test', () => { 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"}]', + 'Unable to attach Contact or User to Company due to : {"type":"error.list","request_id":"123","errors":[{"code":"parameter_not_found","message":"company not specified"}]}', ); } }); @@ -974,7 +974,7 @@ 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 : {"type":"error.list","request_id":"request_401","errors":[{"code":"unauthorized","message":"Access Token Invalid"}]}`, ); } }); @@ -1009,7 +1009,7 @@ 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\":\"rate_limit_exceeded\",\"message\":\"You have exceeded the rate limit. Please try again later.\"}]`, + `Unable to Add or Update the Tag to Company due to : {"type":"error.list","request_id":"request_429","errors":[{"code":"rate_limit_exceeded","message":"You have exceeded the rate limit. Please try again later."}]}`, ); } });