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: Make sure that handler exists for linkederrors integration #2742

Merged
merged 2 commits into from Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/node/src/integrations/linkederrors.ts
Expand Up @@ -13,6 +13,7 @@ export class LinkedErrors implements Integration {
* @inheritDoc
*/
public readonly name: string = LinkedErrors.id;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -43,7 +44,7 @@ export class LinkedErrors implements Integration {
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
const self = getCurrentHub().getIntegration(LinkedErrors);
if (self) {
return (self.handler(event, hint) as unknown) as PromiseLike<Event>;
return typeof self._handler === 'function' ? self._handler(event, hint) : event;
}
return event;
});
Expand All @@ -52,13 +53,13 @@ export class LinkedErrors implements Integration {
/**
* @inheritDoc
*/
public handler(event: Event, hint?: EventHint): PromiseLike<Event> {
private _handler(event: Event, hint?: EventHint): PromiseLike<Event> {
if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) {
return SyncPromise.resolve(event);
}

return new SyncPromise<Event>(resolve => {
this.walkErrorTree(hint.originalException as Error, this._key)
this._walkErrorTree(hint.originalException as Error, this._key)
.then((linkedErrors: Exception[]) => {
if (event && event.exception && event.exception.values) {
event.exception.values = [...linkedErrors, ...event.exception.values];
Expand All @@ -74,14 +75,14 @@ export class LinkedErrors implements Integration {
/**
* @inheritDoc
*/
public walkErrorTree(error: ExtendedError, key: string, stack: Exception[] = []): PromiseLike<Exception[]> {
private _walkErrorTree(error: ExtendedError, key: string, stack: Exception[] = []): PromiseLike<Exception[]> {
if (!isInstanceOf(error[key], Error) || stack.length + 1 >= this._limit) {
return SyncPromise.resolve(stack);
}
return new SyncPromise<Exception[]>((resolve, reject) => {
getExceptionFromError(error[key])
.then((exception: Exception) => {
this.walkErrorTree(error[key], key, [exception, ...stack])
this._walkErrorTree(error[key], key, [exception, ...stack])
.then(resolve)
.then(null, () => {
reject();
Expand Down
18 changes: 9 additions & 9 deletions packages/node/test/integrations/linkederrors.test.ts
Expand Up @@ -14,27 +14,27 @@ describe('LinkedErrors', () => {
describe('handler', () => {
it('should bail out if event doesnt contain exception', async () => {
expect.assertions(2);
const spy = jest.spyOn(linkedErrors, 'walkErrorTree');
const spy = jest.spyOn(linkedErrors, '_walkErrorTree');
const event = {
message: 'foo',
};
return linkedErrors.handler(event).then((result: any) => {
return linkedErrors._handler(event).then((result: any) => {
expect(spy.mock.calls.length).toEqual(0);
expect(result).toEqual(event);
});
});

it('should bail out if event contains exception, but no hint', async () => {
expect.assertions(2);
const spy = jest.spyOn(linkedErrors, 'walkErrorTree');
const spy = jest.spyOn(linkedErrors, '_walkErrorTree');
const one = new Error('originalException');
const backend = new NodeBackend({});
let event: Event | undefined;
return backend
.eventFromException(one)
.then(eventFromException => {
event = eventFromException;
return linkedErrors.handler(eventFromException);
return linkedErrors._handler(eventFromException);
})
.then(result => {
expect(spy.mock.calls.length).toEqual(0);
Expand All @@ -44,7 +44,7 @@ describe('LinkedErrors', () => {

it('should call walkErrorTree if event contains exception and hint with originalException', async () => {
expect.assertions(1);
const spy = jest.spyOn(linkedErrors, 'walkErrorTree').mockImplementation(
const spy = jest.spyOn(linkedErrors, '_walkErrorTree').mockImplementation(
async () =>
new Promise<[]>(resolve => {
resolve([]);
Expand All @@ -54,7 +54,7 @@ describe('LinkedErrors', () => {
const backend = new NodeBackend({});
return backend.eventFromException(one).then(event =>
linkedErrors
.handler(event, {
._handler(event, {
originalException: one,
})
.then(_ => {
Expand All @@ -74,7 +74,7 @@ describe('LinkedErrors', () => {
const backend = new NodeBackend({});
return backend.eventFromException(one).then(event =>
linkedErrors
.handler(event, {
._handler(event, {
originalException: one,
})
.then((result: any) => {
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('LinkedErrors', () => {
const backend = new NodeBackend({});
return backend.eventFromException(one).then(event =>
linkedErrors
.handler(event, {
._handler(event, {
originalException: one,
})
.then((result: any) => {
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('LinkedErrors', () => {
const backend = new NodeBackend({});
return backend.eventFromException(one).then(event =>
linkedErrors
.handler(event, {
._handler(event, {
originalException: one,
})
.then((result: any) => {
Expand Down