Skip to content

Commit

Permalink
[Breaking] ensure Error objects compare properly
Browse files Browse the repository at this point in the history
  • Loading branch information
khirayama authored and ljharb committed Apr 18, 2018
1 parent 0c25e94 commit ffb2522
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -16,6 +16,7 @@
"files": ["example/**", "test/**"],
"rules": {
"array-bracket-newline": 0,
"max-lines": 0,
"max-params": 0,
"max-statements": 0,
"no-console": 0,
Expand Down
10 changes: 9 additions & 1 deletion index.js
Expand Up @@ -50,7 +50,7 @@ function isBuffer(x) {
}

function objEquiv(a, b, opts) {
/* eslint max-statements: [2, 50] */
/* eslint max-statements: [2, 70] */
var i, key;
if (typeof a !== typeof b) { return false; }
if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) { return false; }
Expand All @@ -60,6 +60,14 @@ function objEquiv(a, b, opts) {

if (isArguments(a) !== isArguments(b)) { return false; }

// TODO: replace when a cross-realm brand check is available
var aIsError = a instanceof Error;
var bIsError = b instanceof Error;
if (aIsError !== bIsError) { return false; }
if (aIsError || bIsError) {
if (a.name !== b.name || a.message !== b.message) { return false; }
}

var aIsRegex = isRegex(a);
var bIsRegex = isRegex(b);
if (aIsRegex !== bIsRegex) { return false; }
Expand Down
8 changes: 8 additions & 0 deletions test/cmp.js
Expand Up @@ -342,3 +342,11 @@ test('functions', function (t) {

t.end();
});

test('Errors', function (t) {
t.deepEqualTest(new Error('xyz'), new Error('xyz'), 'two errors of the same type with the same message', true, true, false);
t.deepEqualTest(new Error('xyz'), new TypeError('xyz'), 'two errors of different types with the same message', false, false);
t.deepEqualTest(new Error('xyz'), new Error('zyx'), 'two errors of the same type with a different message', false, false);

t.end();
});

0 comments on commit ffb2522

Please sign in to comment.