@nice-code/common-errors 0.2.8 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,106 @@
1
1
  # @nice-code/common-errors
2
2
 
3
- Shared error domains for Standard Schema validation errors and Hono middleware integration.
3
+ Shared error domains for Standard Schema validation errors, with Hono middleware integration.
4
4
 
5
- See the [main README](../../README.md#nice-codecommon-errors) for full documentation and examples.
5
+ ## Install
6
+
7
+ ```bash
8
+ bun add @nice-code/common-errors
9
+ ```
10
+
11
+ Peer deps: `valibot` (or any Standard Schema library), `hono` (for `/hono` subpath).
12
+
13
+ ---
14
+
15
+ ## Validation error domain
16
+
17
+ `err_validation` is a [`@nice-code/error`](../nice-error) domain for Standard Schema validation failures. Import it to match or inspect validation errors by domain.
18
+
19
+ ```ts
20
+ import { err_validation, EValidator } from "@nice-code/common-errors";
21
+
22
+ // Check if an error is a validation error
23
+ if (err_validation.isExact(caught)) {
24
+ const hydrated = err_validation.hydrate(caught);
25
+ const { issues } = hydrated.getContext(EValidator.standard_schema);
26
+ // issues: readonly StandardSchemaV1.Issue[]
27
+ }
28
+ ```
29
+
30
+ The domain exposes one error id: `EValidator.standard_schema` with context `{ issues }`.
31
+
32
+ ---
33
+
34
+ ## Hono middleware
35
+
36
+ Import from the `/hono` subpath:
37
+
38
+ ```ts
39
+ import { niceSValidator, niceCatchSValidation } from "@nice-code/common-errors/hono";
40
+ ```
41
+
42
+ ### `niceSValidator(target, schema)`
43
+
44
+ Drop-in replacement for `@hono/standard-validator`'s `sValidator` that throws a `NiceError` instead of returning a 400 response when validation fails.
45
+
46
+ ```ts
47
+ import { niceSValidator } from "@nice-code/common-errors/hono";
48
+ import * as v from "valibot";
49
+
50
+ const CreateUserSchema = v.object({
51
+ name: v.string(),
52
+ email: v.pipe(v.string(), v.email()),
53
+ });
54
+
55
+ app.post("/users", niceSValidator("json", CreateUserSchema), async (c) => {
56
+ const body = c.req.valid("json"); // fully typed
57
+ // ...
58
+ });
59
+ ```
60
+
61
+ When validation fails, a `NiceError` from the `err_validation` domain is thrown. Use Hono's `onError` handler to convert it to a JSON response:
62
+
63
+ ```ts
64
+ import { castNiceError } from "@nice-code/error";
65
+
66
+ app.onError((err, c) => {
67
+ const niceError = castNiceError(err);
68
+ return c.json(niceError.toJsonObject(), niceError.httpStatusCode as any);
69
+ });
70
+ ```
71
+
72
+ ### `niceCatchSValidation()`
73
+
74
+ Middleware that intercepts raw `@hono/standard-validator` validation responses (the default `{ success: false, error: [...] }` shape) and converts them into `NiceError` JSON responses. Use this when you can't replace `sValidator` with `niceSValidator` directly.
75
+
76
+ ```ts
77
+ app.use(niceCatchSValidation());
78
+
79
+ // Existing sValidator usage is automatically intercepted
80
+ app.post("/data", sValidator("json", MySchema), handler);
81
+ ```
82
+
83
+ ---
84
+
85
+ ## Full Hono example
86
+
87
+ ```ts
88
+ import { Hono } from "hono";
89
+ import { niceSValidator } from "@nice-code/common-errors/hono";
90
+ import { castNiceError, EErrorPackType } from "@nice-code/error";
91
+ import * as v from "valibot";
92
+
93
+ const app = new Hono();
94
+
95
+ app.onError((err, c) => {
96
+ const niceError = castNiceError(err);
97
+ return c.json(niceError.toJsonObject(), niceError.httpStatusCode as any);
98
+ });
99
+
100
+ const BodySchema = v.object({ message: v.string() });
101
+
102
+ app.post("/echo", niceSValidator("json", BodySchema), async (c) => {
103
+ const { message } = c.req.valid("json");
104
+ return c.json({ echo: message });
105
+ });
106
+ ```