Skip to content

Support setting Error.stackTraceLimit automatically #419

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Jan 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions example/scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ function showDialog() {
broken();
Raven.showReportDialog();
}

function a() { b(); }
function b() { c(); }
function c() { d(); }
function d() { e(); }
function e() { f(); }
function f() { g(); }
function g() { h(); }
function h() { i(); }
function i() { j(); }
function j() { k(); }
function k() { l(); }
function l() { m(); }
function m() { n(); }
function n() { o(); }
function o() { throw new Error('dang'); }
8 changes: 6 additions & 2 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ function Raven() {
includePaths: [],
crossOrigin: 'anonymous',
collectWindowErrors: true,
maxMessageLength: 0
maxMessageLength: 0,
stackTraceLimit: 50
};
this._ignoreOnError = 0;
this._isRavenInstalled = false;
this._originalErrorStackTraceLimit = Error.stackTraceLimit;
// capture references to window.console *and* all its methods first
// before the console plugin has a chance to monkey patch
this._originalConsole = window.console || {};
Expand Down Expand Up @@ -164,6 +166,7 @@ Raven.prototype = {
this._isRavenInstalled = true;
}

Error.stackTraceLimit = this._globalOptions.stackTraceLimit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to modify this? Why is it not sufficient to just slice frames before transmitting?

Note this means that multiple error instances of Raven.js will clobber each other's Error.stackTraceLimit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-reads PR message

Oh, I see re: V8. Hmm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this only affects V8, and unfortunately, it's a global option.

V8 just defaults to a limiting, 10 frames. We've personally hit an issue with this in Sentry since React generates some long stack traces.

In theory, we'd push all browsers to capture Infinity frames, then slice() out of that, but that's not how reality works. :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let's go with 50.

return this;
},

Expand Down Expand Up @@ -269,6 +272,7 @@ Raven.prototype = {

this._restoreBuiltIns();

Error.stackTraceLimit = this._originalErrorStackTraceLimit;
this._isRavenInstalled = false;

return this;
Expand Down Expand Up @@ -757,7 +761,7 @@ Raven.prototype = {
stackInfo.message,
stackInfo.url,
stackInfo.lineno,
frames,
frames.slice(0, this._globalOptions.stackTraceLimit),
options
);
},
Expand Down
23 changes: 23 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,29 @@ describe('globals', function() {
'new <anonymous>', 'hey', 'http://example.com', 10, [], undefined
]);
});

it('should trim number of frames based on stackTraceLimit', function() {
var frame = {url: 'http://example.com'};
this.sinon.stub(Raven, '_normalizeFrame').returns(frame);
this.sinon.stub(Raven, '_processException');

var stackInfo = {
name: 'Matt',
message: 'hey',
url: 'http://example.com',
lineno: 10,
stack: [
frame, frame
]
};

Raven._globalOptions.stackTraceLimit = 1;

Raven._handleStackInfo(stackInfo);
assert.deepEqual(Raven._processException.lastCall.args, [
'Matt', 'hey', 'http://example.com', 10, [frame], undefined
]);
});
});
});

Expand Down