Skip to content

Commit

Permalink
chore: ignore non-errors while creating error payload
Browse files Browse the repository at this point in the history
  • Loading branch information
MoumitaM committed Jul 30, 2024
1 parent 14d545f commit ab05b77
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ describe('Error Reporting utilities', () => {
expect(isRudderSDKError(event)).toBe(expectedValue);
},
);

it('should return false if the error message contains "a non-error"', () => {
const event = {
message: 'a non-error',
};

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

describe('getErrorContext', () => {
Expand Down
27 changes: 11 additions & 16 deletions packages/analytics-js-plugins/src/errorReporting/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,10 @@ const normaliseError = (
let error;
let internalFrames = 0;

const createAndLogInputError = (reason: string) => {
const logInputError = (reason: string) => {
const verb = component === 'error cause' ? 'was' : 'received';
if (logger) logger.warn(`${component} ${verb} a non-error: "${reason}"`);
const err = new Error(
`${component} ${verb} a non-error. See "${component}" tab for more detail.`,
);
err.name = 'InvalidError';
return err;
return undefined;
};

// In some cases:
Expand All @@ -95,8 +91,7 @@ const normaliseError = (
if (isError(maybeError)) {
error = maybeError;
} else {
error = createAndLogInputError(typeof maybeError);
internalFrames += 2;
error = logInputError(typeof maybeError);
}
} else {
switch (typeof maybeError) {
Expand All @@ -107,8 +102,7 @@ const normaliseError = (
internalFrames += 1;
break;
case 'function':
error = createAndLogInputError('function');
internalFrames += 2;
error = logInputError('function');
break;
case 'object':
if (maybeError !== null && isError(maybeError)) {
Expand All @@ -118,17 +112,15 @@ const normaliseError = (
error.name = maybeError.name || maybeError.errorClass;
internalFrames += 1;
} else {
error = createAndLogInputError(maybeError === null ? 'null' : 'unsupported object');
internalFrames += 2;
error = logInputError(maybeError === null ? 'null' : 'unsupported object');
}
break;
default:
error = createAndLogInputError('nothing');
internalFrames += 2;
error = logInputError('nothing');
}
}

if (!hasStack(error)) {
if (error && !hasStack(error)) {
// in IE10/11 a new Error() doesn't have a stacktrace until you throw it, so try that here
try {
throw error;
Expand All @@ -137,7 +129,7 @@ const normaliseError = (
error = e;
// if the error only got a stacktrace after we threw it here, we know it
// will only have one extra internal frame from this function, regardless
// of whether it went through createAndLogInputError() or not
// of whether it went through logInputError() or not
internalFrames = 1;
}
}
Expand Down Expand Up @@ -166,6 +158,9 @@ class ErrorFormat implements IErrorFormat {
component,
logger,
);
if (!error) {
return undefined;
}
let event;
try {
const stacktrace = getStacktrace(
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics-js-plugins/src/errorReporting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const ErrorReporting = (): ExtensionPlugin => ({
);

// filter errors
if (!isRudderSDKError(errorPayload.errors[0])) {
if (!errorPayload || !isRudderSDKError(errorPayload.errors[0])) {
return;
}

Expand Down
4 changes: 0 additions & 4 deletions packages/analytics-js-plugins/src/errorReporting/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ const getBugsnagErrorEvent = (

const isRudderSDKError = (event: any) => {
const errorOrigin = event.stacktrace?.[0]?.file;
const errorMessage = event.message;
if (errorMessage && typeof errorMessage === 'string' && errorMessage.includes('a non-error')) {
return false;
}

if (!errorOrigin || typeof errorOrigin !== 'string') {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('ErrorHandler - getNormalizedErrorForUnhandledError', () => {
expect(normalizedError).toBeUndefined();
});

it.skip('should return error instance for Event argument value with SDK script target', () => {
it('should return error instance for Event argument value with SDK script target', () => {
const event = new Event('dummyError');
const targetElement = document.createElement('script');
targetElement.dataset.loader = 'RS_JS_SDK';
Expand Down
40 changes: 20 additions & 20 deletions packages/analytics-js/src/services/ErrorHandler/processError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ const getNormalizedErrorForUnhandledError = (error: SDKError): SDKError | undefi
return error;
}
// TODO: remove this block once all device mode integrations start using the v3 script loader module (TS)
// if (error instanceof Event) {
// const eventTarget = error.target as ErrorTarget;
// // Discard all the non-script loading errors
// if (eventTarget && eventTarget.localName !== 'script') {
// return undefined;
// }
// // Discard script errors that are not originated at SDK or from native SDKs
// if (
// eventTarget?.dataset &&
// (eventTarget.dataset.loader !== LOAD_ORIGIN ||
// eventTarget.dataset.isnonnativesdk !== 'true')
// ) {
// return undefined;
// }
// const errorMessage = `Error in loading a third-party script from URL ${eventTarget?.src} with ID ${eventTarget?.id}.`;
// return Object.create(error, {
// message: { value: errorMessage },
// });
// }
return undefined;
if (error instanceof Event) {
const eventTarget = error.target as ErrorTarget;
// Discard all the non-script loading errors
if (eventTarget && eventTarget.localName !== 'script') {
return undefined;
}
// Discard script errors that are not originated at SDK or from native SDKs
if (
eventTarget?.dataset &&
(eventTarget.dataset.loader !== LOAD_ORIGIN ||
eventTarget.dataset.isnonnativesdk !== 'true')
) {
return undefined;
}
const errorMessage = `Error in loading a third-party script from URL ${eventTarget?.src} with ID ${eventTarget?.id}.`;
return Object.create(error, {
message: { value: errorMessage },
});
}
return error;
} catch (e) {
return e;
}
Expand Down

0 comments on commit ab05b77

Please sign in to comment.