@zeroxsolutions/server 0.3.0 → 0.3.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## 0.3.2 (2026-09-18)
2
+
3
+ This was a version bump only for @zeroxsolutions/server to align it with other projects, there were no code changes.
4
+
5
+ ## 0.3.1 (2026-09-18)
6
+
7
+ ### 🚀 Features
8
+
9
+ - **server:** log every error the handler answers ([c003f8e](https://github.com/zeroxsolutions/zeroxsolutions/commit/c003f8e))
10
+
11
+ ### ❤️ Thank You
12
+
13
+ - Claude Opus 5 (1M context)
14
+ - Lương Văn Tú
15
+
1
16
  ## 0.3.0 (2026-09-13)
2
17
 
3
18
  ### 💅 Refactors
package/README.md CHANGED
@@ -82,8 +82,13 @@ why `Content-Type` is read on a write only, and why an empty `Accept` is refused
82
82
  `@zeroxsolutions/response`'s `createDomainErrorResolver`;
83
83
  2. a request the validation hook rejected, one error object per issue;
84
84
  3. a `JsonApiError`, rendered as it was thrown;
85
- 4. otherwise the status alone - a hono `HTTPException` at its own, anything else as a 500, written to
86
- the log first because that 500 carries nothing of the error.
85
+ 4. otherwise the status alone - a hono `HTTPException` at its own, anything else as a 500.
86
+
87
+ Whichever step answers, the handler writes one `http.error.answered` line under `category` before the
88
+ document goes out, at the level the answered status class decides: a 5xx at error, anything else at
89
+ warn. The line carries the request id, the method, the path, the answered status, the step that claimed
90
+ the failure (`resolve`, `validation`, `jsonapi`, `status`) and the error's type and message. Without it
91
+ a mapped 500 or a guard's 403 reaches the client with nothing in the log for an operator to join it to.
87
92
 
88
93
  The `requestId` option's value becomes the `id` of each error object that carries none, so a client
89
94
  quotes the id the request's log lines carry.
@@ -4,7 +4,7 @@ import type { SurfaceVocabulary } from './surface-vocabulary.js';
4
4
  /** Tunes {@link createErrorHandler} for a specific transport (the edge gateway vs a service). */
5
5
  export interface ErrorHandlerOptions extends SurfaceVocabulary {
6
6
  /**
7
- * The ISOLATE's own logging category, which the unhandled-500 line is written under. Required
7
+ * The ISOLATE's own logging category, which every answered failure's line is written under. Required
8
8
  * rather than defaulted: a category the isolate did not configure is silent, and silence here
9
9
  * looks the same as a request that never failed.
10
10
  */
@@ -19,8 +19,9 @@ export interface ErrorHandlerOptions extends SurfaceVocabulary {
19
19
  * `errors[]` document. It asks, in order: `resolve`, then a request the validation hook rejected, then a
20
20
  * {@link JsonApiError} as thrown, then the HTTP status alone.
21
21
  *
22
- * A failure none of those claimed is written to the log before it answers, because the generic 500 it renders
23
- * carries nothing of the error itself.
22
+ * Every answer is written to the log before it goes out, at the level its status class decides: a 5xx at error,
23
+ * because this side went wrong, anything else at warn, because the request was refused. Without the line, a
24
+ * mapped 500 and a guard's 401 reach the client leaving no trace that the request arrived.
24
25
  */
25
26
  export declare function createErrorHandler(options: ErrorHandlerOptions): ErrorHandler;
26
27
  //# sourceMappingURL=error-handler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"error-handler.d.ts","sourceRoot":"","sources":["../src/error-handler.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAGlD,OAAO,EAOL,KAAK,aAAa,EACnB,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAuBjE,iGAAiG;AACjG,MAAM,WAAW,mBAAoB,SAAQ,iBAAiB;IAC5D;;;;OAIG;IACH,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5B,uGAAuG;IACvG,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,KAAK,aAAa,GAAG,SAAS,CAAC;IAChE,0GAA0G;IAC1G,SAAS,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;CACvD;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CAkC7E"}
1
+ {"version":3,"file":"error-handler.d.ts","sourceRoot":"","sources":["../src/error-handler.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAGlD,OAAO,EAOL,KAAK,aAAa,EACnB,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAkCjE,iGAAiG;AACjG,MAAM,WAAW,mBAAoB,SAAQ,iBAAiB;IAC5D;;;;OAIG;IACH,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5B,uGAAuG;IACvG,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,KAAK,aAAa,GAAG,SAAS,CAAC;IAChE,0GAA0G;IAC1G,SAAS,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;CACvD;AAmBD;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CA+B7E"}
@@ -9,9 +9,10 @@ for (const [name, meta] of Object.entries(BASE_ERROR_CODES)) {
9
9
  META_BY_STATUS.set(meta.status, { code: name.toLowerCase(), title: meta.title });
10
10
  }
11
11
  }
12
+ // The message template both levels write, so one search finds every error this handler answered.
13
+ const ANSWERED_LINE = 'http.error.answered {method} {path} {status} {claimedBy}';
12
14
  // Projects a failure hono raised - or one nothing claimed - onto a JSON:API error object by HTTP status.
13
- function errorObjectByStatus(err, namespace) {
14
- const status = err instanceof HTTPException ? err.status : 500;
15
+ function errorObjectByStatus(err, status, namespace) {
15
16
  const meta = META_BY_STATUS.get(status) ?? { code: 'error', title: 'Error' };
16
17
  const detail = err instanceof HTTPException ? err.message : 'Internal server error';
17
18
  return {
@@ -21,37 +22,54 @@ function errorObjectByStatus(err, namespace) {
21
22
  ...(detail && detail !== meta.title ? { detail } : {}),
22
23
  };
23
24
  }
25
+ // Asks each step of the dispatch in turn: `resolve`, a request the validation hook rejected, a `JsonApiError` as
26
+ // thrown, the HTTP status alone.
27
+ function claimError(err, c, options) {
28
+ const resolved = options.resolve?.(err, c);
29
+ if (resolved) {
30
+ return { claimedBy: 'resolve', resolved };
31
+ }
32
+ if (err instanceof RequestInvalid) {
33
+ return { claimedBy: 'validation', resolved: { status: err.status, errors: err.errors } };
34
+ }
35
+ if (err instanceof JsonApiError) {
36
+ return { claimedBy: 'jsonapi', resolved: { status: err.status, errors: [toErrorObject(err)] } };
37
+ }
38
+ const status = err instanceof HTTPException ? err.status : 500;
39
+ return { claimedBy: 'status', resolved: { status, errors: [errorObjectByStatus(err, status, options.namespace)] } };
40
+ }
24
41
  /**
25
42
  * The one central `onError` a transport installs, rendering every failure as one `application/vnd.api+json`
26
43
  * `errors[]` document. It asks, in order: `resolve`, then a request the validation hook rejected, then a
27
44
  * {@link JsonApiError} as thrown, then the HTTP status alone.
28
45
  *
29
- * A failure none of those claimed is written to the log before it answers, because the generic 500 it renders
30
- * carries nothing of the error itself.
46
+ * Every answer is written to the log before it goes out, at the level its status class decides: a 5xx at error,
47
+ * because this side went wrong, anything else at warn, because the request was refused. Without the line, a
48
+ * mapped 500 and a guard's 401 reach the client leaving no trace that the request arrived.
31
49
  */
32
50
  export function createErrorHandler(options) {
33
51
  const logger = getLogger(options.category);
34
52
  return (err, c) => {
35
53
  const requestId = options.requestId?.(c);
36
54
  const stamp = (e) => (requestId && !e.id ? { ...e, id: requestId } : e);
37
- const claimed = options.resolve?.(err, c) ??
38
- (err instanceof RequestInvalid ? { status: err.status, errors: err.errors } : undefined) ??
39
- (err instanceof JsonApiError ? { status: err.status, errors: [toErrorObject(err)] } : undefined);
40
- if (!claimed && !(err instanceof HTTPException)) {
41
- logger.error('http.error.unhandled {method} {path} {type}', {
42
- // Undefined where a transport never mounted `requestId()` - minting one here is a second id for one request.
43
- requestId: requestId ?? c.get('requestId'),
44
- method: c.req.method,
45
- // `path`, not `url`: a query string is where a caller puts a token, and a sink keeps what it is given.
46
- path: c.req.path,
47
- type: err.name,
48
- message: err.message,
49
- });
50
- }
51
- const resolved = claimed ?? {
52
- status: err instanceof HTTPException ? err.status : 500,
53
- errors: [errorObjectByStatus(err, options.namespace)],
55
+ const { claimedBy, resolved } = claimError(err, c, options);
56
+ const line = {
57
+ // Undefined where a transport never mounted `requestId()` - minting one here is a second id for one request.
58
+ requestId: requestId ?? c.get('requestId'),
59
+ method: c.req.method,
60
+ // `path`, not `url`: a query string is where a caller puts a token, and a sink keeps what it is given.
61
+ path: c.req.path,
62
+ status: resolved.status,
63
+ claimedBy,
64
+ type: err.name,
65
+ message: err.message,
54
66
  };
67
+ if (resolved.status >= 500) {
68
+ logger.error(ANSWERED_LINE, line);
69
+ }
70
+ else {
71
+ logger.warn(ANSWERED_LINE, line);
72
+ }
55
73
  const doc = toErrorDocument(resolved.errors.map(stamp));
56
74
  return c.body(JSON.stringify(doc), resolved.status, {
57
75
  'Content-Type': JSON_API_MEDIA_TYPE,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeroxsolutions/server",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -21,8 +21,8 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "tslib": "^2.3.0",
24
- "@zeroxsolutions/jsonapi": "0.11.0",
25
- "@zeroxsolutions/response": "0.12.0"
24
+ "@zeroxsolutions/response": "0.12.1",
25
+ "@zeroxsolutions/jsonapi": "0.11.1"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@hono/zod-openapi": "^1.0.0",