Skip to content

Commit

Permalink
Merge pull request #851 from rollbar/wj-stack-trace-limit
Browse files Browse the repository at this point in the history
Allow configuration of stack trace limit
  • Loading branch information
waltjones authored May 12, 2020
2 parents 029e67d + 62a6cba commit 942445a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/angular2/src/app/rollbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const rollbarConfig:Rollbar.Configuration = {
ignoreDuplicateErrors: true,
wrapGlobalEventHandlers: false,
scrubRequestBody: true,
exitOnUncaughtException: false
exitOnUncaughtException: false,
stackTraceLimit: 20
};

export const RollbarService = new InjectionToken<Rollbar>('rollbar');
Expand Down
9 changes: 9 additions & 0 deletions examples/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
// is true in the config.
throw new DOMException('test DOMException');
};
// Deep stack error
window.throwDeepStackError = function throwDeepStackError(curr, limit) {
if (curr < limit) {
throwDeepStackError(curr + 1, limit);
} else {
throw new Error('deep stack error');
}
};
</script>
</head>
<body>
Expand All @@ -26,4 +34,5 @@ <h1>
</div>
<button id="throw-error" onclick="throwError()">Throw Error</button>
<button id="throw-dom-exception" onclick="throwDomException()">Throw DOMException</button>
<button id="throw-depp-stack-error" onclick="throwDeepStackError(0,20)">Throw deep stack Error</button>
</html>
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ declare namespace Rollbar {
scrubRequestBody?: boolean;
scrubTelemetryInputs?: boolean;
sendConfig?: boolean;
stackTraceLimit?: number;
telemetryScrubber?: TelemetryScrubber;
transform?: (data: object) => void;
transmit?: boolean;
Expand Down
11 changes: 11 additions & 0 deletions src/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function Rollbar(options, api, logger, platform) {
this.queue = new Queue(Rollbar.rateLimiter, api, logger, this.options);
this.notifier = new Notifier(this.queue, this.options);
this.telemeter = new Telemeter(this.options);
setStackTraceLimit(options);
this.lastError = null;
this.lastErrorHash = 'none';
}
Expand All @@ -45,6 +46,7 @@ Rollbar.prototype.configure = function(options, payloadData) {
this.options = _.merge(oldOptions, options, payload);
this.notifier && this.notifier.configure(this.options);
this.telemeter && this.telemeter.configure(this.options);
setStackTraceLimit(options);
this.global(this.options);
return this;
};
Expand Down Expand Up @@ -151,4 +153,13 @@ function generateItemHash(item) {
return message + '::' + stack;
}

// Node.js, Chrome, Safari, and some other browsers support this property
// which globally sets the number of stack frames returned in an Error object.
// If a browser can't use it, no harm done.
function setStackTraceLimit(options) {
if (options.stackTraceLimit) {
Error.stackTraceLimit = options.stackTraceLimit;
}
}

module.exports = Rollbar;
33 changes: 33 additions & 0 deletions test/browser.rollbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,39 @@ describe('options.captureUncaught', function() {
done();
});

it('should capture exta frames when stackTraceLimit is set', function(done) {
var server = window.server;
stubResponse(server);
server.requests.length = 0;

var oldLimit = Error.stackTraceLimit;
var options = {
accessToken: 'POST_CLIENT_ITEM_TOKEN',
captureUncaught: true,
stackTraceLimit: 50
};
var rollbar = window.rollbar = new Rollbar(options);

var element = document.getElementById('throw-depp-stack-error');
element.click();
server.respond();

var body = JSON.parse(server.requests[0].requestBody);

expect(body.access_token).to.eql('POST_CLIENT_ITEM_TOKEN');
expect(body.data.body.trace.exception.message).to.eql('deep stack error');
expect(body.data.body.trace.frames.length).to.be.above(20);

// karma doesn't unload the browser between tests, so the onerror handler
// will remain installed. Unset captureUncaught so the onerror handler
// won't affect other tests.
rollbar.configure({
captureUncaught: false,
stackTraceLimit: oldLimit // reset to default
});

done();
});
});

describe('options.captureUnhandledRejections', function() {
Expand Down

0 comments on commit 942445a

Please sign in to comment.