Bug 1284232 - Guard against ill-behaved objects on error check; r=automatedtester

It turns out that certain objects such as a DOMRectList have peculiarities
that cause string comparison to throw. This is normally not a problem
as error.isError is usually passed JSON serialisable data. But when it
is not, this try condition helps diagnose problems.

MozReview-Commit-ID: BLNSziwfxXs

--HG--
extra : rebase_source : 8bad973a20d8b69fa27f5de66e4ea287d4bcddcd
This commit is contained in:
Andreas Tolfsen 2016-07-08 13:58:04 +01:00
Родитель ddc9f80240
Коммит 7e156be01e
2 изменённых файлов: 18 добавлений и 25 удалений

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

@ -29,21 +29,21 @@ const ERRORS = [
"WebDriverError",
];
const BUILTIN_ERRORS = new Set([
"Error",
"EvalError",
"InternalError",
"RangeError",
"ReferenceError",
"SyntaxError",
"TypeError",
"URIError",
]);
this.EXPORTED_SYMBOLS = ["error"].concat(ERRORS);
this.error = {};
error.BuiltinErrors = {
Error: 0,
EvalError: 1,
InternalError: 2,
RangeError: 3,
ReferenceError: 4,
SyntaxError: 5,
TypeError: 6,
URIError: 7,
};
/**
* Checks if obj is an instance of the Error prototype in a safe manner.
* Prefer using this over using instanceof since the Error prototype
@ -61,7 +61,13 @@ error.isError = function(val) {
} else if (val instanceof Ci.nsIException) {
return true;
} else {
return Object.getPrototypeOf(val) in error.BuiltinErrors;
// DOMRectList errors on string comparison
try {
let proto = Object.getPrototypeOf(val);
return BUILTIN_ERRORS.has(proto.toString());
} catch (e) {
return false;
}
}
};

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

@ -10,19 +10,6 @@ function notok(condition) {
ok(!(condition));
}
add_test(function test_BuiltinErrors() {
ok("Error" in error.BuiltinErrors);
ok("EvalError" in error.BuiltinErrors);
ok("InternalError" in error.BuiltinErrors);
ok("RangeError" in error.BuiltinErrors);
ok("ReferenceError" in error.BuiltinErrors);
ok("SyntaxError" in error.BuiltinErrors);
ok("TypeError" in error.BuiltinErrors);
ok("URIError" in error.BuiltinErrors);
run_next_test();
});
add_test(function test_isError() {
notok(error.isError(null));
notok(error.isError([]));