Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a5710b1

Browse files
author
Thomas Grainger
committed
provide isError and use it in place of instanceof Error
Fixes #15868
1 parent f403925 commit a5710b1

File tree

8 files changed

+61
-4
lines changed

8 files changed

+61
-4
lines changed

src/.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"isNumber": false,
5151
"isNumberNaN": false,
5252
"isDate": false,
53+
"isError": false,
5354
"isArray": false,
5455
"isFunction": false,
5556
"isRegExp": false,

src/Angular.js

+18
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
isNumber,
4646
isNumberNaN,
4747
isDate,
48+
isError,
4849
isArray,
4950
isFunction,
5051
isRegExp,
@@ -673,6 +674,23 @@ function isDate(value) {
673674
*/
674675
var isArray = Array.isArray;
675676

677+
/**
678+
* @description
679+
* Determines if a reference is an `Error`.
680+
*
681+
* @param {*} value Reference to check.
682+
* @returns {boolean} True if `value` is an `Error`.
683+
*/
684+
function isError(value) {
685+
var tag = toString.call(value);
686+
switch (tag) {
687+
case '[object Error]': return true;
688+
case '[object Exception]': return true;
689+
case '[object DOMException]': return true;
690+
default: return value instanceof Error;
691+
}
692+
}
693+
676694
/**
677695
* @ngdoc function
678696
* @name angular.isFunction

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3098,7 +3098,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
30983098
}
30993099
linkQueue = null;
31003100
}).catch(function(error) {
3101-
if (error instanceof Error) {
3101+
if (isError(error)) {
31023102
$exceptionHandler(error);
31033103
}
31043104
});

src/ng/log.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function $LogProvider() {
132132
};
133133

134134
function formatError(arg) {
135-
if (arg instanceof Error) {
135+
if (isError(arg)) {
136136
if (arg.stack && formatStackTrace) {
137137
arg = (arg.message && arg.stack.indexOf(arg.message) === -1)
138138
? 'Error: ' + arg.message + '\n' + arg.stack

src/ng/q.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
381381
if (!toCheck.pur) {
382382
toCheck.pur = true;
383383
var errorMessage = 'Possibly unhandled rejection: ' + toDebugString(toCheck.value);
384-
if (toCheck.value instanceof Error) {
384+
if (isError(toCheck.value)) {
385385
exceptionHandler(toCheck.value, errorMessage);
386386
} else {
387387
exceptionHandler(errorMessage);

test/.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"isNumber": false,
6565
"isNumberNaN": false,
6666
"isDate": false,
67+
"isError": false,
6768
"isArray": false,
6869
"isFunction": false,
6970
"isRegExp": false,

test/AngularSpec.js

+37
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,43 @@ describe('angular', function() {
18611861
});
18621862
});
18631863

1864+
describe('isError', function() {
1865+
function testErrorFromDifferentContext(createError) {
1866+
var iframe = document.createElement('iframe');
1867+
document.body.appendChild(iframe);
1868+
try {
1869+
var error = createError(iframe.contentWindow);
1870+
expect(isError(error)).toBe(true);
1871+
} finally {
1872+
iframe.parentElement.removeChild(iframe);
1873+
}
1874+
}
1875+
1876+
it('should not assume objects are errors', function() {
1877+
expect(isError({ message: 'A fake error', stack: 'no stack here'}))
1878+
.toBe(false);
1879+
});
1880+
1881+
it('should detect simple error instances', function() {
1882+
expect(isError(new Error())).toBe(true);
1883+
});
1884+
1885+
it('should detect errors from another context', function() {
1886+
testErrorFromDifferentContext(function(win) {
1887+
return new win.Error();
1888+
});
1889+
});
1890+
1891+
it('should detect DOMException errors from another context', function() {
1892+
testErrorFromDifferentContext(function(win) {
1893+
try {
1894+
win.document.querySelectorAll('');
1895+
} catch (e) {
1896+
return e;
1897+
}
1898+
});
1899+
});
1900+
});
18641901

18651902
describe('isRegExp', function() {
18661903
it('should return true for RegExp object', function() {

test/ng/qSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('q', function() {
3737
// The following private functions are used to help with logging for testing invocation of the
3838
// promise callbacks.
3939
function _argToString(arg) {
40-
return (typeof arg === 'object' && !(arg instanceof Error)) ? toJson(arg) : '' + arg;
40+
return (typeof arg === 'object' && !(isError(arg))) ? toJson(arg) : '' + arg;
4141
}
4242

4343
function _argumentsToString(args) {

0 commit comments

Comments
 (0)