Skip to content

Commit 8e111ff

Browse files
committed
Add surrounding source truncation for long lines
1 parent b612822 commit 8e111ff

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

lib/utils.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ function readSourceFiles(filenames, cb) {
162162
});
163163
}
164164

165+
// This is basically just `trim_line` from https://github.com/getsentry/sentry/blob/master/src/sentry/lang/javascript/processor.py#L67
166+
function snipLine(line, colno) {
167+
var ll = line.length;
168+
if (ll <= 150) return line;
169+
if (colno > ll) colno = ll;
170+
171+
var start = Math.max(colno - 60, 0);
172+
if (start < 5) start = 0;
173+
174+
var end = Math.min(start + 140, ll);
175+
if (end > ll - 5) end = ll;
176+
if (end === ll) start = Math.max(end - 140, 0);
177+
178+
line = line.slice(start, end);
179+
if (start > 0) line = '{snip} ' + line;
180+
if (end < ll) line += ' {snip}';
181+
182+
return line;
183+
}
184+
185+
function snipLine0(line) {
186+
return snipLine(line, 0);
187+
}
188+
165189
function parseStack(err, cb) {
166190
if (!err) return cb([]);
167191

@@ -208,9 +232,9 @@ function parseStack(err, cb) {
208232
if (frame.filename && sourceFiles[frame.filename]) {
209233
var lines = sourceFiles[frame.filename];
210234
try {
211-
frame.pre_context = lines.slice(Math.max(0, frame.lineno - (LINES_OF_CONTEXT + 1)), frame.lineno - 1);
212-
frame.context_line = lines[frame.lineno - 1];
213-
frame.post_context = lines.slice(frame.lineno, frame.lineno + LINES_OF_CONTEXT);
235+
frame.pre_context = lines.slice(Math.max(0, frame.lineno - (LINES_OF_CONTEXT + 1)), frame.lineno - 1).map(snipLine0);
236+
frame.context_line = snipLine(lines[frame.lineno - 1], frame.colno);
237+
frame.post_context = lines.slice(frame.lineno, frame.lineno + LINES_OF_CONTEXT).map(snipLine0);
214238
} catch (e) {
215239
// anomaly, being defensive in case
216240
// unlikely to ever happen in practice but can definitely happen in theory

0 commit comments

Comments
 (0)