Skip to content

Commit

Permalink
ref: Extract error tree walking logic for LinkedErrors integrations (
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Jul 5, 2023
1 parent b48bc08 commit 1d8c81f
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 356 deletions.
65 changes: 20 additions & 45 deletions packages/browser/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import type { Event, EventHint, Exception, ExtendedError, Integration, StackParser } from '@sentry/types';
import { isInstanceOf } from '@sentry/utils';
import type { Event, EventHint, EventProcessor, Hub, Integration } from '@sentry/types';
import { applyAggregateErrorsToEvent } from '@sentry/utils';

import type { BrowserClient } from '../client';
import { exceptionFromError } from '../eventbuilder';

const DEFAULT_KEY = 'cause';
Expand Down Expand Up @@ -46,49 +44,26 @@ export class LinkedErrors implements Integration {
/**
* @inheritDoc
*/
public setupOnce(): void {
const client = getCurrentHub().getClient<BrowserClient>();
if (!client) {
return;
}
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
const self = getCurrentHub().getIntegration(LinkedErrors);
return self ? _handler(client.getOptions().stackParser, self._key, self._limit, event, hint) : event;
});
}
}
const hub = getCurrentHub();
const client = hub.getClient();
const self = hub.getIntegration(LinkedErrors);

/**
* @inheritDoc
*/
export function _handler(
parser: StackParser,
key: string,
limit: number,
event: Event,
hint?: EventHint,
): Event | null {
if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) {
return event;
}
const linkedErrors = _walkErrorTree(parser, limit, hint.originalException as ExtendedError, key);
event.exception.values = [...linkedErrors, ...event.exception.values];
return event;
}
if (!client || !self) {
return event;
}

applyAggregateErrorsToEvent(
exceptionFromError,
client.getOptions().stackParser,
self._key,
self._limit,
event,
hint,
);

/**
* JSDOC
*/
export function _walkErrorTree(
parser: StackParser,
limit: number,
error: ExtendedError,
key: string,
stack: Exception[] = [],
): Exception[] {
if (!isInstanceOf(error[key], Error) || stack.length + 1 >= limit) {
return stack;
return event;
});
}
const exception = exceptionFromError(parser, error[key]);
return _walkErrorTree(parser, limit, error[key], key, [exception, ...stack]);
}
121 changes: 0 additions & 121 deletions packages/browser/test/unit/integrations/linkederrors.test.ts

This file was deleted.

56 changes: 17 additions & 39 deletions packages/node/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import type { Event, EventHint, Exception, ExtendedError, Integration, StackParser } from '@sentry/types';
import { isInstanceOf } from '@sentry/utils';
import type { Event, EventHint, EventProcessor, Hub, Integration } from '@sentry/types';
import { applyAggregateErrorsToEvent } from '@sentry/utils';

import { exceptionFromError } from '../eventbuilder';
import { ContextLines } from './contextlines';
Expand Down Expand Up @@ -41,15 +40,25 @@ export class LinkedErrors implements Integration {
/**
* @inheritDoc
*/
public setupOnce(): void {
addGlobalEventProcessor(async (event: Event, hint: EventHint) => {
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor(async (event: Event, hint?: EventHint) => {
const hub = getCurrentHub();
const self = hub.getIntegration(LinkedErrors);
const client = hub.getClient();
if (client && self && self._handler && typeof self._handler === 'function') {
self._handler(client.getOptions().stackParser, event, hint);
const self = hub.getIntegration(LinkedErrors);

if (!client || !self) {
return event;
}

applyAggregateErrorsToEvent(
exceptionFromError,
client.getOptions().stackParser,
self._key,
self._limit,
event,
hint,
);

// If the ContextLines integration is enabled, we add source code context to linked errors
// because we can't guarantee the order that integrations are run.
const contextLines = getCurrentHub().getIntegration(ContextLines);
Expand All @@ -60,35 +69,4 @@ export class LinkedErrors implements Integration {
return event;
});
}

/**
* @inheritDoc
*/
private _handler(stackParser: StackParser, event: Event, hint: EventHint): Event {
if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) {
return event;
}

const linkedErrors = this._walkErrorTree(stackParser, hint.originalException as ExtendedError, this._key);
event.exception.values = [...linkedErrors, ...event.exception.values];
return event;
}

/**
* @inheritDoc
*/
private _walkErrorTree(
stackParser: StackParser,
error: ExtendedError,
key: string,
stack: Exception[] = [],
): Exception[] {
if (!isInstanceOf(error[key], Error) || stack.length + 1 >= this._limit) {
return stack;
}

const exception = exceptionFromError(stackParser, error[key]);

return this._walkErrorTree(stackParser, error[key], key, [exception, ...stack]);
}
}
Loading

0 comments on commit 1d8c81f

Please sign in to comment.