Skip to content

Commit 7827acf

Browse files
committed
README.md
1 parent 13f57b3 commit 7827acf

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ throw new Error('User not found') // ← What is this? Who handles it? Who knows
2121
## Features
2222

2323
- Central **codes registry** with typed `props`
24-
- Helpers: `errors.throw`, `errors.is`, `errors.match`, `errors.hasTag`
24+
- Fully typed helpers with autocomplete: `errors.create()`, `errors.is()`, `errors.match()`, `errors.hasTag()`
2525
- Built-in **serialize/deserialize** across boundaries
2626
- Optional **scoped zones** for complex services
27-
- **Plugins**import error definitions from Stripe, Prisma, your own packages, etc.
27+
- **Plugins**extend with pre-defined error sets from shared packages
2828
- Zero runtime dependencies
2929

3030
## Install
@@ -36,17 +36,27 @@ npm install errata
3636
## Quick Start
3737

3838
```ts
39-
import { errata } from 'errata'
39+
import { code, errata, props } from 'errata'
4040

4141
const errors = errata({
4242
codes: {
43-
'user.not_found': { status: 404, message: 'User not found' },
44-
'auth.unauthorized': { status: 401, message: 'Not authenticated' },
43+
'user.not_found': code({
44+
status: 404,
45+
message: ({ details }) => `User ${details.userId} not found`,
46+
details: props<{ userId: string }>(),
47+
tags: ['auth', 'user'] as const,
48+
}),
49+
'auth.unauthorized': code({
50+
status: 401,
51+
message: 'Not authenticated',
52+
expose: true,
53+
tags: ['auth', 'security'] as const,
54+
}),
4555
}
4656
})
4757

48-
// Throw
49-
errors.throw('user.not_found')
58+
// Create and throw
59+
throw errors.create('user.not_found', { userId: '123' })
5060

5161
// Check
5262
if (errors.is(e, 'auth.unauthorized')) { ... }
@@ -56,28 +66,30 @@ errors.match(e, {
5666
'user.not_found': () => show404(),
5767
'auth.unauthorized': () => redirect('/login'),
5868
})
69+
70+
// Check tags
71+
if (errors.hasTag(e, 'security')) { ... }
5972
```
6073

6174
## Plugins
6275

63-
Extend your registry with pre-defined error sets:
76+
Extend your registry with pre-defined error sets from shared packages:
6477

6578
```ts
6679
import { myBackendErrors } from '@my-org/api-errors'
6780
import { errata } from 'errata'
68-
import { stripeErrors } from 'errata-stripe'
6981

7082
const errors = errata({
7183
codes: {
7284
'app.unknown': { status: 500, message: 'Something went wrong' }
7385
},
74-
plugins: [stripeErrors(), myBackendErrors()]
86+
plugins: [myBackendErrors()]
7587
})
7688

77-
// Now you can handle Stripe errors with full type safety
89+
// Now you can handle backend errors with full type safety
7890
errors.match(e, {
79-
'stripe.card_declined': () => showCardError(),
80-
'stripe.rate_limit': () => retry(),
91+
'payment.declined': () => showCardError(),
92+
'rate_limit': () => retry(),
8193
})
8294
```
8395

@@ -102,6 +114,6 @@ Then for future releases, you can run `pnpm run release` and the GitHub Actions
102114
[bundle-src]: https://img.shields.io/bundlephobia/minzip/errata?style=flat&colorA=080f12&colorB=1fa669&label=minzip
103115
[bundle-href]: https://bundlephobia.com/result?p=errata
104116
[license-src]: https://img.shields.io/github/license/fprl/errata.svg?style=flat&colorA=080f12&colorB=1fa669
105-
[license-href]: https://github.com/fprl/errata/blob/main/LICENSE
117+
[license-href]: https://github.com/fprl/errata/blob/main/LICENSE.md
106118
[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
107119
[jsdocs-href]: https://www.jsdocs.io/package/errata

0 commit comments

Comments
 (0)