mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Ensure _.isError works with subclasses values. [closes #1994]
This commit is contained in:
@@ -9761,8 +9761,11 @@
|
|||||||
* // => false
|
* // => false
|
||||||
*/
|
*/
|
||||||
function isError(value) {
|
function isError(value) {
|
||||||
return isObjectLike(value) &&
|
if (!isObjectLike(value)) {
|
||||||
typeof value.message == 'string' && objectToString.call(value) == errorTag;
|
return false;
|
||||||
|
}
|
||||||
|
var Ctor = value.constructor;
|
||||||
|
return typeof Ctor == 'function' && objectToString.call(Ctor.prototype) == errorTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13121,7 +13124,7 @@
|
|||||||
try {
|
try {
|
||||||
return apply(func, undefined, args);
|
return apply(func, undefined, args);
|
||||||
} catch (e) {
|
} 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); })
|
function(chr) { return /\s/.exec(chr); })
|
||||||
.join('');
|
.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.
|
* Extracts the unwrapped value from its wrapper.
|
||||||
*
|
*
|
||||||
@@ -375,18 +401,6 @@
|
|||||||
return result;
|
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`.
|
* Sets a non-enumerable property value on `object`.
|
||||||
*
|
*
|
||||||
@@ -1519,13 +1533,6 @@
|
|||||||
QUnit.test('should preserve custom errors', function(assert) {
|
QUnit.test('should preserve custom errors', function(assert) {
|
||||||
assert.expect(1);
|
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'); });
|
var actual = _.attempt(function() { throw new CustomError('x'); });
|
||||||
assert.ok(actual instanceof CustomError);
|
assert.ok(actual instanceof CustomError);
|
||||||
});
|
});
|
||||||
@@ -9219,6 +9226,12 @@
|
|||||||
assert.deepEqual(actual, expected);
|
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) {
|
QUnit.test('should return `false` for non error objects', function(assert) {
|
||||||
assert.expect(12);
|
assert.expect(12);
|
||||||
|
|
||||||
@@ -11012,11 +11025,11 @@
|
|||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
QUnit.test('should return `false` for subclassed values', function(assert) {
|
QUnit.test('should return `false` for subclassed values', function(assert) {
|
||||||
assert.expect(8);
|
assert.expect(7);
|
||||||
|
|
||||||
var funcs = [
|
var funcs = [
|
||||||
'isArray', 'isBoolean', 'isDate', 'isError',
|
'isArray', 'isBoolean', 'isDate', 'isFunction',
|
||||||
'isFunction', 'isNumber', 'isRegExp', 'isString'
|
'isNumber', 'isRegExp', 'isString'
|
||||||
];
|
];
|
||||||
|
|
||||||
lodashStable.each(funcs, function(methodName) {
|
lodashStable.each(funcs, function(methodName) {
|
||||||
|
|||||||
Reference in New Issue
Block a user