From 157a9ab0af2d063f503378abffe6693de643be29 Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Mon, 1 Jul 2024 15:53:02 +0530 Subject: [PATCH] chore: refactor code --- src/v0/destinations/drip/util.js | 58 ++++++++++++------------- src/v0/destinations/trengo/transform.js | 1 - 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/v0/destinations/drip/util.js b/src/v0/destinations/drip/util.js index a989ba59e2..d3e6760f32 100644 --- a/src/v0/destinations/drip/util.js +++ b/src/v0/destinations/drip/util.js @@ -54,11 +54,11 @@ const userExists = async (Config, id, metadata) => { ); } - const error = httpResponse.response.response; + const error = httpResponse.response?.response; let errMsg = ''; let errStatus = 400; - if (httpResponse.response.response.status) { - errStatus = httpResponse.response.response.status || 400; + if (error) { + errStatus = error.status || 400; errMsg = error.data ? JSON.stringify(error.data) : 'error response not found'; } throw new NetworkError(`Error occurred while checking user : ${errMsg}`, errStatus, { @@ -67,39 +67,39 @@ const userExists = async (Config, id, metadata) => { }; const createUpdateUser = async (finalpayload, Config, basicAuth, metadata) => { - try { - const { httpResponse } = await handleHttpRequest( - 'post', - `${ENDPOINT}/v2/${Config.accountId}/subscribers`, - finalpayload, - { - headers: { - Authorization: `Basic ${basicAuth}`, - 'Content-Type': JSON_MIME_TYPE, - }, - }, - { - metadata, - destType: 'drip', - feature: 'transformation', - requestMethod: 'POST', - endpointPath: '/subscribers', - module: 'router', + const { httpResponse } = await handleHttpRequest( + 'post', + `${ENDPOINT}/v2/${Config.accountId}/subscribers`, + finalpayload, + { + headers: { + Authorization: `Basic ${basicAuth}`, + 'Content-Type': JSON_MIME_TYPE, }, - ); + }, + { + metadata, + destType: 'drip', + feature: 'transformation', + requestMethod: 'POST', + endpointPath: '/subscribers', + module: 'router', + }, + ); + if (httpResponse.success) { const { response } = httpResponse; if (response) { return response.status === 200 || response.status === 201; } throw new AbortedError('Invalid response.'); - } catch ({ destinationResponse }) { - const error = destinationResponse; - let errMsg = ''; - if (error.response && error.response.data) { - errMsg = JSON.stringify(error.response.data); - } - throw new AbortedError(`Error occurred while creating or updating user : ${errMsg}`); } + + const error = httpResponse.response; + let errMsg = ''; + if (error.response && error.response.data) { + errMsg = JSON.stringify(error.response.data); + } + throw new AbortedError(`Error occurred while creating or updating user : ${errMsg}`); }; const createList = (productList) => { diff --git a/src/v0/destinations/trengo/transform.js b/src/v0/destinations/trengo/transform.js index a1232e9ce2..36edadbe8a 100644 --- a/src/v0/destinations/trengo/transform.js +++ b/src/v0/destinations/trengo/transform.js @@ -103,7 +103,6 @@ const lookupContact = async (term, destination, metadata) => { throw new NetworkError( `Inside lookupContact, failed to make request: ${httpResponse.response?.response?.statusText}`, status, - { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), },