-
Notifications
You must be signed in to change notification settings - Fork 105
prevent passing a specific error type for .fromThrowable
without providing an error mapping function
#583
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
base: master
Are you sure you want to change the base?
prevent passing a specific error type for .fromThrowable
without providing an error mapping function
#583
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'neverthrow': minor | ||
--- | ||
|
||
prevent passing a specific error type for `.fromThrowable` without providing an error mapping function |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// 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) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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