Skip to content

Commit

Permalink
fix: fixing network handler proxy v1
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Mar 20, 2024
1 parent 5e2be9c commit 51fe474
Showing 1 changed file with 60 additions and 64 deletions.
124 changes: 60 additions & 64 deletions src/v1/destinations/linkedin_ads/networkHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-restricted-syntax */
const { TransformerProxyError } = require('../../../v0/util/errorTypes');
const { prepareProxyRequest, proxyRequest } = require('../../../adapters/network');
const { isHttpStatusSuccess, getAuthErrCategoryFromStCode } = require('../../../v0/util/index');
Expand All @@ -10,30 +8,6 @@ const {
} = require('../../../adapters/utils/networkUtils');
const tags = require('../../../v0/util/tags');

function isEventAbortableAndExtractErrMsg(element, proxyOutputObj) {
let isAbortable = false;
let errorMsg = '';
// success event
if (!element.errors) {
return isAbortable;
}
for (const err of element.errors) {
errorMsg += `${err.message}, `;
// if code is any of these, event is not retryable
if (
err.code === 'PERMISSION_DENIED' ||
err.code === 'INVALID_ARGUMENT' ||
err.code === 'NOT_FOUND'
) {
isAbortable = true;
}
}
if (errorMsg) {
proxyOutputObj.error = errorMsg;
}
return isAbortable;
}

function constructPartialStatus(errorMessage) {
const errorPattern = /Index: (\d+), ERROR :: (.*?)\n/g;

Check warning on line 12 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L11-L12

Added lines #L11 - L12 were not covered by tests
let match;
Expand All @@ -49,57 +23,79 @@ function constructPartialStatus(errorMessage) {
return errorMap;

Check warning on line 23 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L23

Added line #L23 was not covered by tests
}

function createResponseArray(metadata, partialStatus) {

Check warning on line 26 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L26

Added line #L26 was not covered by tests
// Convert destPartialStatus to an object for easier lookup
const errorMap = partialStatus.reduce((acc, [index, message]) => {
const jobId = metadata[index]?.jobId; // Get the jobId from the metadata array based on the index

Check warning on line 29 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L28-L29

Added lines #L28 - L29 were not covered by tests
if (jobId !== undefined) {
acc[jobId] = message;

Check warning on line 31 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L31

Added line #L31 was not covered by tests
}
return acc;

Check warning on line 33 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L33

Added line #L33 was not covered by tests
}, {});

return metadata.map((item) => {
const error = errorMap[item.jobId];
return {
statusCode: error ? 400 : 500,

Check warning on line 39 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L36-L39

Added lines #L36 - L39 were not covered by tests
metadata: item,
error: error || 'success',

Check warning on line 41 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L41

Added line #L41 was not covered by tests
};
});
}

// eslint-disable-next-line consistent-return
const responseHandler = (responseParams) => {
const { destinationResponse, rudderJobMetadata } = responseParams;
const message = `[LINKEDIN_CONVERSION_API Response V1 Handler] - Request Processed Successfully`;
const responseWithIndividualEvents = [];
let responseWithIndividualEvents = [];
const { response, status } = destinationResponse;

Check warning on line 51 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L48-L51

Added lines #L48 - L51 were not covered by tests

// even if a single event is unsuccessful, the entire batch will fail, we will filter that event out and retry others
if (!isHttpStatusSuccess(status)) {
// check for Partial Event failures and Successes
const destPartialStatus = constructPartialStatus(response.error?.message);

for (const [idx, element] of destPartialStatus.entries()) {
const proxyOutputObj = {
statusCode: 500,
metadata: rudderJobMetadata[idx],
error: 'success',
if (status === 401 || status === 403) {
const errorMessage = response.error?.message || 'unknown error format';
responseWithIndividualEvents = rudderJobMetadata.map((metadata) => ({

Check warning on line 57 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L57

Added line #L57 was not covered by tests
statusCode: status,
metadata,
error: errorMessage,
}));
throw new TransformerProxyError(

Check warning on line 62 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L62

Added line #L62 was not covered by tests
`LinkedIn Conversion API: Error transformer proxy v1 during LinkedIn Conversion API response transformation`,
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
},
destinationResponse,
getAuthErrCategoryFromStCode(status),
responseWithIndividualEvents,
);
}
// if the status is 422, we need to parse the error message and construct the response array
if (status === 422) {
const destPartialStatus = constructPartialStatus(response.error?.message);
responseWithIndividualEvents = [...createResponseArray(rudderJobMetadata, destPartialStatus)];
return {

Check warning on line 77 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L75-L77

Added lines #L75 - L77 were not covered by tests
status,
message,
destinationResponse,
response: responseWithIndividualEvents,
};
// update status of partial event if abortable
if (isEventAbortableAndExtractErrMsg(element, proxyOutputObj)) {
proxyOutputObj.statusCode = 400;
}
responseWithIndividualEvents.push(proxyOutputObj);
}

return {
status,
message,
destinationResponse,
response: responseWithIndividualEvents,
};
}

// in case of failure status, populate response to maintain len(metadata)=len(response)
const errorMessage = response.error?.message || 'unknown error format';
for (const metadata of rudderJobMetadata) {
responseWithIndividualEvents.push({
statusCode: status,
metadata,
error: errorMessage,
});
}
// otherwise all events are successful
responseWithIndividualEvents = rudderJobMetadata.map((metadata) => ({

Check warning on line 87 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L87

Added line #L87 was not covered by tests
statusCode: 200,
metadata,
error: 'success',
}));

throw new TransformerProxyError(
`Campaign Manager: Error transformer proxy v1 during LINKED_IN_CONVERSION_API response transformation`,
return {

Check warning on line 93 in src/v1/destinations/linkedin_ads/networkHandler.js

View check run for this annotation

Codecov / codecov/patch

src/v1/destinations/linkedin_ads/networkHandler.js#L93

Added line #L93 was not covered by tests
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
},
message,
destinationResponse,
getAuthErrCategoryFromStCode(status),
responseWithIndividualEvents,
);
response: responseWithIndividualEvents,
};
};

function networkHandler() {
Expand Down

0 comments on commit 51fe474

Please sign in to comment.