Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent passing a specific error type for .fromThrowable without providing an error mapping function #583

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-hats-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'neverthrow': minor
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be considered a breaking change, since users that use the function like described in the PR will get red squiggles. However, I would argue that this is a "type bug", and shouldn't be considered a valid use case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree to consider this a bug fix

---

prevent passing a specific error type for `.fromThrowable` without providing an error mapping function
13 changes: 11 additions & 2 deletions src/result-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,26 @@ export class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
return new ResultAsync(newPromise)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
static fromThrowable<A extends readonly any[], R>(
fn: (...args: A) => Promise<R>,
): (...args: A) => ResultAsync<R, unknown>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static fromThrowable<A extends readonly any[], R, E>(
fn: (...args: A) => Promise<R>,
errorFn: (err: unknown) => E,
): (...args: A) => ResultAsync<R, E>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static fromThrowable<A extends readonly any[], R, E>(
fn: (...args: A) => Promise<R>,
errorFn?: (err: unknown) => E,
): (...args: A) => ResultAsync<R, E> {
): (...args: A) => ResultAsync<R, E | unknown> {
return (...args) => {
return new ResultAsync(
(async () => {
try {
return new Ok(await fn(...args))
} catch (error) {
} catch (error: unknown) {
return new Err(errorFn ? errorFn(error) : error)
}
})(),
Expand Down
21 changes: 18 additions & 3 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,33 @@ export namespace Result {
* arguments but returning `Ok` if successful, `Err` if the function throws
*
* @param fn function to wrap with ok on success or err on failure
* @param errorFn when an error is thrown, this will wrap the error result if provided
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function fromThrowable<Fn extends (...args: readonly any[]) => any>(
fn: Fn,
): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, unknown>
/**
* Wraps a function with a try catch, creating a new function with the same
* arguments but returning `Ok` if successful, `Err` if the function throws
*
* @param fn function to wrap with ok on success or err on failure
* @param errorFn when an error is thrown, this will wrap the error result
*/
Comment on lines +25 to +31
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the most elegant to have to maintain two versions of the documentation, however I'm not aware of any other way of doing it. Please let me know if there's a better way!

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function fromThrowable<Fn extends (...args: readonly any[]) => any, E>(
fn: Fn,
errorFn: (e: unknown) => E,
): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function fromThrowable<Fn extends (...args: readonly any[]) => any, E>(
fn: Fn,
errorFn?: (e: unknown) => E,
): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E> {
): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E | unknown> {
return (...args) => {
try {
const result = fn(...args)
return ok(result)
} catch (e) {
} catch (e: unknown) {
return err(errorFn ? errorFn(e) : e)
}
}
Expand Down