mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 23:37:49 +00:00
Ensure _.isError works with subclasses values. [closes #1994]
This commit is contained in:
@@ -9761,8 +9761,11 @@
|
||||
* // => false
|
||||
*/
|
||||
function isError(value) {
|
||||
return isObjectLike(value) &&
|
||||
typeof value.message == 'string' && objectToString.call(value) == errorTag;
|
||||
if (!isObjectLike(value)) {
|
||||
return false;
|
||||
}
|
||||
var Ctor = value.constructor;
|
||||
return typeof Ctor == 'function' && objectToString.call(Ctor.prototype) == errorTag;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -13121,7 +13124,7 @@
|
||||
try {
|
||||
return apply(func, undefined, args);
|
||||
} catch (e) {
|
||||
return isObject(e) ? e : new Error(e);
|
||||
return isError(e) ? e : new Error(e);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
57
test/test.js
57
test/test.js
@@ -352,6 +352,32 @@
|
||||
function(chr) { return /\s/.exec(chr); })
|
||||
.join('');
|
||||
|
||||
/**
|
||||
* The custom error constructor.
|
||||
*
|
||||
* @private
|
||||
* @param {string} message The error message.
|
||||
* @returns {Object} Returns the new error object instance.
|
||||
*/
|
||||
function CustomError(message) {
|
||||
this.name = 'CustomError';
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
CustomError.prototype = lodashStable.create(Error.prototype);
|
||||
|
||||
/**
|
||||
* Removes all own enumerable properties from a given object.
|
||||
*
|
||||
* @private
|
||||
* @param {Object} object The object to empty.
|
||||
*/
|
||||
function emptyObject(object) {
|
||||
lodashStable.forOwn(object, function(value, key, object) {
|
||||
delete object[key];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the unwrapped value from its wrapper.
|
||||
*
|
||||
@@ -375,18 +401,6 @@
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all own enumerable properties from a given object.
|
||||
*
|
||||
* @private
|
||||
* @param {Object} object The object to empty.
|
||||
*/
|
||||
function emptyObject(object) {
|
||||
lodashStable.forOwn(object, function(value, key, object) {
|
||||
delete object[key];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a non-enumerable property value on `object`.
|
||||
*
|
||||
@@ -1519,13 +1533,6 @@
|
||||
QUnit.test('should preserve custom errors', function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
function CustomError(message) {
|
||||
this.name = 'CustomError';
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
CustomError.prototype = lodashStable.create(Error.prototype);
|
||||
|
||||
var actual = _.attempt(function() { throw new CustomError('x'); });
|
||||
assert.ok(actual instanceof CustomError);
|
||||
});
|
||||
@@ -9219,6 +9226,12 @@
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
QUnit.test('should return `true` for subclassed values', function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
assert.strictEqual(_.isError(new CustomError('x')), true);
|
||||
});
|
||||
|
||||
QUnit.test('should return `false` for non error objects', function(assert) {
|
||||
assert.expect(12);
|
||||
|
||||
@@ -11012,11 +11025,11 @@
|
||||
|
||||
(function() {
|
||||
QUnit.test('should return `false` for subclassed values', function(assert) {
|
||||
assert.expect(8);
|
||||
assert.expect(7);
|
||||
|
||||
var funcs = [
|
||||
'isArray', 'isBoolean', 'isDate', 'isError',
|
||||
'isFunction', 'isNumber', 'isRegExp', 'isString'
|
||||
'isArray', 'isBoolean', 'isDate', 'isFunction',
|
||||
'isNumber', 'isRegExp', 'isString'
|
||||
];
|
||||
|
||||
lodashStable.each(funcs, function(methodName) {
|
||||
|
||||
Reference in New Issue
Block a user