diff --git a/package-lock.json b/package-lock.json index e690e650c9..7fbf47bab5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,7 @@ "@koa/router": "^12.0.0", "@ndhoule/extend": "^2.0.0", "@pyroscope/nodejs": "^0.2.6", - "@rudderstack/integrations-lib": "^0.1.8", + "@rudderstack/integrations-lib": "^0.2.2", "@rudderstack/workflow-engine": "^0.6.9", "ajv": "^8.12.0", "ajv-draft-04": "^1.0.0", @@ -114,6 +114,41 @@ "typescript": "^5.0.4" } }, + "../rudder-integrations-lib": { + "name": "@rudderstack/integrations-lib", + "version": "0.1.10", + "extraneous": true, + "license": "MIT", + "dependencies": { + "@rudderstack/workflow-engine": "^0.5.7", + "axios": "^1.4.0", + "axios-mock-adapter": "^1.22.0", + "crypto": "^1.0.1", + "get-value": "^3.0.1", + "handlebars": "^4.7.8", + "lodash": "^4.17.21", + "moment": "^2.29.4", + "moment-timezone": "^0.5.43", + "set-value": "^4.1.0", + "sha256": "^0.2.0", + "tslib": "^2.4.0", + "winston": "^3.11.0" + }, + "devDependencies": { + "@types/get-value": "^3.0.3", + "@types/jest": "^29.5.4", + "@types/lodash": "^4.14.195", + "@types/node": "^20.3.3", + "@types/set-value": "^4.0.1", + "@types/sha256": "^0.2.0", + "jest": "^29.4.3", + "pre-commit": "^1.2.2", + "prettier": "^2.8.4", + "ts-jest": "^29.0.5", + "ts-node": "^10.9.1", + "typescript": "^5.1.6" + } + }, "node_modules/@aashutoshrathi/word-wrap": { "version": "1.2.6", "resolved": "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz", @@ -4432,9 +4467,9 @@ } }, "node_modules/@rudderstack/integrations-lib": { - "version": "0.1.9", - "resolved": "https://registry.npmjs.org/@rudderstack/integrations-lib/-/integrations-lib-0.1.9.tgz", - "integrity": "sha512-ROi/LfI7PXqKDrjSig+1Rf2TQ8MgxJGJ7sAD1B0PmRKELQpxK6PLt8QF+vKXl8wYILQu2gwTkZ5o+uwmNKxGzg==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@rudderstack/integrations-lib/-/integrations-lib-0.2.2.tgz", + "integrity": "sha512-LilQsYcYh/4XXHNmYHM164fCbO5U3uvlw7k1wiCvFOR0MS1RhFXD9sPgCYpri683Jy3gqq1FrKN1EFj7oWAMjw==", "dependencies": { "@rudderstack/workflow-engine": "^0.5.7", "axios": "^1.4.0", @@ -4447,7 +4482,8 @@ "moment-timezone": "^0.5.43", "set-value": "^4.1.0", "sha256": "^0.2.0", - "tslib": "^2.4.0" + "tslib": "^2.4.0", + "winston": "^3.11.0" } }, "node_modules/@rudderstack/integrations-lib/node_modules/@aws-crypto/sha256-js": { diff --git a/package.json b/package.json index 98640af1e6..13aaecb000 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "@koa/router": "^12.0.0", "@ndhoule/extend": "^2.0.0", "@pyroscope/nodejs": "^0.2.6", - "@rudderstack/integrations-lib": "^0.1.8", + "@rudderstack/integrations-lib": "^0.2.2", "@rudderstack/workflow-engine": "^0.6.9", "ajv": "^8.12.0", "ajv-draft-04": "^1.0.0", diff --git a/src/util/error-extractor/index.ts b/src/util/error-extractor/index.ts index b8cfef4136..68ebac9aca 100644 --- a/src/util/error-extractor/index.ts +++ b/src/util/error-extractor/index.ts @@ -1,14 +1,17 @@ /* eslint-disable max-classes-per-file */ -import { MessageDetails, StatusCode } from "./types"; +import { MessageDetails, StatusCode, Stat } from "./types"; export class ErrorDetailsExtractor { status: StatusCode; messageDetails: MessageDetails; + stat : Stat + constructor (builder: ErrorDetailsExtractorBuilder) { this.status = builder.getStatus(); this.messageDetails = builder.getMessageDetails(); + this.stat = builder.getStat(); } } @@ -18,9 +21,11 @@ export class ErrorDetailsExtractorBuilder { messageDetails: MessageDetails; + stat: Stat; constructor() { this.status = 0; this.messageDetails = {}; + this.stat = {}; } setStatus(status: number): ErrorDetailsExtractorBuilder { @@ -28,6 +33,11 @@ export class ErrorDetailsExtractorBuilder { return this; } + setStat(stat: Record): ErrorDetailsExtractorBuilder { + this.stat = stat + return this; + } + /** * This means we need to set a message from a specific field that we see from the destination's response * @@ -69,6 +79,10 @@ export class ErrorDetailsExtractorBuilder { getStatus(): number { return this.status; } + + getStat(): Record { + return this.stat; + } getMessageDetails(): Record { return this.messageDetails; diff --git a/src/util/error-extractor/types.ts b/src/util/error-extractor/types.ts index 7c500177cb..ff7290b4ff 100644 --- a/src/util/error-extractor/types.ts +++ b/src/util/error-extractor/types.ts @@ -1,2 +1,3 @@ export type MessageDetails = Record; -export type StatusCode = number; \ No newline at end of file +export type StatusCode = number; +export type Stat = Record \ No newline at end of file diff --git a/src/v0/util/facebookUtils/networkHandler.js b/src/v0/util/facebookUtils/networkHandler.js index e0d69fa5c8..7219120dd7 100644 --- a/src/v0/util/facebookUtils/networkHandler.js +++ b/src/v0/util/facebookUtils/networkHandler.js @@ -1,12 +1,18 @@ const { isEmpty } = require('lodash'); const get = require('get-value'); -const { NetworkError } = require('@rudderstack/integrations-lib'); +const { + NetworkError, + ConfigurationAuthError, + isDefinedAndNotNull, + ERROR_TYPES, + TAG_NAMES, + METADATA, +} = require('@rudderstack/integrations-lib'); const { processAxiosResponse, getDynamicErrorType, } = require('../../../adapters/utils/networkUtils'); const { prepareProxyRequest, proxyRequest } = require('../../../adapters/network'); -const tags = require('../tags'); const { ErrorDetailsExtractorBuilder } = require('../../../util/error-extractor'); /** @@ -100,12 +106,26 @@ const errorDetailsMap = { 190: { 460: new ErrorDetailsExtractorBuilder() .setStatus(400) + .setStat({ + [TAG_NAMES.ERROR_TYPE]: ERROR_TYPES.AUTH, + }) .setMessage( 'The session has been invalidated because the user changed their password or Facebook has changed the session for security reasons', ) .build(), + + 463: new ErrorDetailsExtractorBuilder() + .setStatus(400) + .setStat({ + [TAG_NAMES.ERROR_TYPE]: ERROR_TYPES.AUTH, + }) + .setMessageField('message') + .build(), default: new ErrorDetailsExtractorBuilder() .setStatus(400) + .setStat({ + [TAG_NAMES.ERROR_TYPE]: ERROR_TYPES.AUTH, + }) .setMessage('Invalid OAuth 2.0 access token') .build(), }, @@ -217,7 +237,7 @@ const getStatus = (error) => { // Unhandled error response return { status: errorStatus, - tags: { [tags.TAG_NAMES.META]: tags.METADATA.UNHANDLED_STATUS_CODE }, + stats: { [TAG_NAMES.META]: METADATA.UNHANDLED_STATUS_CODE }, }; } errorStatus = errorDetail.status; @@ -227,7 +247,7 @@ const getStatus = (error) => { errorMessage = get(error, errorDetail?.messageDetails?.field); } - return { status: errorStatus, errorMessage }; + return { status: errorStatus, errorMessage, stats: errorDetail?.stat }; }; const errorResponseHandler = (destResponse) => { @@ -237,13 +257,19 @@ const errorResponseHandler = (destResponse) => { return; } const { error } = response; - const { status, errorMessage, tags: errorStatTags } = getStatus(error); + const { status, errorMessage, stats: errorStatTags } = getStatus(error); + if ( + isDefinedAndNotNull(errorStatTags) && + errorStatTags?.[TAG_NAMES.ERROR_TYPE] === ERROR_TYPES.AUTH + ) { + throw new ConfigurationAuthError(errorMessage, { ...response, status: destResponse.status }); + } throw new NetworkError( `${errorMessage || error.message || 'Unknown failure during response transformation'}`, status, { ...errorStatTags, - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), + [TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), }, { ...response, status: destResponse.status }, ); diff --git a/src/v0/util/tags.js b/src/v0/util/tags.js index 81e6b9a2a6..18f00f963f 100644 --- a/src/v0/util/tags.js +++ b/src/v0/util/tags.js @@ -51,7 +51,7 @@ const ERROR_TYPES = { OAUTH_SECRET: 'oAuthSecret', UNSUPPORTED: 'unsupported', REDIS: 'redis', - FILTERED: 'filtered', + FILTERED: 'filtered' }; const METADATA = { diff --git a/test/integrations/destinations/facebook_pixel/dataDelivery/data.ts b/test/integrations/destinations/facebook_pixel/dataDelivery/data.ts index eb9ce344e0..239fa93a6a 100644 --- a/test/integrations/destinations/facebook_pixel/dataDelivery/data.ts +++ b/test/integrations/destinations/facebook_pixel/dataDelivery/data.ts @@ -43,19 +43,20 @@ export const data = [ message: 'Invalid OAuth 2.0 access token', destinationResponse: { error: { - message: 'The access token could not be decrypted', - type: 'OAuthException', code: 190, fbtrace_id: 'fbpixel_trace_id', + message: 'The access token could not be decrypted', + type: 'OAuthException', }, status: 500, }, statTags: { destType: 'FACEBOOK_PIXEL', - errorCategory: 'network', + errorCategory: 'dataValidation', destinationId: 'Non-determininable', workspaceId: 'Non-determininable', - errorType: 'aborted', + errorType: 'configuration', + meta: 'accessTokenExpired', feature: 'dataDelivery', implementation: 'native', module: 'destination', diff --git a/test/integrations/destinations/fb/dataDelivery/data.ts b/test/integrations/destinations/fb/dataDelivery/data.ts index f9405ba4b3..3b37d03f46 100644 --- a/test/integrations/destinations/fb/dataDelivery/data.ts +++ b/test/integrations/destinations/fb/dataDelivery/data.ts @@ -57,19 +57,20 @@ export const data = [ message: 'Invalid OAuth 2.0 access token', destinationResponse: { error: { - message: 'The access token could not be decrypted', - type: 'OAuthException', code: 190, fbtrace_id: 'fbpixel_trace_id', + message: 'The access token could not be decrypted', + type: 'OAuthException', }, status: 500, }, statTags: { destType: 'FB', - errorCategory: 'network', + errorCategory: 'dataValidation', destinationId: 'Non-determininable', workspaceId: 'Non-determininable', - errorType: 'aborted', + errorType: 'configuration', + meta: 'accessTokenExpired', feature: 'dataDelivery', implementation: 'native', module: 'destination', diff --git a/test/integrations/destinations/fb_custom_audience/dataDelivery/data.ts b/test/integrations/destinations/fb_custom_audience/dataDelivery/data.ts index 3066dae887..5ce15e0ea0 100644 --- a/test/integrations/destinations/fb_custom_audience/dataDelivery/data.ts +++ b/test/integrations/destinations/fb_custom_audience/dataDelivery/data.ts @@ -572,4 +572,76 @@ export const data = [ }, }, }, + { + name: 'fb_custom_audience', + description: 'user addition failed due expired access token error', + feature: 'dataDelivery', + module: 'destination', + version: 'v0', + input: { + request: { + body: { + version: '1', + type: 'REST', + method: 'DELETE', + endpoint: getEndPoint('aud1'), + headers: { + 'test-dest-response-key': 'accessTokenInvalidError', + }, + params: { + access_token: 'ABC', + payload: { + is_raw: true, + data_source: { + sub_type: 'ANYTHING', + }, + schema: ['DOBY', 'PHONE', 'GEN', 'FI', 'MADID', 'ZIP', 'ST', 'COUNTRY'], + data: [['2013', '@09432457768', 'f', 'Ms.', 'ABC', 'ZIP ', '123abc ', 'IN']], + }, + }, + body: { + JSON: {}, + XML: {}, + JSON_ARRAY: {}, + FORM: {}, + }, + files: {}, + }, + }, + }, + output: { + response: { + status: 400, + body: { + output: { + destinationResponse: { + error: { + code: 190, + error_subcode: 463, + fbtrace_id: 'A3b8C6PpI-kdIOwPwV4PANi', + message: + 'Error validating access token: Session has expired on Tuesday, 01-Aug-23 10:12:14 PDT. The current time is Sunday, 28-Jan-24 16:01:17 PST.', + type: 'OAuthException', + }, + status: 400, + }, + message: + 'Error validating access token: Session has expired on Tuesday, 01-Aug-23 10:12:14 PDT. The current time is Sunday, 28-Jan-24 16:01:17 PST.', + statTags: { + destType: 'FB_CUSTOM_AUDIENCE', + destinationId: 'Non-determininable', + errorCategory: 'dataValidation', + errorType: 'configuration', + meta: 'accessTokenExpired', + feature: 'dataDelivery', + implementation: 'native', + module: 'destination', + workspaceId: 'Non-determininable', + }, + status: 400, + }, + }, + }, + }, + }, ]; diff --git a/test/integrations/destinations/fb_custom_audience/network.ts b/test/integrations/destinations/fb_custom_audience/network.ts index bbdc1ffc28..fa11f28370 100644 --- a/test/integrations/destinations/fb_custom_audience/network.ts +++ b/test/integrations/destinations/fb_custom_audience/network.ts @@ -480,4 +480,46 @@ export const networkCallsData = [ status: 403, }, }, + { + httpReq: { + version: '1', + type: 'REST', + method: 'DELETE', + endpoint: getEndPoint('aud1'), + headers: { + 'test-dest-response-key': 'accessTokenInvalidError', + }, + params: { + access_token: 'ABC', + payload: { + is_raw: true, + data_source: { + sub_type: 'ANYTHING', + }, + schema: ['DOBY', 'PHONE', 'GEN', 'FI', 'MADID', 'ZIP', 'ST', 'COUNTRY'], + data: [['2013', '@09432457768', 'f', 'Ms.', 'ABC', 'ZIP ', '123abc ', 'IN']], + }, + }, + userId: '', + body: { + JSON: {}, + XML: {}, + JSON_ARRAY: {}, + FORM: {}, + }, + files: {}, + }, + httpRes: { + data: { + error: { + message: 'Error validating access token: Session has expired on Tuesday, 01-Aug-23 10:12:14 PDT. The current time is Sunday, 28-Jan-24 16:01:17 PST.', + type: 'OAuthException', + code: 190, + error_subcode: 463, + fbtrace_id: 'A3b8C6PpI-kdIOwPwV4PANi' + }, + }, + status: 400, + }, + } ];