Skip to content

Commit

Permalink
fix: Correctly detect and remove wrapped function frames
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Nov 27, 2018
1 parent 17a965c commit 3ce404b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
8 changes: 7 additions & 1 deletion packages/browser/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ export default [
// Uglify has to be at the end of compilation, BUT before the license banner
plugins: bundleConfig.plugins
.slice(0, -1)
.concat(uglify())
.concat(
uglify({
mangle: {
reserved: ['addBreadcrumb', 'captureException', 'captureMessage', 'sentryWrapped'],
},
}),
)
.concat(bundleConfig.plugins.slice(-1)),
}),
];
18 changes: 9 additions & 9 deletions packages/browser/src/integrations/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub, withScope } from '@sentry/core';
import { captureException, getCurrentHub, withScope } from '@sentry/core';
import { Mechanism, SentryEvent, SentryWrappedFunction } from '@sentry/types';
import { isFunction } from '@sentry/utils/is';
import { htmlTreeAsString } from '@sentry/utils/misc';
Expand Down Expand Up @@ -57,7 +57,7 @@ export function wrap(
return fn;
}

const wrapped: SentryWrappedFunction = function(this: any): void {
const sentryWrapped: SentryWrappedFunction = function(this: any): void {
if (before && isFunction(before)) {
before.apply(this, arguments);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export function wrap(
return processedEvent;
});

getCurrentHub().captureException(ex, { originalException: ex });
captureException(ex);
});

throw ex;
Expand All @@ -108,20 +108,20 @@ export function wrap(
try {
for (const property in fn) {
if (Object.prototype.hasOwnProperty.call(fn, property)) {
wrapped[property] = fn[property];
sentryWrapped[property] = fn[property];
}
}
} catch (_oO) {} // tslint:disable-line:no-empty

wrapped.prototype = fn.prototype;
fn.__sentry_wrapped__ = wrapped;
sentryWrapped.prototype = fn.prototype;
fn.__sentry_wrapped__ = sentryWrapped;

// Signal that this function has been wrapped/filled already
// for both debugging and to prevent it to being wrapped/filled twice
wrapped.__sentry__ = true;
wrapped.__sentry_original__ = fn;
sentryWrapped.__sentry__ = true;
sentryWrapped.__sentry_original__ = fn;

return wrapped;
return sentryWrapped;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/src/integrations/pluggable/ember.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureMessage, getCurrentHub, Scope, withScope } from '@sentry/core';
import { captureException, captureMessage, getCurrentHub, Scope, withScope } from '@sentry/core';
import { Integration, SentryEvent } from '@sentry/types';
import { getGlobalObject } from '@sentry/utils/misc';

Expand Down Expand Up @@ -43,7 +43,7 @@ export class Ember implements Integration {
if (getCurrentHub().getIntegration(Ember)) {
withScope(scope => {
this.addIntegrationToSdkInfo(scope);
getCurrentHub().captureException(error, { originalException: error });
captureException(error);
});
}

Expand All @@ -62,7 +62,7 @@ export class Ember implements Integration {
if (reason instanceof Error) {
scope.setExtra('context', 'Unhandled Promise error detected');
this.addIntegrationToSdkInfo(scope);
getCurrentHub().captureException(reason, { originalException: reason });
captureException(reason);
} else {
scope.setExtra('reason', reason);
this.addIntegrationToSdkInfo(scope);
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/integrations/pluggable/vue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub, withScope } from '@sentry/core';
import { captureException, getCurrentHub, withScope } from '@sentry/core';
import { Integration, SentryEvent } from '@sentry/types';
import { isPlainObject, isUndefined } from '@sentry/utils/is';
import { getGlobalObject } from '@sentry/utils/misc';
Expand Down Expand Up @@ -91,7 +91,7 @@ export class Vue implements Integration {
return event;
});

getCurrentHub().captureException(error, { originalException: error });
captureException(error);
});
}

Expand Down
8 changes: 8 additions & 0 deletions packages/browser/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ export function prepareFramesForEvent(stack: TraceKitStackFrame[]): StackFrame[]
}

let localStack = stack;

const firstFrameFunction = localStack[0].func || '';
const lastFrameFunction = localStack[localStack.length - 1].func || '';

// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
if (includes(firstFrameFunction, 'captureMessage') || includes(firstFrameFunction, 'captureException')) {
localStack = localStack.slice(1);
}

// If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call)
if (includes(lastFrameFunction, 'sentryWrapped')) {
localStack = localStack.slice(0, -1);
}

// The frame where the crash happened, should be the last entry in the array
return localStack
.map(
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { captureException, getCurrentHub } from '@sentry/core';
import { SentryEvent } from '@sentry/types';
import { forget } from '@sentry/utils/async';
import { logger } from '@sentry/utils/logger';
Expand Down Expand Up @@ -271,7 +271,7 @@ export function errorHandler(): (
next(error);
return;
}
getCurrentHub().captureException(error, { originalException: error });
captureException(error);
next(error);
};
}
Expand Down

0 comments on commit 3ce404b

Please sign in to comment.