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: implement error reporting in core #2002

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
19 changes: 16 additions & 3 deletions jest/jest.setup-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ global.window.innerHeight = 1024;
global.window.__BUNDLE_ALL_PLUGINS__ = false;
global.window.__IS_LEGACY_BUILD__ = false;
global.window.__IS_DYNAMIC_CUSTOM_BUNDLE__ = false;
global.PromiseRejectionEvent = function (reason) {
this.reason = reason;
};
// Only define the mock if it's not already defined (e.g., in a real browser)
if (typeof PromiseRejectionEvent === 'undefined') {
// Mock class (very minimal)
class PromiseRejectionEvent extends Event {
constructor(type, eventInitDict) {
super(type, eventInitDict);
this.promise = eventInitDict?.promise;
this.reason = eventInitDict?.reason;
}
}

// Attach it to the global object so tests can use it.
global.PromiseRejectionEvent = PromiseRejectionEvent;
// If you rely on "window" instead:
// global.window.PromiseRejectionEvent = PromiseRejectionEvent;
}

// TODO: remove once we use globalThis in analytics v1.1 too
// Setup mocking for window.navigator
Expand Down
90 changes: 88 additions & 2 deletions package-lock.json

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

11 changes: 5 additions & 6 deletions packages/analytics-js-common/__mocks__/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { IErrorHandler, PreLoadErrorData } from '../src/types/ErrorHandler';
import { BufferQueue } from './BufferQueue';
import type { IErrorHandler } from '../src/types/ErrorHandler';
import { defaultHttpClient } from './HttpClient';
import { defaultLogger } from './Logger';

// Mock all the methods of the ErrorHandler class
class ErrorHandler implements IErrorHandler {
onError = jest.fn();
leaveBreadcrumb = jest.fn();
notifyError = jest.fn();
init = jest.fn();
attachErrorListeners = jest.fn();
errorBuffer = new BufferQueue<PreLoadErrorData>();
httpClient = defaultHttpClient;
logger = defaultLogger;
}

const defaultErrorHandler = new ErrorHandler();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { IErrorHandler } from '@rudderstack/analytics-js-common/types/ErrorHandler';
import type { IHttpClient } from '@rudderstack/analytics-js-common/types/HttpClient';
import type { ILogger } from '@rudderstack/analytics-js-common/types/Logger';
import type { IErrorHandler } from '../src/types/ErrorHandler';
import type { IHttpClient } from '../src/types/HttpClient';
import type { ILogger } from '../src/types/Logger';

class HttpClient implements IHttpClient {
errorHandler?: IErrorHandler;
logger?: ILogger;
hasErrorHandler = false;
init = jest.fn();
getData = jest.fn();
getAsyncData = jest.fn();
setAuthHeader = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IPluginEngine } from '@rudderstack/analytics-js-common/types/PluginEngine';
import type { IPluginEngine } from '../src/types/PluginEngine';

class PluginEngine implements IPluginEngine {
// mock all the properties and methods of the PluginEngine class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IPluginEngine } from '@rudderstack/analytics-js-common/types/PluginEngine';
import type { IPluginsManager } from '@rudderstack/analytics-js-common/types/PluginsManager';
import { defaultPluginEngine } from './PluginEngine';
import type { IPluginEngine } from '../src/types/PluginEngine';
import type { IPluginsManager } from '../src/types/PluginsManager';
import { defaultPluginEngine } from '../__mocks__/PluginEngine';

class PluginsManager implements IPluginsManager {
// mock all the properties and methods of the PluginsManager class
Expand Down
4 changes: 3 additions & 1 deletion packages/analytics-js-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
"ramda": "0.30.1",
"storejs": "2.1.0"
},
"devDependencies": {},
"devDependencies": {
"@bugsnag/js": "8.1.2"
},
"overrides": {},
"browserslist": {
"production": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ErrorState } from '../../types/ErrorHandler';
import type { IErrorHandler } from '../../types/ErrorHandler';
import type { ILogger } from '../../types/Logger';

export interface IExternalSourceLoadConfig {
Expand All @@ -11,16 +11,7 @@ export interface IExternalSourceLoadConfig {
}

export interface IExternalSrcLoader {
errorHandler?: {
onError(
error: unknown,
context?: string,
customMessage?: string,
shouldAlwaysThrow?: boolean,
): void;
leaveBreadcrumb(breadcrumb: string): void;
notifyError(error: Error, errorState: ErrorState): void;
};
errorHandler?: IErrorHandler;
logger?: ILogger;
timeout: number;
loadJSFile(config: IExternalSourceLoadConfig): void;
Expand Down
1 change: 0 additions & 1 deletion packages/analytics-js-common/src/types/ApplicationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export type Breadcrumb = {
export type ReportingState = {
isErrorReportingEnabled: Signal<boolean>;
isMetricsReportingEnabled: Signal<boolean>;
isErrorReportingPluginLoaded: Signal<boolean>;
breadcrumbs: Signal<Breadcrumb[]>;
};

Expand Down
22 changes: 5 additions & 17 deletions packages/analytics-js-common/src/types/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import type { IPluginEngine } from './PluginEngine';
import type { Event } from '@bugsnag/js';
import type { ILogger } from './Logger';
import type { BufferQueue } from '../services/BufferQueue/BufferQueue';
import type { IHttpClient } from './HttpClient';
import type { IExternalSrcLoader } from '../services/ExternalSrcLoader/types';

export type SDKError = unknown | Error | ErrorEvent | Event | PromiseRejectionEvent;

export interface IErrorHandler {
logger?: ILogger;
pluginEngine?: IPluginEngine;
errorBuffer: BufferQueue<PreLoadErrorData>;
init(httpClient: IHttpClient, externalSrcLoader: IExternalSrcLoader): void;
onError(
error: SDKError,
context?: string,
customMessage?: string,
shouldAlwaysThrow?: boolean,
errorType?: string,
): void;
httpClient: IHttpClient;
logger: ILogger;
onError(error: SDKError, context?: string, customMessage?: string, errorType?: string): void;
leaveBreadcrumb(breadcrumb: string): void;
notifyError(error: Error, errorState: ErrorState): void;
attachErrorListeners(): void;
}

export type ErrorState = {
severity: string;
severity: Event['severity'];
unhandled: boolean;
severityReason: { type: string };
};
Expand Down
1 change: 1 addition & 0 deletions packages/analytics-js-common/src/types/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface IHttpClient {
logger?: ILogger;
basicAuthHeader?: string;
hasErrorHandler: boolean;
init: (errorHandler: IErrorHandler) => void;
getData<T = any>(
config: IRequestConfig,
): Promise<{ data: T | string | undefined; details?: ResponseDetails }>;
Expand Down
39 changes: 6 additions & 33 deletions packages/analytics-js-common/src/types/Metrics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Event, Stackframe, User } from '@bugsnag/js';
import type { Breadcrumb } from './ApplicationState';

export type MetricServicePayload = {
Expand All @@ -12,46 +13,26 @@ export type MetricServicePayload = {
errors?: ErrorEventPayload;
};

// https://bugsnagerrorreportingapi.docs.apiary.io/#reference/0/notify/send-error-reports
export type ErrorEventPayload = {
notifier: {
name: string;
version: string;
url: string;
};
events: ErrorEventType[];
events: ErrorEvent[];
};

export type ErrorEventType = {
export type ErrorEvent = Pick<Event, 'severity' | 'app' | 'device' | 'request' | 'context'> & {
payloadVersion: string;
exceptions: Exception[];
severity: string;
unhandled: boolean;
severityReason: { type: string };
app: {
version: string;
releaseStage: string;
};
device: {
locale?: string;
userAgent?: string;
time?: Date;
};
request: {
url: string;
clientIp: string;
};
breadcrumbs: Breadcrumb[] | [];
context: string;
breadcrumbs: Breadcrumb[];
metaData: {
[index: string]: any;
};
user: {
id: string;
};
};

export type GeneratedEventType = {
errors: Exception[];
user: User;
};

export interface Exception {
Expand All @@ -60,11 +41,3 @@ export interface Exception {
type: string;
stacktrace: Stackframe[];
}
export interface Stackframe {
file: string;
method?: string;
lineNumber?: number;
columnNumber?: number;
code?: Record<string, string>;
inProject?: boolean;
}
17 changes: 13 additions & 4 deletions packages/analytics-js-common/src/utilities/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,20 @@ const isDefinedNotNullAndNotEmptyString = (value: any): boolean =>
isDefinedAndNotNull(value) && value !== '';

/**
* Determines if the input is an instance of Error
* @param obj input value
* @returns true if the input is an instance of Error and false otherwise
* Determines if the input is of type error
* @param value input value
* @returns true if the input is of type error else false
*/
const isTypeOfError = (obj: any): obj is Error => obj instanceof Error;
const isTypeOfError = (value: any): boolean => {
switch (Object.prototype.toString.call(value)) {
case '[object Error]':
case '[object Exception]':
case '[object DOMException]':
return true;
default:
return value instanceof Error;
}
};

export {
isFunction,
Expand Down
Loading