Skip to content

Commit

Permalink
fix: addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
manish339k committed Jun 13, 2024
1 parent 2a083ed commit e822bce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
35 changes: 25 additions & 10 deletions src/cdk/v2/destinations/intercom/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -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(
Expand All @@ -420,11 +438,8 @@ const attachContactToCompany = async (payload, endpoint, destination) => {
headers,
},
{
destType: 'intercom',
feature: 'transformation',
...commonStatTags,
endpointPath,
requestMethod: 'POST',
module: 'router',
},
);

Expand Down
8 changes: 4 additions & 4 deletions src/cdk/v2/destinations/intercom/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]}',
);
}
});
Expand Down Expand Up @@ -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"}]}',
);
}
});
Expand Down Expand Up @@ -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"}]}`,
);
}
});
Expand Down Expand Up @@ -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."}]}`,
);
}
});
Expand Down

0 comments on commit e822bce

Please sign in to comment.