Bug 966681: improve robustness of error reporting. r=Unfocused

This commit is contained in:
Mike de Boer 2014-02-04 00:09:28 +01:00
Родитель 7e7788903b
Коммит 453d6a09c2
2 изменённых файлов: 26 добавлений и 3 удалений

Просмотреть файл

@ -57,9 +57,21 @@ function truncate(text, newLength = kTruncateLength) {
}
function getMessage(error) {
return truncate(JSON.stringify(error.actual, replacer)) + " " +
(error.operator ? error.operator + " " : "") +
truncate(JSON.stringify(error.expected, replacer));
let actual, expected;
// Wrap calls to JSON.stringify in try...catch blocks, as they may throw. If
// so, fall back to toString().
try {
actual = JSON.stringify(error.actual, replacer);
} catch (ex) {
actual = Object.prototype.toString.call(error.actual);
}
try {
expected = JSON.stringify(error.expected, replacer);
} catch (ex) {
expected = Object.prototype.toString.call(error.expected);
}
return truncate(actual) + " " + (error.operator ? error.operator + " " : "") +
truncate(expected);
}
/**

Просмотреть файл

@ -288,4 +288,15 @@ function run_test() {
deepEqual(/a/igm, /a/igm, "deep equal should work on RegExp");
deepEqual({a: 4, b: "1"}, {b: "1", a: 4}, "deep equal should work on regular Object");
deepEqual(a1, a2, "deep equal should work on Array with Object properties");
// Test robustness of reporting:
equal(new ns.Assert.AssertionError({
actual: {
toJSON: function() {
throw "bam!";
}
},
expected: "foo",
operator: "="
}).message, "[object Object] = \"foo\"");
}