-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from janglad/map-err
mapErr()
- Loading branch information
Showing
28 changed files
with
837 additions
and
1,134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"safe-fn": patch | ||
--- | ||
|
||
- add `mapErr()` function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"safe-fn": patch | ||
--- | ||
|
||
- removes asAction, all errors are now stripped by default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"safe-fn": patch | ||
--- | ||
|
||
- Use child `.catch()` handler if parent didn't define one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"safe-fn-react": patch | ||
"safe-fn": patch | ||
--- | ||
|
||
- Rename some Infer types, see docs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
title: Modifying result type | ||
--- | ||
|
||
Often times you want to filter out some specific errors from the client. Typically you'll handle this by calling NeverThrow's own `mapErr()` on your returned/yielded results, however sometimes it can be handy to do this globally for your SafeFn. | ||
You can modify the error type of the SafeFn by using `.mapErr()`. This is a function that takes in an argument of the original error type (see ), and returns a value that will set the new error type. | ||
|
||
As an example: | ||
|
||
```ts | ||
// Fake function declaration | ||
declare const generateText: ( | ||
prompt: string, | ||
) => ResultAsync<string, { code: "NO_CREDITS_REMAINING" }>; | ||
|
||
const generateTodo = createSafeFn() | ||
.input(z.object({ prompt: z.string() })) | ||
.safeHandler(async function* (args) { | ||
const todo = yield* generateText( | ||
`Make todo based on ${args.input.prompt}`, | ||
).safeUnwrap(); | ||
return ok(todo); | ||
}) | ||
.mapErr((e) => { | ||
if (e.code === "NO_CREDITS_REMAINING") { | ||
// Don't let em know funds are running low lmao | ||
return { | ||
code: "UNKNOWN_ERROR", | ||
} as const; | ||
} | ||
return e; | ||
}); | ||
``` | ||
|
||
The possible `Error` type when running this function will be | ||
|
||
```ts | ||
type Res = | ||
| { | ||
code: "UNKNOWN_ERROR"; | ||
} | ||
| { | ||
code: "INPUT_PARSING"; | ||
cause: ...; | ||
} | ||
| { code: "UNCAUGHT_ERROR"; cause: ... }; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.