Skip to content

Commit

Permalink
fix: Make sure that handler exists for linkederrors integration (#2742)
Browse files Browse the repository at this point in the history
* fix: Make sure that handler exists for linkederrors integration
  • Loading branch information
kamilogorek committed Jul 14, 2020
1 parent bc52769 commit 9041c1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
12 changes: 7 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,8 @@ 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>;
const handler = self._handler && self._handler.bind(self);
return typeof handler === 'function' ? handler(event, hint) : event;
}
return event;
});
Expand All @@ -52,13 +54,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 +76,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

0 comments on commit 9041c1d

Please sign in to comment.