From 8c3104bc5d471ea57af8d84dbce25409970ac4cd Mon Sep 17 00:00:00 2001 From: Yashasvi Bajpai <33063622+yashasvibajpai@users.noreply.github.com> Date: Thu, 29 Aug 2024 00:03:05 +0530 Subject: [PATCH 1/3] fix(bugsnag): reddit response coming as a non-string --- src/v0/destinations/reddit/networkHandler.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/v0/destinations/reddit/networkHandler.js b/src/v0/destinations/reddit/networkHandler.js index 55087b52ac4..d7838ce3401 100644 --- a/src/v0/destinations/reddit/networkHandler.js +++ b/src/v0/destinations/reddit/networkHandler.js @@ -1,4 +1,5 @@ const { RetryableError } = require('@rudderstack/integrations-lib'); +const isString = require('lodash/isString'); const { prepareProxyRequest, proxyRequest } = require('../../../adapters/network'); const { isHttpStatusSuccess } = require('../../util/index'); const { REFRESH_TOKEN } = require('../../../adapters/networkhandler/authConstants'); @@ -9,7 +10,7 @@ const redditRespHandler = (destResponse) => { const { status, response } = destResponse; // to handle the case when authorization-token is invalid - if (status === 401 && response.includes('Authorization Required')) { + if (status === 401 && isString(response) && response.includes('Authorization Required')) { throw new RetryableError( `Request failed due to ${response} 'during reddit response transformation'`, 500, From 78ab9d78d08e5df24d4bcae890ad5a357755ba5c Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 29 Aug 2024 18:26:09 +0530 Subject: [PATCH 2/3] fix: update error handling logic for unauthorised case - new error response handled - backward compatibility --- src/v0/destinations/reddit/networkHandler.js | 23 +++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/v0/destinations/reddit/networkHandler.js b/src/v0/destinations/reddit/networkHandler.js index d7838ce3401..d51c8b1ea13 100644 --- a/src/v0/destinations/reddit/networkHandler.js +++ b/src/v0/destinations/reddit/networkHandler.js @@ -10,12 +10,25 @@ const redditRespHandler = (destResponse) => { const { status, response } = destResponse; // to handle the case when authorization-token is invalid - if (status === 401 && isString(response) && response.includes('Authorization Required')) { + if (status === 401) { + let errorMessage = 'Authorization failed'; + let errorDetails = response; + let authErrorCategory = ''; + + if (isString(response) && response.includes('Authorization Required')) { + errorMessage = `Request failed due to ${response}`; + authErrorCategory = REFRESH_TOKEN; + } else if (response?.error?.reason === 'UNAUTHORIZED') { + errorMessage = response.error.explanation || errorMessage; + errorDetails = response.error; + authErrorCategory = REFRESH_TOKEN; + } + throw new RetryableError( - `Request failed due to ${response} 'during reddit response transformation'`, - 500, - destResponse, - REFRESH_TOKEN, + `${errorMessage} 'during reddit response transformation'`, + status, + errorDetails, + authErrorCategory, ); } }; From e775a3e7e9db1a6a445a25bbda3aed8f1ee45edd Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 29 Aug 2024 18:41:07 +0530 Subject: [PATCH 3/3] fix: existing test-cases for error handling - added new test-cases --- src/v0/destinations/reddit/networkHandler.js | 6 +- .../destinations/reddit/dataDelivery/oauth.ts | 102 ++++++++++++++++-- .../destinations/reddit/network.ts | 57 ++++++++++ 3 files changed, 154 insertions(+), 11 deletions(-) diff --git a/src/v0/destinations/reddit/networkHandler.js b/src/v0/destinations/reddit/networkHandler.js index d51c8b1ea13..e691255a26d 100644 --- a/src/v0/destinations/reddit/networkHandler.js +++ b/src/v0/destinations/reddit/networkHandler.js @@ -12,7 +12,6 @@ const redditRespHandler = (destResponse) => { // to handle the case when authorization-token is invalid if (status === 401) { let errorMessage = 'Authorization failed'; - let errorDetails = response; let authErrorCategory = ''; if (isString(response) && response.includes('Authorization Required')) { @@ -20,14 +19,13 @@ const redditRespHandler = (destResponse) => { authErrorCategory = REFRESH_TOKEN; } else if (response?.error?.reason === 'UNAUTHORIZED') { errorMessage = response.error.explanation || errorMessage; - errorDetails = response.error; authErrorCategory = REFRESH_TOKEN; } throw new RetryableError( - `${errorMessage} 'during reddit response transformation'`, + `${errorMessage} during reddit response transformation`, status, - errorDetails, + destResponse, authErrorCategory, ); } diff --git a/test/integrations/destinations/reddit/dataDelivery/oauth.ts b/test/integrations/destinations/reddit/dataDelivery/oauth.ts index 90368cd60b0..8c0d486a7a3 100644 --- a/test/integrations/destinations/reddit/dataDelivery/oauth.ts +++ b/test/integrations/destinations/reddit/dataDelivery/oauth.ts @@ -81,7 +81,7 @@ export const v0oauthScenarios = [ }, output: { response: { - status: 500, + status: 401, body: { output: { authErrorCategory: 'REFRESH_TOKEN', @@ -90,9 +90,53 @@ export const v0oauthScenarios = [ status: 401, }, message: - "Request failed due to Authorization Required 'during reddit response transformation'", + 'Request failed due to Authorization Required during reddit response transformation', statTags: expectedStatTags, - status: 500, + status: 401, + }, + }, + }, + }, + }, + { + id: 'reddit_v0_oauth_scenario_2', + name: 'reddit', + description: '[Proxy v0 API] :: Oauth where error response is an object from destination', + successCriteria: 'Should return 401 with authErrorCategory as REFRESH_TOKEN', + scenario: 'Oauth', + feature: 'dataDelivery', + module: 'destination', + version: 'v0', + input: { + request: { + body: generateProxyV0Payload({ + ...commonRequestParameters, + endpoint: 'https://ads-api.reddit.com/api/v2.0/conversions/events/a2_objResp_401', + }), + method: 'POST', + }, + }, + output: { + response: { + status: 401, + body: { + output: { + authErrorCategory: 'REFRESH_TOKEN', + destinationResponse: { + response: { + success: false, + error: { + reason: 'UNAUTHORIZED', + explanation: + 'This server could not verify that you are authorized to access the document you requested.', + }, + }, + status: 401, + }, + message: + 'This server could not verify that you are authorized to access the document you requested. during reddit response transformation', + statTags: expectedStatTags, + status: 401, }, }, }, @@ -124,21 +168,65 @@ export const v1oauthScenarios = [ }, output: { response: { - status: 500, + status: 401, body: { output: { authErrorCategory: 'REFRESH_TOKEN', message: - "Request failed due to Authorization Required 'during reddit response transformation'", + 'Request failed due to Authorization Required during reddit response transformation', response: [ { error: '"Authorization Required"', metadata: generateMetadata(1), - statusCode: 500, + statusCode: 401, + }, + ], + statTags: expectedStatTags, + status: 401, + }, + }, + }, + }, + }, + { + id: 'reddit_v1_oauth_scenario_2', + name: 'reddit', + description: '[Proxy v1 API] :: Oauth where error response is an object from destination', + successCriteria: 'Should return 401 with authErrorCategory as REFRESH_TOKEN', + scenario: 'Oauth', + feature: 'dataDelivery', + module: 'destination', + version: 'v1', + input: { + request: { + body: generateProxyV1Payload( + { + ...commonRequestParameters, + endpoint: 'https://ads-api.reddit.com/api/v2.0/conversions/events/a2_objResp_401', + }, + [generateMetadata(1)], + ), + method: 'POST', + }, + }, + output: { + response: { + status: 401, + body: { + output: { + authErrorCategory: 'REFRESH_TOKEN', + message: + 'This server could not verify that you are authorized to access the document you requested. during reddit response transformation', + response: [ + { + error: + '{"success":false,"error":{"reason":"UNAUTHORIZED","explanation":"This server could not verify that you are authorized to access the document you requested."}}', + metadata: generateMetadata(1), + statusCode: 401, }, ], statTags: expectedStatTags, - status: 500, + status: 401, }, }, }, diff --git a/test/integrations/destinations/reddit/network.ts b/test/integrations/destinations/reddit/network.ts index 7c436e8fb89..562c8e95ad8 100644 --- a/test/integrations/destinations/reddit/network.ts +++ b/test/integrations/destinations/reddit/network.ts @@ -97,4 +97,61 @@ export const networkCallsData = [ }, httpRes: { data: 'Authorization Required', status: 401, statusText: 'Unauthorized' }, }, + { + httpReq: { + url: 'https://ads-api.reddit.com/api/v2.0/conversions/events/a2_objResp_401', + data: { + events: [ + { + event_at: '2019-10-14T09:03:17.562Z', + event_type: { + tracking_type: 'ViewContent', + }, + user: { + aaid: 'c12d34889302d3c656b5699fa9190b51c50d6f62fce57e13bd56b503d66c487a', + email: 'ac144532d9e4efeab19475d9253a879173ea12a3d2238d1cb8a332a7b3a105f2', + external_id: '7b023241a3132b792a5a33915a5afb3133cbb1e13d72879689bf6504de3b036d', + ip_address: 'e80bd55a3834b7c2a34ade23c7ecb54d2a49838227080f50716151e765a619db', + user_agent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', + screen_dimensions: {}, + }, + event_metadata: { + item_count: 3, + products: [ + { + id: '123', + name: 'Monopoly', + category: 'Games', + }, + { + id: '345', + name: 'UNO', + category: 'Games', + }, + ], + }, + }, + ], + }, + params: { destination: 'reddit' }, + headers: { + Authorization: 'Bearer dummyAccessToken', + 'Content-Type': 'application/json', + }, + method: 'POST', + }, + httpRes: { + data: { + success: false, + error: { + reason: 'UNAUTHORIZED', + explanation: + 'This server could not verify that you are authorized to access the document you requested.', + }, + }, + status: 401, + statusText: 'Unauthorized', + }, + }, ];