Skip to content

Commit d9a05ca

Browse files
committed
Fix BaseError.normalize() bug
1 parent f57ed4c commit d9a05ca

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# 7.1.3
2+
3+
## Bug fixes
4+
5+
- Fix
6+
[`BaseError.normalize(error, NewErrorClass)](README.md#errorclassnormalizeerror-newerrorclass).
7+
`error` is converted to `NewErrorClass` if it was an instance of a
8+
`BaseError`'s subclass. However, `error` was not converted when it was an
9+
instance of `BaseError` itself, which was incorrect.
10+
111
# 7.1.2
212

313
## Bug fixes

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,10 @@ _Return value_: `Error`
638638

639639
Normalizes [invalid errors](#invalid-errors).
640640

641-
If the `error`'s class is a subclass of `ErrorClass`, it is left as is.
642-
Otherwise, it is [converted to `NewErrorClass`](#normalizing-unknown-errors),
643-
which defaults to `ErrorClass` itself.
641+
If `error` is an instance of `ErrorClass` (or one of its subclasses), it is left
642+
as is. Otherwise, it is
643+
[converted to `NewErrorClass`](#normalizing-unknown-errors), which defaults to
644+
`ErrorClass` itself.
644645

645646
# Modules
646647

src/subclass/create.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ interface ErrorSubclassCore<
111111
/**
112112
* Normalizes invalid errors.
113113
*
114-
* If the `error`'s class is a subclass of `ErrorClass`, it is left as is.
115-
* Otherwise, it is converted to `NewErrorClass`, which defaults to
116-
* `ErrorClass` itself.
114+
* If `error` is an instance of `ErrorClass` (or one of its subclasses), it is
115+
* left as is. Otherwise, it is converted to `NewErrorClass`, which defaults
116+
* to `ErrorClass` itself.
117117
*
118118
* @example
119119
* ```js

src/subclass/normalize.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,4 @@ const normalizeAggregateErrors = ({
7373
}
7474

7575
const shouldKeepClass = (error, ErrorClass, UnknownError) =>
76-
error?.constructor === UnknownError ||
77-
(error instanceof ErrorClass && error.constructor !== ErrorClass)
76+
error?.constructor === UnknownError || error instanceof ErrorClass

src/subclass/normalize.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ each(ErrorClasses, ({ title }, ErrorClass) => {
7070
t.true(ErrorClass.normalize(error) instanceof ErrorClass)
7171
})
7272

73-
test(`ErrorClass.normalize(error, TestError) changes error class if same class as ErrorClass | ${title}`, (t) => {
73+
test(`ErrorClass.normalize(error, TestError) keeps error class if same class as ErrorClass | ${title}`, (t) => {
7474
const TestError = ErrorClass.subclass('TestError')
7575
const error = new ErrorClass('test')
76-
t.true(ErrorClass.normalize(error, TestError) instanceof TestError)
76+
t.false(ErrorClass.normalize(error, TestError) instanceof TestError)
7777
})
7878

7979
test(`ErrorClass.normalize(error, TestError) keeps error class if same class as TestError | ${title}`, (t) => {

0 commit comments

Comments
 (0)