|
1 | | -# just-error |
| 1 | +# just-error ⚠️ |
| 2 | + |
| 3 | + |
| 4 | + |
2 | 5 | A json-serializable, incredibly ergonomic way of declaring and working with errors in your TypeScript applications. |
| 6 | + |
| 7 | +```ts |
| 8 | +import * as justerror from "."; |
| 9 | + |
| 10 | +// Define your errors |
| 11 | + |
| 12 | +export type Error<T extends keyof typeof error> = ReturnType<(typeof error)[T]>; |
| 13 | + |
| 14 | +const error = justerror.create({ |
| 15 | + ApiError: { |
| 16 | + code: "API_001", |
| 17 | + message: (args: { url: string }) => `Error fetching ${args.url}`, |
| 18 | + }, |
| 19 | + EmailError: { |
| 20 | + code: "EMAIL_001", |
| 21 | + message: (args: { email: string }) => `Error sending email to ${args.email}`, |
| 22 | + }, |
| 23 | +}); |
| 24 | + |
| 25 | +// Write safe code |
| 26 | + |
| 27 | +import type { Result } from "neverthrow"; |
| 28 | +import type { Error, error } from "./errors"; |
| 29 | + |
| 30 | +function safeFn(): Result<string, Error<"ApiError"> | Error<"EmailError">> { |
| 31 | + // ... |
| 32 | +} |
| 33 | + |
| 34 | +const result = safeFn().match( |
| 35 | + (v) => console.log(v), |
| 36 | + (e) => { |
| 37 | + justerror.match(e, { |
| 38 | + ApiError: (e) => console.log(`We couldn't service your request to ${e.config.url}`), |
| 39 | + EmailError: () => console.error("We encountered an unexpected error"), |
| 40 | + }); |
| 41 | + } |
| 42 | +); |
| 43 | +``` |
| 44 | + |
| 45 | +## Introduction |
| 46 | + |
| 47 | +With the addition of libraries like [neverthrow](https://github.com/supermacro/neverthrow) the JS community has started to realize the power of "never throwing" and instead returning results. This library is a great way to compliment that pattern. |
| 48 | + |
| 49 | +## Getting Started |
| 50 | + |
| 51 | +Install `just-error`: |
| 52 | + |
| 53 | +```sh |
| 54 | +npm i just-error |
| 55 | +``` |
| 56 | + |
| 57 | +Define your errors: |
| 58 | + |
| 59 | +```ts |
| 60 | +import * as justerror from "just-error"; |
| 61 | + |
| 62 | +const error = justerror.create({ |
| 63 | + ApiError: { |
| 64 | + code: "API_001", |
| 65 | + message: (args: { url: string }) => `Error fetching ${args.url}`, |
| 66 | + }, |
| 67 | + EmailError: { |
| 68 | + code: "EMAIL_001", |
| 69 | + message: (args: { email: string }) => `Error sending email to ${args.email}`, |
| 70 | + }, |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +Create errors by calling them as functions: |
| 75 | + |
| 76 | +```ts |
| 77 | +import { ResultAsync } from "nevereverthrow"; |
| 78 | +import { error } from "./errors"; |
| 79 | + |
| 80 | +const url = "https://api.example.com/data"; |
| 81 | + |
| 82 | +const result = ResultAsync.fromPromise( |
| 83 | + () => fetch(url), |
| 84 | + (e) => error.ApiError({ url }, e) // optionally add the cause |
| 85 | +); |
| 86 | +``` |
| 87 | + |
| 88 | +## Internal and User Facing Errors |
| 89 | + |
| 90 | +Errors defined with the `userMessage` property considered user facing errors. These are errors that are allowed to be shown to the user. |
| 91 | + |
| 92 | +> [!IMPORTANT] Internal errors should never be shown to the user. |
| 93 | +
|
| 94 | +### Error Handling Utilities |
| 95 | + |
| 96 | +Often times error handling can be verbose and it is expected for you to create utility functions for handling your user facing errors. |
| 97 | + |
| 98 | +We can import the `UserFacingError` type from `just-error` to ensure that we are only passing user facing errors to the function. |
| 99 | + |
| 100 | +> For functions expecting only internal errors you can use the `InternalError` type. |
| 101 | +
|
| 102 | +```ts |
| 103 | +import { UserFacingError } from "just-error"; |
| 104 | + |
| 105 | +function showErrorToast(error: UserFacingError) { |
| 106 | + // ... |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Mapping Internal Errors to User Facing Errors |
| 111 | + |
| 112 | +You will often find yourself wanting to map an internal error to better user facing message. This can be done with the `mapToUserFacingError` function. |
| 113 | + |
| 114 | +> This can also be useful for localizing your error messages! |
| 115 | +
|
| 116 | +```ts |
| 117 | +import * as justerror from "just-error"; |
| 118 | + |
| 119 | +// ... |
| 120 | + |
| 121 | +const userFacingError = result.mapError((e) => justerror.mapToUserFacingError(e, { |
| 122 | + ApiError: (e) => `There was an error serving your request from ${e.config.url}`, |
| 123 | +})); |
| 124 | +``` |
| 125 | + |
| 126 | +## Types |
| 127 | + |
| 128 | +Here are a few types you may want to implement to make using `just-error` easier: |
| 129 | + |
| 130 | +```ts |
| 131 | +import * as justerror from "just-error"; |
| 132 | + |
| 133 | +// allows you to get the type of an error by name i.e. Error<"ApiError"> |
| 134 | +export type Error<T extends keyof typeof error> = ReturnType<(typeof error)[T]>; |
| 135 | + |
| 136 | +// A union of all defined errors |
| 137 | +export type AnyError = InternalError<keyof typeof error>; |
| 138 | + |
| 139 | +const error = justerror.create({ |
| 140 | + ApiError: { |
| 141 | + code: "API_001", |
| 142 | + message: (args: { url: string }) => `Error fetching ${args.url}`, |
| 143 | + }, |
| 144 | + EmailError: { |
| 145 | + code: "EMAIL_001", |
| 146 | + message: (args: { email: string }) => `Error sending email to ${args.email}`, |
| 147 | + }, |
| 148 | +}); |
| 149 | +``` |
0 commit comments