Skip to content

Commit

Permalink
fix: moving networkHandler to v1 from v0
Browse files Browse the repository at this point in the history
  • Loading branch information
manish339k committed Dec 13, 2024
1 parent 568f72d commit fe97f9e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
const { RetryableError, TAG_NAMES, NetworkError } = require('@rudderstack/integrations-lib');
const isString = require('lodash/isString');
const { prepareProxyRequest, proxyRequest } = require('../../../adapters/network');
const { isHttpStatusSuccess } = require('../../util/index');
const { isHttpStatusSuccess } = require('../../../v0/util/index');
const { REFRESH_TOKEN } = require('../../../adapters/networkhandler/authConstants');

const {
processAxiosResponse,
getDynamicErrorType,
} = require('../../../adapters/utils/networkUtils');
const { TransformerProxyError } = require('../../util/errorTypes');
const tags = require('../../util/tags');
const { TransformerProxyError } = require('../../../v0/util/errorTypes');

const populateResponseWithDontBatch = (rudderJobMetadata, response) => {
const errorMessage = JSON.stringify(response);
const responseWithIndividualEvents = [];

rudderJobMetadata.forEach((metadata) => {
responseWithIndividualEvents.push({
return rudderJobMetadata.map(metadata => {
// eslint-disable-next-line no-param-reassign
metadata.dontBatch = true;
return {
statusCode: 500,
metadata: { ...metadata, dontBatch: true },
metadata,
error: errorMessage,
});
};
});
return responseWithIndividualEvents;
};

const redditRespHandler = (responseParams) => {
const { destinationResponse, destinationRequest } = responseParams;
const { destinationResponse, rudderJobMetadata } = responseParams;
const { status, response } = destinationResponse;

// to handle the case when authorization-token is invalid
Expand All @@ -49,17 +47,14 @@ const redditRespHandler = (responseParams) => {
authErrorCategory,
);
}
if (Array.isArray(destinationRequest.metadata) && destinationRequest.metadata.length > 1) {
if (status === 400 && Array.isArray(rudderJobMetadata) && rudderJobMetadata.length > 1) {
// sending back 500 for retry only when events came in a batch
const responseWithDontBatch = populateResponseWithDontBatch(
destinationRequest.metadata,
response,
);
const responseWithDontBatch = populateResponseWithDontBatch(rudderJobMetadata, response);
throw new TransformerProxyError(
`REDDIT: Error transformer proxy during REDDIT response transformation`,
500,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(500),
[TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(500),
},
destinationResponse,
'',
Expand All @@ -77,7 +72,7 @@ const redditRespHandler = (responseParams) => {
);
};
const responseHandler = (responseParams) => {
const { destinationResponse } = responseParams;
const { destinationResponse, rudderJobMetadata } = responseParams;
const message = `Request Processed Successfully`;
const { status } = destinationResponse;
if (!isHttpStatusSuccess(status)) {
Expand All @@ -90,11 +85,17 @@ const responseHandler = (responseParams) => {
? response?.invalid_events[0]?.error_message
: null;
const destResp = errorMessage || destinationResponse;
const responseWithIndividualEvents = rudderJobMetadata.map((metadata) => ({
statusCode: 200,
metadata,
error: 'success',
}));
// Mostly any error will not have a status of 2xx
return {
status,
message,
destResp,
destinationResponse: destResp,
response: responseWithIndividualEvents,
};
};
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down
12 changes: 10 additions & 2 deletions test/integrations/destinations/reddit/dataDelivery/business.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ export const testScenariosForV0API = [
status: 200,
body: {
output: {
destResp: {
destinationResponse: {
response: {
message: 'Successfully processed 1 conversion events.',
},
status: 200,
},
message: 'Request Processed Successfully',
message:
'[Generic Response Handler] Request for destination: reddit Processed Successfully',
status: 200,
},
},
Expand Down Expand Up @@ -181,8 +182,15 @@ export const testScenariosForV1API = [
body: {
output: {
message: 'Request Processed Successfully',
destinationResponse: {
response: {
message: 'Successfully processed 1 conversion events.',
},
status: 200,
},
response: [
{
error: 'success',
metadata: generateMetadata(1),
statusCode: 200,
},
Expand Down
10 changes: 4 additions & 6 deletions test/integrations/destinations/reddit/dataDelivery/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ export const v0oauthScenarios = [
status: 401,
body: {
output: {
authErrorCategory: 'REFRESH_TOKEN',
destinationResponse: {
response: 'Authorization Required',
status: 401,
},
message:
'Request failed due to Authorization Required during reddit response transformation',
statTags: expectedStatTags,
'[Generic Response Handler] Request failed for destination reddit with status: 401',
statTags: { ...expectedStatTags, errorType: 'aborted' },
status: 401,
},
},
Expand Down Expand Up @@ -121,7 +120,6 @@ export const v0oauthScenarios = [
status: 401,
body: {
output: {
authErrorCategory: 'REFRESH_TOKEN',
destinationResponse: {
response: {
success: false,
Expand All @@ -134,8 +132,8 @@ export const v0oauthScenarios = [
status: 401,
},
message:
'This server could not verify that you are authorized to access the document you requested. during reddit response transformation',
statTags: expectedStatTags,
'[Generic Response Handler] Request failed for destination reddit with status: 401',
statTags: { ...expectedStatTags, errorType: 'aborted' },
status: 401,
},
},
Expand Down

0 comments on commit fe97f9e

Please sign in to comment.