Skip to content

Commit a3ea54f

Browse files
BridgeARTrott
authored andcommittedJun 13, 2019
assert: limit string inspection when logging assertion errors
This makes sure long strings as `actual` or `expected` values on an `AssertionError` won't be logged completely. This is important as the actual value is somewhat redundant in combination with the error message which already logs the difference between the input values. PR-URL: nodejs#28058 Reviewed-By: Rich Trott <[email protected]>
1 parent f7ffa52 commit a3ea54f

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed
 

‎lib/internal/assert/assertion_error.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,37 @@ class AssertionError extends Error {
429429
}
430430

431431
[inspect.custom](recurseTimes, ctx) {
432+
// Long strings should not be fully inspected.
433+
const tmpActual = this.actual;
434+
const tmpExpected = this.expected;
435+
436+
for (const name of ['actual', 'expected']) {
437+
if (typeof this[name] === 'string') {
438+
const lines = this[name].split('\n');
439+
if (lines.length > 10) {
440+
lines.length = 10;
441+
this[name] = `${lines.join('\n')}\n...`;
442+
} else if (this[name].length > 512) {
443+
this[name] = `${this[name].slice(512)}...`;
444+
}
445+
}
446+
}
447+
432448
// This limits the `actual` and `expected` property default inspection to
433449
// the minimum depth. Otherwise those values would be too verbose compared
434450
// to the actual error message which contains a combined view of these two
435451
// input values.
436-
return inspect(this, { ...ctx, customInspect: false, depth: 0 });
452+
const result = inspect(this, {
453+
...ctx,
454+
customInspect: false,
455+
depth: 0
456+
});
457+
458+
// Reset the properties after inspection.
459+
this.actual = tmpActual;
460+
this.expected = tmpExpected;
461+
462+
return result;
437463
}
438464
}
439465

0 commit comments

Comments
 (0)
Please sign in to comment.