Skip to content

Commit

Permalink
chore: logic to filter by error msg moved to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
MoumitaM committed Jul 30, 2024
1 parent 325044d commit 2cd884a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getBugsnagErrorEvent,
getErrorDeliveryPayload,
getConfigForPayloadCreation,
isAllowedToBeNotified,
} from '../../src/errorReporting/utils';

jest.mock('@rudderstack/analytics-js-common/utilities/uuId', () => ({
Expand Down Expand Up @@ -115,6 +116,14 @@ describe('Error Reporting utilities', () => {
expect(isRudderSDKError(event)).toBe(expectedValue);
},
);

it('should return false if error message contains value that is not allowed to be notified', () => {
const event = {
message: 'The request failed',
};

expect(isRudderSDKError(event)).toBe(false);
});
});

describe('getErrorContext', () => {
Expand Down Expand Up @@ -548,4 +557,21 @@ describe('Error Reporting utilities', () => {
});
});
});

describe('isAllowedToBeNotified', () => {
it('should return true for Error argument value', () => {
const result = isAllowedToBeNotified('dummy error');
expect(result).toBeTruthy();
});

it('should return false for Error argument value', () => {
const result = isAllowedToBeNotified('The request failed');
expect(result).toBeFalsy();
});

it('should return false for Error argument value', () => {
const result = isAllowedToBeNotified('unhandledException handler received a non-error');
expect(result).toBeFalsy();
});
});
});
19 changes: 19 additions & 0 deletions packages/analytics-js-plugins/src/errorReporting/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
import { generateUUID } from '@rudderstack/analytics-js-common/utilities/uuId';
import { METRICS_PAYLOAD_VERSION } from '@rudderstack/analytics-js-common/constants/metrics';
import { stringifyWithoutCircular } from '@rudderstack/analytics-js-common/utilities/json';
import { ERROR_MESSAGES_TO_BE_FILTERED } from '@rudderstack/analytics-js-common/constants/errors';
import {
APP_STATE_EXCLUDE_KEYS,
DEV_HOSTS,
Expand Down Expand Up @@ -142,8 +143,25 @@ const getBugsnagErrorEvent = (
],
});

/**
* A function to determine whether the error should be promoted to notify or not
* @param {Error} error
* @returns
*/
const isAllowedToBeNotified = (errorMessage: string) =>
!ERROR_MESSAGES_TO_BE_FILTERED.some(e => errorMessage.includes(e));

/**
* A function to determine if the error is from Rudder SDK
* @param {Error} event
* @returns
*/
const isRudderSDKError = (event: any) => {
const errorOrigin = event.stacktrace?.[0]?.file;
const errorMessage = event.message;
if (errorMessage && typeof errorMessage === 'string' && !isAllowedToBeNotified(errorMessage)) {
return false;
}

if (!errorOrigin || typeof errorOrigin !== 'string') {
return false;
Expand Down Expand Up @@ -188,4 +206,5 @@ export {
isRudderSDKError,
getErrorDeliveryPayload,
getErrorContext,
isAllowedToBeNotified,
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
processError,
getNormalizedErrorForUnhandledError,
isAllowedToBeNotified,
} from '../../../src/services/ErrorHandler/processError';

jest.mock('../../../src/components/utilities/event', () => {
Expand Down Expand Up @@ -116,30 +115,3 @@ describe('ErrorHandler - getNormalizedErrorForUnhandledError', () => {
);
});
});

describe('ErrorHandler - isAllowedToBeNotified', () => {
it('should return true for Error argument value', () => {
const result = isAllowedToBeNotified(new Error('dummy error'));
expect(result).toBeTruthy();
});

it('should return true for Error argument value', () => {
const result = isAllowedToBeNotified(new Error('The request failed'));
expect(result).toBeFalsy();
});

it('should return true for ErrorEvent argument value', () => {
const result = isAllowedToBeNotified(new ErrorEvent('dummy error'));
expect(result).toBeTruthy();
});

it('should return true for PromiseRejectionEvent argument value', () => {
const result = isAllowedToBeNotified(new PromiseRejectionEvent('dummy error'));
expect(result).toBeTruthy();
});

it('should return true for PromiseRejectionEvent argument value', () => {
const result = isAllowedToBeNotified(new PromiseRejectionEvent('The request failed'));
expect(result).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ import {
import { state } from '../../state';
import { defaultPluginEngine } from '../PluginEngine';
import { defaultLogger } from '../Logger';
import {
isAllowedToBeNotified,
getNormalizedErrorForUnhandledError,
processError,
} from './processError';
import { getNormalizedErrorForUnhandledError, processError } from './processError';

/**
* A service to handle errors
Expand Down Expand Up @@ -204,7 +200,7 @@ class ErrorHandler implements IErrorHandler {
* @param {Error} error Error instance from handled error
*/
notifyError(error: SDKError, errorState: ErrorState) {
if (this.pluginEngine && this.httpClient && isAllowedToBeNotified(error)) {
if (this.pluginEngine && this.httpClient) {
try {
this.pluginEngine.invokeSingle(
'errorReporting.notify',
Expand Down
18 changes: 1 addition & 17 deletions packages/analytics-js/src/services/ErrorHandler/processError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { stringifyWithoutCircular } from '@rudderstack/analytics-js-common/utilities/json';
import { isString } from '@rudderstack/analytics-js-common/utilities/checks';
import type { ErrorTarget, SDKError } from '@rudderstack/analytics-js-common/types/ErrorHandler';
import { ERROR_MESSAGES_TO_BE_FILTERED } from '../../constants/errors';
import { LOAD_ORIGIN } from './constant';

/**
Expand Down Expand Up @@ -64,19 +63,4 @@ const getNormalizedErrorForUnhandledError = (error: SDKError): SDKError | undefi
}
};

/**
* A function to determine whether the error should be promoted to notify or not
* @param {Error} error
* @returns
*/
const isAllowedToBeNotified = (error: SDKError) => {
if ((error instanceof Error || error instanceof ErrorEvent) && error.message) {
return !ERROR_MESSAGES_TO_BE_FILTERED.some(e => error.message.includes(e));
}
if (error instanceof PromiseRejectionEvent && typeof error.reason === 'string') {
return !ERROR_MESSAGES_TO_BE_FILTERED.some(e => error.reason.includes(e));
}
return true;
};

export { processError, isAllowedToBeNotified, getNormalizedErrorForUnhandledError };
export { processError, getNormalizedErrorForUnhandledError };
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type {
} from '@rudderstack/analytics-js-common/types/HttpClient';
import type { ILogger } from '@rudderstack/analytics-js-common/types/Logger';
import { getMutatedError } from '@rudderstack/analytics-js-common/utilities/errors';
import { FAILED_REQUEST_ERR_MSG_PREFIX } from '@rudderstack/analytics-js-common/constants/errors';
import { DEFAULT_XHR_TIMEOUT_MS } from '../../../constants/timeouts';
import { FAILED_REQUEST_ERR_MSG_PREFIX } from '../../../constants/errors';
import {
XHR_PAYLOAD_PREP_ERROR,
XHR_DELIVERY_ERROR,
Expand Down

0 comments on commit 2cd884a

Please sign in to comment.