Skip to content

Commit

Permalink
fix: Parse unhandledrejection error objects (#123)
Browse files Browse the repository at this point in the history
* fix: Parse unhandledrejection error objects
  • Loading branch information
adebayor123 committed Mar 18, 2022
1 parent cf59ecb commit f69c859
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/plugins/event-plugins/JsErrorPlugin.ts
@@ -1,4 +1,3 @@
import { JSErrorEvent } from '../../events/js-error-event';
import { RecordEvent, Plugin, PluginContext } from '../Plugin';
import { JS_ERROR_EVENT_TYPE } from '../utils/constant';
import { errorEventToJsErrorEvent } from '../utils/js-error-utils';
Expand Down Expand Up @@ -70,12 +69,10 @@ export class JsErrorPlugin implements Plugin {
};

private promiseRejectEventHandler = (event: PromiseRejectionEvent) => {
const errorEvent: JSErrorEvent = {
version: '1.0.0',
this.eventHandler({
type: event.type,
message: event.reason
};
this.recordEvent(JS_ERROR_EVENT_TYPE, errorEvent);
error: event.reason
} as ErrorEvent);
};

private addEventHandler(): void {
Expand Down
98 changes: 98 additions & 0 deletions src/plugins/event-plugins/__tests__/JsErrorPlugin.test.ts
Expand Up @@ -422,4 +422,102 @@ describe('JsErrorPlugin tests', () => {
})
);
});

test('when unhandledrejection error event outputs empty object as reason then message is recorded as undefined', async () => {
// Init
const plugin: JsErrorPlugin = new JsErrorPlugin();

// Run
plugin.load(context);
const promiseRejectionEvent: PromiseRejectionEvent = new Event(
'unhandledrejection'
) as PromiseRejectionEvent;
// JSDOM has not implemented PromiseRejectionEvent, so we 'extend'
// Event to have the same functionality
window.dispatchEvent(
Object.assign(promiseRejectionEvent, {
promise: new Promise(() => {}),
reason: {}
})
);
plugin.disable();

// Assert
expect(record).toHaveBeenCalledTimes(1);
expect(record.mock.calls[0][0]).toEqual(JS_ERROR_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject(
expect.objectContaining({
version: '1.0.0',
type: 'unhandledrejection',
message: 'undefined'
})
);
});

test('when unhandledrejection error event outputs null object as reason then message is recorded as undefined', async () => {
// Init
const plugin: JsErrorPlugin = new JsErrorPlugin();

// Run
plugin.load(context);
const promiseRejectionEvent: PromiseRejectionEvent = new Event(
'unhandledrejection'
) as PromiseRejectionEvent;
// JSDOM has not implemented PromiseRejectionEvent, so we 'extend'
// Event to have the same functionality
window.dispatchEvent(
Object.assign(promiseRejectionEvent, {
promise: new Promise(() => {}),
reason: null
})
);
plugin.disable();

// Assert
expect(record).toHaveBeenCalledTimes(1);
expect(record.mock.calls[0][0]).toEqual(JS_ERROR_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject(
expect.objectContaining({
version: '1.0.0',
type: 'unhandledrejection',
message: 'undefined'
})
);
});

test('when unhandledrejection error event outputs error object as reason then error object is used', async () => {
// Init
const plugin: JsErrorPlugin = new JsErrorPlugin();

// Run
plugin.load(context);
const promiseRejectionEvent: PromiseRejectionEvent = new Event(
'unhandledrejection'
) as PromiseRejectionEvent;
// JSDOM has not implemented PromiseRejectionEvent, so we 'extend'
// Event to have the same functionality
window.dispatchEvent(
Object.assign(promiseRejectionEvent, {
promise: new Promise(() => {}),
reason: {
name: 'TypeError',
message: 'NetworkError when attempting to fetch resource.',
stack: 't/n.fetch@mock_client.js:2:104522t/n.fetchWrapper'
}
})
);
plugin.disable();

// Assert
expect(record).toHaveBeenCalledTimes(1);
expect(record.mock.calls[0][0]).toEqual(JS_ERROR_EVENT_TYPE);
expect(record.mock.calls[0][1]).toMatchObject(
expect.objectContaining({
version: '1.0.0',
type: 'TypeError',
message: 'NetworkError when attempting to fetch resource.',
stack: 't/n.fetch@mock_client.js:2:104522t/n.fetchWrapper'
})
);
});
});
5 changes: 4 additions & 1 deletion src/plugins/utils/js-error-utils.ts
Expand Up @@ -48,7 +48,10 @@ const appendErrorPrimitiveDetails = (
rumEvent: JSErrorEvent,
error: any
): void => {
rumEvent.type = error.toString();
// Keep unhandledrejection as type as it will write to rumEvent.message
if (rumEvent.type !== 'unhandledrejection') {
rumEvent.type = error.toString();
}
rumEvent.message = error.toString();
};

Expand Down

0 comments on commit f69c859

Please sign in to comment.