hono-problem-details 0.7.0 → 0.8.1

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
@@ -38,9 +38,10 @@ can all agree on.
38
38
  - **Zod integration** — `@hono/zod-validator` hook for validation errors
39
39
  - **Valibot integration** — `@hono/valibot-validator` hook for validation errors
40
40
  - **OpenAPI integration** — `@hono/zod-openapi` schemas for API documentation
41
+ - **OpenTelemetry integration** — `@opentelemetry/api` for automatically injecting trace information.
41
42
  - **Standard Schema** — `@hono/standard-validator` hook (works with any schema library)
42
43
  - **Type-safe** — full TypeScript support with inference
43
- - **Zero runtime dependencies** — `hono` is the only required peer dependency; validator integrations are optional
44
+ - **Zero runtime dependencies** — `hono` is the only required peer dependency; validation and opentelemetry integrations are optional
44
45
  - **Localization** — `localize` callback for title/detail translation
45
46
  - **Edge-first** — works on Cloudflare Workers, Deno, Bun, and Node.js
46
47
 
@@ -434,6 +435,21 @@ The callback receives the fully-built `ProblemDetails` object and the Hono `Cont
434
435
  > the error handler to re-enter itself, so the swallow is deliberate. Catch errors inside your
435
436
  > callback if you need to observe them.
436
437
 
438
+ ## OpenTelemetry Integration
439
+
440
+ To enable OpenTelemetry instrumentation in problem details responses, pass the OpenTelemetry API as an option to the handler:
441
+
442
+ ```ts
443
+ import * as otelApi from "@opentelemetry/api";
444
+
445
+ problemDetailsHandler({
446
+ otelApi,
447
+ });
448
+ ```
449
+
450
+ When enabled, the handler adds a `traceId` extension member to all problem details responses when a trace is active.
451
+ This allows clients to correlate errors with server-side traces for easier debugging.
452
+
437
453
  ## Handler Options
438
454
 
439
455
  ```ts
package/dist/index.cjs CHANGED
@@ -162,6 +162,19 @@ function problemDetails(input) {
162
162
 
163
163
  // src/handler.ts
164
164
  var import_http_exception = require("hono/http-exception");
165
+
166
+ // src/integrations/opentelemetry.ts
167
+ function getOtelTraceId(api) {
168
+ try {
169
+ const span = api.trace.getSpan(api.context.active());
170
+ const traceId = span?.spanContext().traceId;
171
+ return traceId;
172
+ } catch {
173
+ return void 0;
174
+ }
175
+ }
176
+
177
+ // src/handler.ts
165
178
  function buildType(status, options) {
166
179
  if (options.typePrefix) {
167
180
  const slug = statusToSlug(status);
@@ -174,6 +187,15 @@ function toResponse(input, c, options) {
174
187
  if (options.autoInstance && pd.instance === void 0) {
175
188
  pd = { ...pd, instance: c.req.path };
176
189
  }
190
+ if (options.otelApi && pd.extensions?.traceId === void 0) {
191
+ const traceId = getOtelTraceId(options.otelApi);
192
+ if (traceId) {
193
+ if (!pd.extensions) {
194
+ pd.extensions = {};
195
+ }
196
+ pd.extensions.traceId = traceId;
197
+ }
198
+ }
177
199
  if (options.localize) {
178
200
  try {
179
201
  pd = { ...pd, ...options.localize(pd, c) };
package/dist/index.d.cts CHANGED
@@ -51,6 +51,21 @@ interface ProblemDetailsHandlerOptions {
51
51
  * Default: `false` — opt-in to avoid silently changing response shape.
52
52
  */
53
53
  autoInstance?: boolean;
54
+ /**
55
+ * If the OpenTelemetry API is available, the handler will attempt to
56
+ * include the current trace ID in the Problem Details response.
57
+ *
58
+ * To use this feature, pass the OpenTelemetry API as `otelApi` in the options:
59
+ *
60
+ * ```ts
61
+ * import * as api from "@opentelemetry/api";
62
+ *
63
+ * app.onError(problemDetailsHandler({
64
+ * otelApi: api,
65
+ * }));
66
+ * ```
67
+ */
68
+ otelApi?: OtelApiLike;
54
69
  /** Custom error to ProblemDetails mapping */
55
70
  mapError?: (error: Error) => ProblemDetailsInput | undefined;
56
71
  /**
@@ -60,6 +75,18 @@ interface ProblemDetailsHandlerOptions {
60
75
  */
61
76
  localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
62
77
  }
78
+ interface OtelApiLike {
79
+ trace: {
80
+ getSpan(context: unknown): {
81
+ spanContext(): {
82
+ traceId: string;
83
+ };
84
+ } | undefined;
85
+ };
86
+ context: {
87
+ active(): unknown;
88
+ };
89
+ }
63
90
 
64
91
  /**
65
92
  * Error class for RFC 9457 Problem Details.
package/dist/index.d.ts CHANGED
@@ -51,6 +51,21 @@ interface ProblemDetailsHandlerOptions {
51
51
  * Default: `false` — opt-in to avoid silently changing response shape.
52
52
  */
53
53
  autoInstance?: boolean;
54
+ /**
55
+ * If the OpenTelemetry API is available, the handler will attempt to
56
+ * include the current trace ID in the Problem Details response.
57
+ *
58
+ * To use this feature, pass the OpenTelemetry API as `otelApi` in the options:
59
+ *
60
+ * ```ts
61
+ * import * as api from "@opentelemetry/api";
62
+ *
63
+ * app.onError(problemDetailsHandler({
64
+ * otelApi: api,
65
+ * }));
66
+ * ```
67
+ */
68
+ otelApi?: OtelApiLike;
54
69
  /** Custom error to ProblemDetails mapping */
55
70
  mapError?: (error: Error) => ProblemDetailsInput | undefined;
56
71
  /**
@@ -60,6 +75,18 @@ interface ProblemDetailsHandlerOptions {
60
75
  */
61
76
  localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
62
77
  }
78
+ interface OtelApiLike {
79
+ trace: {
80
+ getSpan(context: unknown): {
81
+ spanContext(): {
82
+ traceId: string;
83
+ };
84
+ } | undefined;
85
+ };
86
+ context: {
87
+ active(): unknown;
88
+ };
89
+ }
63
90
 
64
91
  /**
65
92
  * Error class for RFC 9457 Problem Details.
package/dist/index.js CHANGED
@@ -35,6 +35,19 @@ function problemDetails(input) {
35
35
 
36
36
  // src/handler.ts
37
37
  import { HTTPException } from "hono/http-exception";
38
+
39
+ // src/integrations/opentelemetry.ts
40
+ function getOtelTraceId(api) {
41
+ try {
42
+ const span = api.trace.getSpan(api.context.active());
43
+ const traceId = span?.spanContext().traceId;
44
+ return traceId;
45
+ } catch {
46
+ return void 0;
47
+ }
48
+ }
49
+
50
+ // src/handler.ts
38
51
  function buildType(status, options) {
39
52
  if (options.typePrefix) {
40
53
  const slug = statusToSlug(status);
@@ -47,6 +60,15 @@ function toResponse(input, c, options) {
47
60
  if (options.autoInstance && pd.instance === void 0) {
48
61
  pd = { ...pd, instance: c.req.path };
49
62
  }
63
+ if (options.otelApi && pd.extensions?.traceId === void 0) {
64
+ const traceId = getOtelTraceId(options.otelApi);
65
+ if (traceId) {
66
+ if (!pd.extensions) {
67
+ pd.extensions = {};
68
+ }
69
+ pd.extensions.traceId = traceId;
70
+ }
71
+ }
50
72
  if (options.localize) {
51
73
  try {
52
74
  pd = { ...pd, ...options.localize(pd, c) };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono-problem-details",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "description": "RFC 9457 Problem Details middleware for Hono",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -104,6 +104,7 @@
104
104
  "@hono/valibot-validator": "^0.6.0",
105
105
  "@hono/zod-openapi": "^1.0.0",
106
106
  "@hono/zod-validator": "^0.7.5",
107
+ "@opentelemetry/api": "^1.0.0",
107
108
  "@standard-schema/spec": "^1.0.0",
108
109
  "hono": ">=4.12.14",
109
110
  "valibot": "^1.0.0",
@@ -128,6 +129,9 @@
128
129
  "@hono/standard-validator": {
129
130
  "optional": true
130
131
  },
132
+ "@opentelemetry/api": {
133
+ "optional": true
134
+ },
131
135
  "@standard-schema/spec": {
132
136
  "optional": true
133
137
  }
@@ -140,6 +144,7 @@
140
144
  "@hono/valibot-validator": "^0.6.1",
141
145
  "@hono/zod-openapi": "1.3.0",
142
146
  "@hono/zod-validator": "^0.7.6",
147
+ "@opentelemetry/api": "^1.0.0",
143
148
  "@standard-schema/spec": "1.1.0",
144
149
  "@vitest/coverage-v8": "^3.0.0",
145
150
  "hono": "^4.12.14",