Skip to content

Commit d2dddb5

Browse files
committed
Fix duplicate error name in messages
1 parent cde7aac commit d2dddb5

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 7.1.2
2+
3+
## Bug fixes
4+
5+
- When wrapping an error with a different class but the same `error.name`, do
6+
not repeat that name in the error message
7+
18
# 7.1.1
29

310
## Bug fixes

src/merge/prefix.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { isSubclass } from '../utils/subclass.js'
1212
// - When the parent is a subclass of the child, since the new class becomes
1313
// the subclass, which already contains the other class in its chain, i.e.
1414
// not worth adding to the message
15+
// - When the error classes are different but have the same name.
16+
// This is common when a library wraps another one with identical classes.
17+
// For example, a CLI wrapping a programmatic library.
1518
export const shouldPrefixCause = (error, ErrorClass) => {
1619
const { cause } = error
1720
return (
@@ -30,7 +33,8 @@ const hasUsefulName = (cause, ErrorClass) =>
3033
!(
3134
isSubclass(cause.constructor, ErrorClass) ||
3235
isSubclass(ErrorClass, cause.constructor) ||
33-
cause.constructor.name in globalThis
36+
cause.constructor.name in globalThis ||
37+
cause.name === ErrorClass.name
3438
)
3539

3640
// Prefix `cause.name` to its `message`

src/merge/prefix.test.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ each(ErrorClasses, ({ title }, ErrorClass) => {
9595
const cause = new ErrorClass('causeMessage')
9696
t.is(new TestError('', { cause }).message, cause.message)
9797
})
98+
99+
test(`Name of cause with same name is ignored | ${title}`, (t) => {
100+
const TestError = ErrorClass.subclass('TestError')
101+
const OtherError = ErrorClass.subclass('TestError')
102+
const cause = new OtherError('causeMessage')
103+
t.is(new TestError('', { cause }).message, cause.message)
104+
})
105+
106+
test(`Handle invalid error message | ${title}`, (t) => {
107+
const TestError = ErrorClass.subclass('TestError')
108+
const OtherError = ErrorClass.subclass('OtherError')
109+
const error = new OtherError('')
110+
error.message = true
111+
t.is(new TestError('', { cause: error }).message, error.name)
112+
})
98113
})
99114

100115
const getExpectedMessage = (cause) => {
@@ -132,13 +147,3 @@ each(
132147
)
133148

134149
const GENERATED_STACK_HINT = 'normalize-exception'
135-
136-
each(ErrorClasses, ({ title }, ErrorClass) => {
137-
test(`Handle invalid error message | ${title}`, (t) => {
138-
const TestError = ErrorClass.subclass('TestError')
139-
const OtherError = ErrorClass.subclass('OtherError')
140-
const error = new OtherError('')
141-
error.message = true
142-
t.is(new TestError('', { cause: error }).message, error.name)
143-
})
144-
})

0 commit comments

Comments
 (0)