Skip to content

Commit 1ee7ce6

Browse files
committed
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: #28058 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent ecb963d commit 1ee7ce6

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

‎lib/internal/assert/assertion_error.js‎

Lines changed: 27 additions & 1 deletion
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+
consttmpActual=this.actual;
434+
consttmpExpected=this.expected;
435+
436+
for(constnameof['actual','expected']){
437+
if(typeofthis[name]==='string'){
438+
constlines=this[name].split('\n');
439+
if(lines.length>10){
440+
lines.length=10;
441+
this[name]=`${lines.join('\n')}\n...`;
442+
}elseif(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-
returninspect(this,{ ...ctx,customInspect: false,depth: 0});
452+
constresult=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+
returnresult;
437463
}
438464
}
439465

0 commit comments

Comments
 (0)