Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Truncate long source context lines #329

Merged
merged 2 commits into from
Jun 7, 2017
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage/
test/instrumentation/node-*
test/manual/largeModule.js
test/fixtures/*
30 changes: 27 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,30 @@ function readSourceFiles(filenames, cb) {
});
}

// This is basically just `trim_line` from https://github.com/getsentry/sentry/blob/master/src/sentry/lang/javascript/processor.py#L67
function snipLine(line, colno) {
var ll = line.length;
if (ll <= 150) return line;
if (colno > ll) colno = ll;

var start = Math.max(colno - 60, 0);
if (start < 5) start = 0;

var end = Math.min(start + 140, ll);
if (end > ll - 5) end = ll;
if (end === ll) start = Math.max(end - 140, 0);

line = line.slice(start, end);
if (start > 0) line = '{snip} ' + line;
if (end < ll) line += ' {snip}';

return line;
}

function snipLine0(line) {
return snipLine(line, 0);
}

function parseStack(err, cb) {
if (!err) return cb([]);

Expand Down Expand Up @@ -208,9 +232,9 @@ function parseStack(err, cb) {
if (frame.filename && sourceFiles[frame.filename]) {
var lines = sourceFiles[frame.filename];
try {
frame.pre_context = lines.slice(Math.max(0, frame.lineno - (LINES_OF_CONTEXT + 1)), frame.lineno - 1);
frame.context_line = lines[frame.lineno - 1];
frame.post_context = lines.slice(frame.lineno, frame.lineno + LINES_OF_CONTEXT);
frame.pre_context = lines.slice(Math.max(0, frame.lineno - (LINES_OF_CONTEXT + 1)), frame.lineno - 1).map(snipLine0);
frame.context_line = snipLine(lines[frame.lineno - 1], frame.colno);
frame.post_context = lines.slice(frame.lineno, frame.lineno + LINES_OF_CONTEXT).map(snipLine0);
} catch (e) {
// anomaly, being defensive in case
// unlikely to ever happen in practice but can definitely happen in theory
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/long-line.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions test/raven.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ describe('raven.utils', function () {
}
});

it('should trim long source line in surrounding source context', function (done) {
var parseStack = raven.utils.parseStack;
var callback = function (frames) {
var frame = frames.pop();
frame.in_app.should.be.true;

var lineBefore = frame.pre_context[frame.pre_context.length - 1].trim();
lineBefore.should.not.startWith('{snip}');
lineBefore.should.endWith('{snip}');

var lineOf = frame.context_line.trim();
lineOf.should.startWith('{snip}');
lineOf.should.endWith('{snip}');
lineOf.length.should.equal(154); // 140 limit + 7 for `{snip} ` and ` {snip}`
lineOf.should.containEql("throw new Error('boom');");

var lineAfter = frame.post_context[0].trim();
lineAfter.should.not.startWith('{snip}');
lineAfter.should.endWith('{snip}');
done();
};
try {
require('./fixtures/long-line')();
} catch (e) {
parseStack(e, callback);
}
});

it('should treat windows files as being in app: in_app should be true', function (done) {
var parseStack = raven.utils.parseStack;
var callback = function (frames) {
Expand Down