Skip to content

Commit

Permalink
Don't fail when a value can't be converted to a primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid authored and gcanti committed Mar 11, 2024
1 parent 50e8e63 commit 6e9f926
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,11 @@ export const toUnion: <E, A>(fa: Either<E, A>) => E | A = /*#__PURE__*/ foldW(id
* @since 2.0.0
*/
export function toError(e: unknown): Error {
return e instanceof Error ? e : new Error(String(e))
try {
return e instanceof Error ? e : new Error(String(e))
} catch (error) {
return new Error()
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions test/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,20 @@ describe.concurrent('Either', () => {
}, _.toError),
_.left(new Error('string error'))
)

U.deepStrictEqual(
_.tryCatch(() => {
throw Object.create(null)
}, _.toError),
_.left(new Error())
)

U.deepStrictEqual(
_.tryCatch(() => {
throw { toString: [] }
}, _.toError),
_.left(new Error())
)
})

describe.concurrent('getEq', () => {
Expand Down

0 comments on commit 6e9f926

Please sign in to comment.