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

fix: error extraction migrated to typescript #2233

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = {
// diagnostics: false
// },
// },
preset: 'ts-jest/presets/js-with-ts-esm',

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
Expand Down
6 changes: 0 additions & 6 deletions src/util/error-extractor/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
const ErrorDetailsExtractor = require(".");
/* eslint-disable max-classes-per-file */
import { MessageDetails, StatusCode } from "./types";

class ErrorDetailsExtractorBuilder {
export class ErrorDetailsExtractor {
status: StatusCode;

messageDetails: MessageDetails;

constructor (builder: ErrorDetailsExtractorBuilder) {
this.status = builder.getStatus();
this.messageDetails = builder.getMessageDetails();
}

}

export class ErrorDetailsExtractorBuilder {
status: StatusCode;

messageDetails: MessageDetails;

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

setStatus(status) {
setStatus(status: number): ErrorDetailsExtractorBuilder {
this.status = status;
return this;
}
Expand All @@ -18,7 +34,7 @@ class ErrorDetailsExtractorBuilder {
* @param {string} fieldPath -- Path of the field which should be set as "error message"
* @returns
*/
setMessageField(fieldPath) {
setMessageField(fieldPath: string): ErrorDetailsExtractorBuilder {
if (this.messageDetails?.message) {
// This check basically ensures that "setMessage" was not already before
return this;
Expand All @@ -35,7 +51,7 @@ class ErrorDetailsExtractorBuilder {
* @param {string} msg - error message
* @returns
*/
setMessage(msg) {
setMessage(msg: string): ErrorDetailsExtractorBuilder {
if (this.messageDetails?.field) {
// This check basically ensures that "setMessageField" was not already called before
return this;
Expand All @@ -46,17 +62,17 @@ class ErrorDetailsExtractorBuilder {
return this;
}

build() {
build(): ErrorDetailsExtractor {
return new ErrorDetailsExtractor(this)
}

getStatus() {
getStatus(): number {
return this.status;
}

getMessageDetails() {
getMessageDetails(): Record<string, string> {
return this.messageDetails;
}
}

module.exports = ErrorDetailsExtractorBuilder

2 changes: 2 additions & 0 deletions src/util/error-extractor/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type MessageDetails = Record<string, string>;
export type StatusCode = number;
1 change: 1 addition & 0 deletions src/util/redis/redisConnector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe(`Redis Class Get Tests`, () => {
const data = JSON.parse(testDataFile);
data.forEach((dataPoint, index) => {
it(`${index}. Redis Get- ${dataPoint.description}`, async () => {
let isObjExpected;
try {
const output = await RedisDB.getVal(dataPoint.input.value, (isObjExpected = false));
expect(output).toEqual(dataPoint.output);
Expand Down
12 changes: 8 additions & 4 deletions src/v0/destinations/facebook_pixel/networkHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
const { prepareProxyRequest, proxyRequest } = require('../../../adapters/network');
const { NetworkError } = require('../../util/errorTypes');
const tags = require('../../util/tags');
const ErrorDetailsExtractorBuilder = require('../../../util/error-extractor/builder');
const { ErrorDetailsExtractorBuilder } = require('../../../util/error-extractor');

/**
* Only under below mentioned scenario(s), add the errorCodes, subCodes etc,. to this map
Expand Down Expand Up @@ -165,9 +165,12 @@ const getErrorDetailsFromErrorMap = (error) => {
const getStatus = (error) => {
const errorDetail = getErrorDetailsFromErrorMap(error);
let errorStatus = 500;
if (!isEmpty(errorDetail)) {
errorStatus = errorDetail.status;
const isErrorDetailEmpty = isEmpty(errorDetail);
if (isErrorDetailEmpty) {
// Unhandled error response
return {status: errorStatus, tags: { [tags.TAG_NAMES.META]: tags.METADATA.UNHANDLED_STATUS_CODE, } }
}
errorStatus = errorDetail.status;

let errorMessage = errorDetail?.messageDetails?.message;
if (errorDetail?.messageDetails?.field) {
Expand All @@ -184,11 +187,12 @@ const errorResponseHandler = (destResponse) => {
return;
}
const { error } = response;
const { status, errorMessage } = getStatus(error);
const { status, errorMessage, tags: errorStatTags } = getStatus(error);
throw new NetworkError(
`${errorMessage || error.message || 'Unknown failure during response transformation'}`,
status,
{
...errorStatTags,
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
},
{ ...response, status: destResponse.status },
Expand Down
14 changes: 14 additions & 0 deletions test/__mocks__/data/facebook_pixel/proxy_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@
"status": 400
}
},
"https://graph.facebook.com/v16.0/1234567891234572/events?access_token=valid_access_token_unhandled_response": {
"response": {
"data": {
"error": {
"message": "Unhandled random error",
"type": "RandomException",
"code": 5,
"error_subcode": 12,
"fbtrace_id": "facebook_px_trace_id_10"
}
},
"status": 412
}
},
"https://graph.facebook.com/v16.0/1234567891234570/events?access_token=valid_access_token": {
"response": {
"data": {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/azure_synapse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var testCases = [
];

describe("Azure Synapse data types testing", () => {
options = {};
let options = {};
options.getDataTypeOverride = getDataTypeOverride;
testCases.forEach(testCase => {
it(`should return data type ${testCase.type} for this input data ${testCase.data} everytime`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/bq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var testCases = [
];

describe("Big Query data types testing", () => {
options = {};
let options = {};
options.getDataTypeOverride = getDataTypeOverride;
testCases.forEach(testCase => {
it(`should return data type ${testCase.type} for this input data ${testCase.data} everytime`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/clickhouse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var testCases = [
];

describe("ClickHouse data types testing", () => {
options = {};
let options = {};
options.getDataTypeOverride = getDataTypeOverride;
testCases.forEach(testCase => {
it(`should return data type ${testCase.type} for this input data ${testCase.data} everytime`, () => {
Expand Down
24 changes: 24 additions & 0 deletions test/__tests__/data/facebook_pixel_proxy_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,29 @@
"endpoint": "https://graph.facebook.com/v16.0/1234567891234571/events?access_token=valid_access_token"
}
}
},
{
"request": {
"body": {
"body": {
"XML": {},
"FORM": {
"data": [
"{\"user_data\":{\"external_id\":\"c58f05b5e3cc4796f3181cf07349d306547c00b20841a175b179c6860e6a34ab\",\"client_ip_address\":\"32.122.223.26\",\"client_user_agent\":\"Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1\"},\"event_name\":\"Checkout Step Viewed\",\"event_time\":1654772112,\"event_source_url\":\"https://www.my.kaiser.com/checkout\",\"event_id\":\"4f002656-a7b2-4c17-b9bd-8caa5a29190a\",\"custom_data\":{\"checkout_id\":\"26SF29B\",\"site\":\"www.my.kaiser.com\",\"step\":1}}"
]
},
"JSON": {},
"JSON_ARRAY": {}
},
"type": "REST",
"files": {},
"method": "POST",
"params": {},
"userId": "",
"headers": {},
"version": "1",
"endpoint": "https://graph.facebook.com/v16.0/1234567891234572/events?access_token=valid_access_token_unhandled_response"
}
}
}
]
25 changes: 25 additions & 0 deletions test/__tests__/data/facebook_pixel_proxy_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,30 @@
"module": "destination"
}
}
},
{
"output": {
"status": 500,
"message": "Unhandled random error",
"destinationResponse": {
"error": {
"message": "Unhandled random error",
"type": "RandomException",
"code": 5,
"error_subcode": 12,
"fbtrace_id": "facebook_px_trace_id_10"
},
"status": 412
},
"statTags": {
"destType": "FACEBOOK_PIXEL",
"errorCategory": "network",
"errorType": "retryable",
"feature": "dataDelivery",
"implementation": "native",
"module": "destination",
"meta": "unhandledStatusCode"
}
}
}
]
8 changes: 8 additions & 0 deletions test/__tests__/marketo_bulk_upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ const integration = "marketo_bulk_upload";
const transformer = require(`../../src/${version}/destinations/${integration}/transform`);

jest.mock("axios");
let reqTransformBody;
let respTransformBody;
let respFileUploadBody;
let reqFileUploadBody;
let reqPollBody;
let respPollBody;
let reqJobStatusBody;
let respJobStatusBody;

try {
reqTransformBody = JSON.parse(
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/mssql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var testCases = [
];

describe("MSSQL data types testing", () => {
options = {};
let options = {};
options.getDataTypeOverride = getDataTypeOverride;
testCases.forEach(testCase => {
it(`should return data type ${testCase.type} for this input data ${testCase.data} everytime`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/postgres.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var testCases = [
];

describe("Postgres data types testing", () => {
options = {};
let options = {};
options.getDataTypeOverride = getDataTypeOverride;
testCases.forEach(testCase => {
it(`should return data type ${testCase.type} for this input data ${testCase.data} everytime`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/rs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var testCases = [
];

describe("Redshift data types testing", () => {
options = {};
let options = {};
options.getDataTypeOverride = getDataTypeOverride;
testCases.forEach(testCase => {
it(`should return data type ${testCase.type} for this input data ${testCase.data} everytime`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/s3_datalake.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var testCases = [
];

describe("S3 Datalake data types testing", () => {
options = {};
let options = {};
options.getDataTypeOverride = getDataTypeOverride;
testCases.forEach(testCase => {
it(`should return data type ${testCase.type} for this input data ${testCase.data} everytime`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/snowflake.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var testCases = [
];

describe("Snowflake data types testing", () => {
options = {};
let options = {};
options.getDataTypeOverride = getDataTypeOverride;
testCases.forEach(testCase => {
it(`should return data type ${testCase.type} for this input data ${testCase.data} everytime`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/__tests__/warehouse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ describe("handle reserved words", () => {
Object.keys(reserverdKeywordsMap).forEach(k => {
expect(out.metadata.columns).not.toHaveProperty(k.toLowerCase());
expect(out.metadata.columns).not.toHaveProperty(k.toUpperCase());
snakeCasedKey = _.snakeCase(k).toUpperCase();
let snakeCasedKey = _.snakeCase(k).toUpperCase();
if (k === snakeCasedKey) {
k = `_${k}`;
} else {
Expand Down