Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new stat for access token expired in fb custom audience #3043

Merged
merged 16 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1",
"@rudderstack/workflow-engine": "^0.6.9",
"ajv": "^8.12.0",
"ajv-draft-04": "^1.0.0",
Expand Down
16 changes: 15 additions & 1 deletion src/util/error-extractor/index.ts
Original file line number Diff line number Diff line change
@@ -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();
}

}
Expand All @@ -18,16 +21,23 @@ export class ErrorDetailsExtractorBuilder {

messageDetails: MessageDetails;

stat: Stat;
constructor() {
this.status = 0;
this.messageDetails = {};
this.stat = {};
}

setStatus(status: number): ErrorDetailsExtractorBuilder {
this.status = status;
return this;
}

setStat(stat: Record<string, string>): 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
*
Expand Down Expand Up @@ -69,6 +79,10 @@ export class ErrorDetailsExtractorBuilder {
getStatus(): number {
return this.status;
}

getStat(): Record<string, string> {
return this.stat;
}

getMessageDetails(): Record<string, string> {
return this.messageDetails;
Expand Down
3 changes: 2 additions & 1 deletion src/util/error-extractor/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type MessageDetails = Record<string, string>;
export type StatusCode = number;
export type StatusCode = number;
export type Stat = Record<string, string>
24 changes: 20 additions & 4 deletions src/v0/util/facebookUtils/networkHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { isEmpty } = require('lodash');
const get = require('get-value');
const { NetworkError } = require('@rudderstack/integrations-lib');
const {
NetworkError,
ConfigurationAuthError,
isDefinedAndNotNull,
} = require('@rudderstack/integrations-lib');
const {
processAxiosResponse,
getDynamicErrorType,
Expand Down Expand Up @@ -100,12 +104,18 @@
190: {
460: new ErrorDetailsExtractorBuilder()
.setStatus(400)
.setStat({
[tags.TAG_NAMES.ERROR_TYPE]: tags.ERROR_TYPES.ACCESS_TOKEN_EXPIRED,
})
.setMessage(
'The session has been invalidated because the user changed their password or Facebook has changed the session for security reasons',
)
.build(),
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
default: new ErrorDetailsExtractorBuilder()
.setStatus(400)
.setStat({
[tags.TAG_NAMES.ERROR_TYPE]: tags.ERROR_TYPES.UNAUTHORIZED,
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
})
.setMessage('Invalid OAuth 2.0 access token')
.build(),
},
Expand Down Expand Up @@ -211,13 +221,14 @@

const getStatus = (error) => {
const errorDetail = getErrorDetailsFromErrorMap(error);
console.log(errorDetail);

Check warning on line 224 in src/v0/util/facebookUtils/networkHandler.js

View workflow job for this annotation

GitHub Actions / Code Coverage

Unexpected console statement
let errorStatus = 500;
const isErrorDetailEmpty = isEmpty(errorDetail);
if (isErrorDetailEmpty) {
// Unhandled error response
return {
status: errorStatus,
tags: { [tags.TAG_NAMES.META]: tags.METADATA.UNHANDLED_STATUS_CODE },
stats: { [tags.TAG_NAMES.META]: tags.METADATA.UNHANDLED_STATUS_CODE },
};
}
errorStatus = errorDetail.status;
Expand All @@ -227,7 +238,9 @@
errorMessage = get(error, errorDetail?.messageDetails?.field);
}

return { status: errorStatus, errorMessage };
const stats = errorDetail?.stat;

return { status: errorStatus, errorMessage, stats };
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
};

const errorResponseHandler = (destResponse) => {
Expand All @@ -237,7 +250,10 @@
return;
}
const { error } = response;
const { status, errorMessage, tags: errorStatTags } = getStatus(error);
const { status, errorMessage, stats: errorStatTags } = getStatus(error);
if (isDefinedAndNotNull(errorStatTags) && errorStatTags?.errorType === 'accessTokenExpired') {
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
throw new ConfigurationAuthError(`${errorMessage}`);
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
}
throw new NetworkError(
`${errorMessage || error.message || 'Unknown failure during response transformation'}`,
status,
Expand Down
1 change: 1 addition & 0 deletions src/v0/util/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const ERROR_TYPES = {
UNSUPPORTED: 'unsupported',
REDIS: 'redis',
FILTERED: 'filtered',
UNAUTHORIZED: 'accessTokenExpired',
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
};

const METADATA = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,14 @@ export const data = [
output: {
status: 400,
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',
},
status: 500,
},
destinationResponse: '',
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
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',
Expand Down
15 changes: 4 additions & 11 deletions test/integrations/destinations/fb/dataDelivery/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,14 @@ export const data = [
output: {
status: 400,
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',
},
status: 500,
},
destinationResponse: '',
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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,65 @@ 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: '',
message: 'Invalid OAuth 2.0 access token',
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,
},
},
},
},
},
];
42 changes: 42 additions & 0 deletions test/integrations/destinations/fb_custom_audience/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
];
Loading