Skip to content

Commit bffc3d3

Browse files
khirayamaljharb
authored andcommitted
[Breaking] ensure Error objects compare properly
1 parent 0c5b21a commit bffc3d3

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"files": ["example/**", "test/**"],
1717
"rules": {
1818
"array-bracket-newline": 0,
19+
"max-lines": 0,
1920
"max-statements": 0,
2021
"no-console": 0,
2122
"no-magic-numbers": 0,

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function isBuffer(x) {
5050
}
5151

5252
function objEquiv(a, b, opts) {
53-
/* eslint max-statements: [2, 50] */
53+
/* eslint max-statements: [2, 70] */
5454
var i, key;
5555
if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) { return false; }
5656

@@ -59,6 +59,13 @@ function objEquiv(a, b, opts) {
5959

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

62+
var aIsError = a instanceof Error;
63+
var bIsError = b instanceof Error;
64+
if (aIsError !== bIsError) { return false; }
65+
if (aIsError || bIsError) {
66+
if (a.name !== b.name || a.message !== b.message) { return false; }
67+
}
68+
6269
var aIsRegex = isRegex(a);
6370
var bIsRegex = isRegex(b);
6471
if (aIsRegex !== bIsRegex) { return false; }

test/cmp.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,16 @@ test('arrays and objects', function (t) {
316316

317317
t.end();
318318
});
319+
320+
test('Errors', function (t) {
321+
t.ok(equal(new Error('xyz'), new Error('xyz')), 'two errors of the same type with the same message are equal');
322+
t.ok(equal(new Error('xyz'), new Error('xyz'), { strict: true }), 'strict: two errors of the same type with the same message are equal');
323+
324+
t.notOk(equal(new Error('xyz'), new TypeError('xyz')), 'two errors of different types with the same message are not equal');
325+
t.notOk(equal(new TypeError('xyz'), new Error('xyz'), { strict: true }), 'strict: two errors of different types with the same message are not equal');
326+
327+
t.notOk(equal(new Error('xyz'), new Error('zyx')), 'two errors of the same type with a different message are not equal');
328+
t.notOk(equal(new Error('xyz'), new Error('zyx'), { strict: true }), 'strict: two errors of the same type with a different message are not equal');
329+
330+
t.end();
331+
});

0 commit comments

Comments
 (0)