@adonisjs/http-server 8.0.0-next.9 → 8.1.0

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.
Files changed (50) hide show
  1. package/build/chunk-B2GA45YG.js +35 -0
  2. package/build/define_config-t7GL92UR.js +5470 -0
  3. package/build/factories/http_context.d.ts +10 -4
  4. package/build/factories/main.d.ts +2 -2
  5. package/build/factories/main.js +328 -345
  6. package/build/factories/request.d.ts +4 -4
  7. package/build/factories/response.d.ts +4 -4
  8. package/build/factories/router.d.ts +1 -1
  9. package/build/factories/server_factory.d.ts +1 -1
  10. package/build/factories/url_builder_factory.d.ts +3 -3
  11. package/build/helpers-XD4_pfkD.js +108 -0
  12. package/build/helpers-aa47NQc6.js +1426 -0
  13. package/build/index.d.ts +2 -2
  14. package/build/index.js +330 -373
  15. package/build/src/client/helpers.d.ts +37 -0
  16. package/build/src/client/types.d.ts +194 -0
  17. package/build/src/client/url_builder.d.ts +15 -0
  18. package/build/src/client/url_builder.js +131 -0
  19. package/build/src/cookies/client.d.ts +28 -5
  20. package/build/src/cookies/drivers/encrypted.d.ts +1 -1
  21. package/build/src/cookies/drivers/signed.d.ts +1 -1
  22. package/build/src/cookies/parser.d.ts +1 -1
  23. package/build/src/cookies/serializer.d.ts +2 -2
  24. package/build/src/debug.d.ts +14 -1
  25. package/build/src/define_config.d.ts +19 -1
  26. package/build/src/define_middleware.d.ts +19 -3
  27. package/build/src/errors.d.ts +60 -5
  28. package/build/src/exception_handler.d.ts +28 -8
  29. package/build/src/helpers.d.ts +5 -17
  30. package/build/src/helpers.js +4 -24
  31. package/build/src/http_context/main.d.ts +67 -17
  32. package/build/src/qs.d.ts +17 -3
  33. package/build/src/redirect.d.ts +22 -3
  34. package/build/src/request.d.ts +12 -5
  35. package/build/src/response.d.ts +5 -5
  36. package/build/src/response_status.d.ts +14 -0
  37. package/build/src/router/main.d.ts +6 -2
  38. package/build/src/router/route.d.ts +130 -32
  39. package/build/src/router/signed_url_builder.d.ts +1 -1
  40. package/build/src/server/main.d.ts +6 -6
  41. package/build/src/types/main.js +2 -0
  42. package/build/src/types/request.d.ts +2 -1
  43. package/build/src/types/response.d.ts +6 -1
  44. package/build/src/types/route.d.ts +3 -27
  45. package/build/src/types/url_builder.d.ts +2 -140
  46. package/build/src/utils.d.ts +71 -6
  47. package/package.json +57 -48
  48. package/build/chunk-CVZAIRWJ.js +0 -1222
  49. package/build/chunk-JD6QW4NQ.js +0 -4428
  50. package/build/src/router/url_builder.d.ts +0 -9
package/build/index.js CHANGED
@@ -1,376 +1,333 @@
1
- import {
2
- CookieClient,
3
- CookieParser,
4
- CookieSerializer,
5
- E_CANNOT_LOOKUP_ROUTE,
6
- E_HTTP_EXCEPTION,
7
- E_HTTP_REQUEST_ABORTED,
8
- E_ROUTE_NOT_FOUND,
9
- HttpContext,
10
- Qs,
11
- Redirect,
12
- Request,
13
- Response,
14
- ResponseStatus,
15
- Router,
16
- Server,
17
- defineConfig,
18
- errors_exports
19
- } from "./chunk-JD6QW4NQ.js";
20
- import {
21
- BriskRoute,
22
- Route,
23
- RouteGroup,
24
- RouteResource,
25
- canWriteResponseBody,
26
- parseRange,
27
- tracing_channels_exports
28
- } from "./chunk-CVZAIRWJ.js";
29
-
30
- // src/exception_handler.ts
31
- import is from "@sindresorhus/is";
1
+ import "./chunk-B2GA45YG.js";
2
+ import { _ as Qs, a as CookieSerializer, c as HttpRequest, d as Redirect, f as E_CANNOT_LOOKUP_ROUTE, g as errors_exports, h as E_ROUTE_NOT_FOUND, i as HttpResponse, l as CookieParser, m as E_HTTP_REQUEST_ABORTED, n as Server, o as ResponseStatus, p as E_HTTP_EXCEPTION, r as HttpContext, s as Router, t as defineConfig, u as CookieClient } from "./define_config-t7GL92UR.js";
3
+ import { C as canWriteResponseBody, S as tracing_channels_exports, _ as Route, g as BriskRoute, h as RouteResource, m as RouteGroup, u as parseRange } from "./helpers-aa47NQc6.js";
4
+ import "./helpers-XD4_pfkD.js";
32
5
  import Macroable from "@poppinss/macroable";
6
+ import is from "@sindresorhus/is";
7
+ //#region src/exception_handler.ts
8
+ /**
9
+ * The base HTTP exception handler that provides comprehensive error handling capabilities.
10
+ *
11
+ * This class can be inherited to create custom exception handlers for your application.
12
+ * It provides built-in support for:
13
+ *
14
+ * - Self-handling exceptions via their own render/handle methods
15
+ * - Custom status page rendering for different HTTP error codes
16
+ * - Debug-friendly error display during development
17
+ * - Content negotiation for JSON, JSON API, and HTML error responses
18
+ * - Configurable error reporting and logging
19
+ * - Validation error handling with field-specific messages
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * export default class HttpExceptionHandler extends ExceptionHandler {
24
+ * protected debug = app.inDev
25
+ * protected renderStatusPages = app.inProduction
26
+ *
27
+ * protected statusPages = {
28
+ * '404': (error, ctx) => ctx.view.render('errors/404')
29
+ * }
30
+ * }
31
+ * ```
32
+ */
33
33
  var ExceptionHandler = class extends Macroable {
34
- /**
35
- * Cached expanded status pages mapping individual status codes to their renderers
36
- * Computed from the statusPages property when first accessed
37
- */
38
- #expandedStatusPages;
39
- /**
40
- * Controls whether to include debug information in error responses
41
- * When enabled, errors include complete stack traces and detailed debugging info
42
- * Defaults to true in non-production environments
43
- */
44
- debug = process.env.NODE_ENV !== "production";
45
- /**
46
- * Controls whether to render custom status pages for unhandled errors
47
- * When enabled, errors with matching status codes use configured status page renderers
48
- * Defaults to true in production environments
49
- */
50
- renderStatusPages = process.env.NODE_ENV === "production";
51
- /**
52
- * Mapping of HTTP status code ranges to their corresponding page renderers
53
- * Supports ranges like '400-499' or individual codes like '404'
54
- */
55
- statusPages = {};
56
- /**
57
- * Controls whether errors should be reported to logging systems
58
- * When disabled, errors are handled but not logged or reported
59
- */
60
- reportErrors = true;
61
- /**
62
- * Array of exception class constructors to exclude from error reporting
63
- * These exceptions are handled but not logged or reported to external systems
64
- */
65
- ignoreExceptions = [
66
- E_HTTP_EXCEPTION,
67
- E_ROUTE_NOT_FOUND,
68
- E_CANNOT_LOOKUP_ROUTE,
69
- E_HTTP_REQUEST_ABORTED
70
- ];
71
- /**
72
- * Array of HTTP status codes to exclude from error reporting
73
- * Errors with these status codes are handled but not logged
74
- */
75
- ignoreStatuses = [400, 422, 401];
76
- /**
77
- * Array of custom error codes to exclude from error reporting
78
- * Errors with these codes are handled but not logged
79
- */
80
- ignoreCodes = [];
81
- /**
82
- * Expands status page ranges into individual status code mappings
83
- * Creates a cached lookup table for faster status page resolution
84
- * @returns Mapping of status codes to renderers
85
- */
86
- #expandStatusPages() {
87
- if (!this.#expandedStatusPages) {
88
- this.#expandedStatusPages = Object.keys(this.statusPages).reduce(
89
- (result, range) => {
90
- const renderer = this.statusPages[range];
91
- result = Object.assign(result, parseRange(range, renderer));
92
- return result;
93
- },
94
- {}
95
- );
96
- }
97
- return this.#expandedStatusPages;
98
- }
99
- /**
100
- * Normalizes any thrown value into a standardized HttpError object
101
- * Ensures the error has required properties like message and status
102
- * @param error - Any thrown value (Error, string, object, etc.)
103
- * @returns {HttpError} Normalized error object with status and message
104
- */
105
- #toHttpError(error) {
106
- const httpError = is.object(error) ? error : new Error(String(error));
107
- if (!httpError.message) {
108
- httpError.message = "Internal server error";
109
- }
110
- if (!httpError.status) {
111
- httpError.status = 500;
112
- }
113
- return httpError;
114
- }
115
- /**
116
- * Provides additional context information for error reporting
117
- * Includes request ID when available for correlation across logs
118
- * @param ctx - HTTP context containing request information
119
- * @returns Additional context data for error reporting
120
- */
121
- context(ctx) {
122
- const requestId = ctx.request.id();
123
- return requestId ? {
124
- "x-request-id": requestId
125
- } : {};
126
- }
127
- /**
128
- * Determines the appropriate log level based on HTTP status code
129
- * 5xx errors are logged as 'error', 4xx as 'warn', others as 'info'
130
- * @param error - HTTP error object with status code
131
- * @returns {Level} Appropriate logging level for the error
132
- */
133
- getErrorLogLevel(error) {
134
- if (error.status >= 500) {
135
- return "error";
136
- }
137
- if (error.status >= 400) {
138
- return "warn";
139
- }
140
- return "info";
141
- }
142
- /**
143
- * Determines whether debug information should be included in error responses
144
- * Override this method to implement context-specific debug control
145
- * @param _ - HTTP context (unused in base implementation)
146
- * @returns {boolean} True if debugging should be enabled
147
- */
148
- isDebuggingEnabled(_) {
149
- return this.debug;
150
- }
151
- /**
152
- * Determines whether an error should be reported to logging systems
153
- * Checks against ignore lists for exceptions, status codes, and error codes
154
- * @param error - HTTP error to evaluate for reporting
155
- * @returns {boolean} True if the error should be reported
156
- */
157
- shouldReport(error) {
158
- if (this.reportErrors === false) {
159
- return false;
160
- }
161
- if (this.ignoreStatuses.includes(error.status)) {
162
- return false;
163
- }
164
- if (error.code && this.ignoreCodes.includes(error.code)) {
165
- return false;
166
- }
167
- if (this.ignoreExceptions.find((exception) => error instanceof exception)) {
168
- return false;
169
- }
170
- return true;
171
- }
172
- /**
173
- * Renders an error as a JSON response
174
- * In debug mode, includes full stack trace using Youch
175
- * @param error - HTTP error to render
176
- * @param ctx - HTTP context for the request
177
- */
178
- async renderErrorAsJSON(error, ctx) {
179
- if (this.isDebuggingEnabled(ctx)) {
180
- const { Youch } = await import("youch");
181
- const json = await new Youch().toJSON(error);
182
- ctx.response.status(error.status).send(json);
183
- return;
184
- }
185
- ctx.response.status(error.status).send({ message: error.message });
186
- }
187
- /**
188
- * Renders an error as a JSON API compliant response
189
- * Follows JSON API specification for error objects
190
- * @param error - HTTP error to render
191
- * @param ctx - HTTP context for the request
192
- */
193
- async renderErrorAsJSONAPI(error, ctx) {
194
- if (this.isDebuggingEnabled(ctx)) {
195
- const { Youch } = await import("youch");
196
- const json = await new Youch().toJSON(error);
197
- ctx.response.status(error.status).send(json);
198
- return;
199
- }
200
- ctx.response.status(error.status).send({
201
- errors: [
202
- {
203
- title: error.message,
204
- code: error.code,
205
- status: error.status
206
- }
207
- ]
208
- });
209
- }
210
- /**
211
- * Renders an error as an HTML response
212
- * Uses status pages if configured, otherwise shows debug info or simple message
213
- * @param error - HTTP error to render
214
- * @param ctx - HTTP context for the request
215
- */
216
- async renderErrorAsHTML(error, ctx) {
217
- const statusPages = this.#expandStatusPages();
218
- if (this.renderStatusPages && statusPages[error.status]) {
219
- const statusPageResponse = await statusPages[error.status](error, ctx);
220
- if (canWriteResponseBody(statusPageResponse, ctx)) {
221
- return ctx.response.safeStatus(error.status).send(statusPageResponse);
222
- }
223
- return statusPageResponse;
224
- }
225
- if (this.isDebuggingEnabled(ctx)) {
226
- const { Youch } = await import("youch");
227
- const html = await new Youch().toHTML(error, {
228
- request: ctx.request.request,
229
- cspNonce: "nonce" in ctx.response ? ctx.response.nonce : void 0
230
- });
231
- ctx.response.status(error.status).send(html);
232
- return;
233
- }
234
- ctx.response.status(error.status).send(`<p> ${error.message} </p>`);
235
- }
236
- /**
237
- * Renders validation error messages as a JSON response
238
- * Returns errors in a simple format with field-specific messages
239
- * @param error - Validation error containing messages array
240
- * @param ctx - HTTP context for the request
241
- */
242
- async renderValidationErrorAsJSON(error, ctx) {
243
- ctx.response.status(error.status).send({
244
- errors: error.messages
245
- });
246
- }
247
- /**
248
- * Renders validation error messages as JSON API compliant response
249
- * Transforms validation messages to JSON API error object format
250
- * @param error - Validation error containing messages array
251
- * @param ctx - HTTP context for the request
252
- */
253
- async renderValidationErrorAsJSONAPI(error, ctx) {
254
- ctx.response.status(error.status).send({
255
- errors: error.messages.map((message) => {
256
- return {
257
- title: message.message,
258
- code: message.rule,
259
- source: {
260
- pointer: message.field
261
- },
262
- meta: message.meta
263
- };
264
- })
265
- });
266
- }
267
- /**
268
- * Renders validation error messages as an HTML response
269
- * Creates simple HTML list of field errors separated by line breaks
270
- * @param error - Validation error containing messages array
271
- * @param ctx - HTTP context for the request
272
- */
273
- async renderValidationErrorAsHTML(error, ctx) {
274
- ctx.response.status(error.status).type("html").send(
275
- error.messages.map((message) => {
276
- return `${message.field} - ${message.message}`;
277
- }).join("<br />")
278
- );
279
- }
280
- /**
281
- * Renders an error to the appropriate response format based on content negotiation
282
- * Supports HTML, JSON API, and JSON formats based on Accept headers
283
- * @param error - HTTP error to render
284
- * @param ctx - HTTP context for the request
285
- */
286
- renderError(error, ctx) {
287
- switch (ctx.request.accepts(["html", "application/vnd.api+json", "json"])) {
288
- case "application/vnd.api+json":
289
- return this.renderErrorAsJSONAPI(error, ctx);
290
- case "json":
291
- return this.renderErrorAsJSON(error, ctx);
292
- case "html":
293
- default:
294
- return this.renderErrorAsHTML(error, ctx);
295
- }
296
- }
297
- /**
298
- * Renders validation errors to the appropriate response format based on content negotiation
299
- * Supports HTML, JSON API, and JSON formats for validation error messages
300
- * @param error - Validation error to render
301
- * @param ctx - HTTP context for the request
302
- */
303
- renderValidationError(error, ctx) {
304
- switch (ctx.request.accepts(["html", "application/vnd.api+json", "json"])) {
305
- case "application/vnd.api+json":
306
- return this.renderValidationErrorAsJSONAPI(error, ctx);
307
- case "json":
308
- return this.renderValidationErrorAsJSON(error, ctx);
309
- case "html":
310
- default:
311
- return this.renderValidationErrorAsHTML(error, ctx);
312
- }
313
- }
314
- /**
315
- * Reports an error to logging systems if reporting is enabled
316
- * Allows errors to self-report via their own report method if available
317
- * @param error - Any error object to report
318
- * @param ctx - HTTP context for additional reporting context
319
- */
320
- async report(error, ctx) {
321
- const httpError = this.#toHttpError(error);
322
- if (!this.shouldReport(httpError)) {
323
- return;
324
- }
325
- if (typeof httpError.report === "function") {
326
- httpError.report(httpError, ctx);
327
- return;
328
- }
329
- const level = this.getErrorLogLevel(httpError);
330
- ctx.logger.log(
331
- level,
332
- {
333
- ...level === "error" || level === "fatal" ? { err: httpError } : {},
334
- ...this.context(ctx)
335
- },
336
- httpError.message
337
- );
338
- }
339
- /**
340
- * Handles errors during HTTP request processing
341
- * Delegates to error's own handle method if available, otherwise renders response
342
- * @param error - Any error object to handle
343
- * @param ctx - HTTP context for error handling
344
- */
345
- async handle(error, ctx) {
346
- const httpError = this.#toHttpError(error);
347
- if (typeof httpError.handle === "function") {
348
- return httpError.handle(httpError, ctx);
349
- }
350
- if (httpError.code === "E_VALIDATION_ERROR" && "messages" in httpError) {
351
- return this.renderValidationError(httpError, ctx);
352
- }
353
- return this.renderError(httpError, ctx);
354
- }
355
- };
356
- export {
357
- BriskRoute,
358
- CookieClient,
359
- CookieParser,
360
- CookieSerializer,
361
- ExceptionHandler,
362
- HttpContext,
363
- Qs,
364
- Redirect,
365
- Request,
366
- Response,
367
- ResponseStatus,
368
- Route,
369
- RouteGroup,
370
- RouteResource,
371
- Router,
372
- Server,
373
- defineConfig,
374
- errors_exports as errors,
375
- tracing_channels_exports as tracingChannels
34
+ /**
35
+ * Cached expanded status pages mapping individual status codes to their renderers
36
+ * Computed from the statusPages property when first accessed
37
+ */
38
+ #expandedStatusPages;
39
+ /**
40
+ * Controls whether to include debug information in error responses
41
+ * When enabled, errors include complete stack traces and detailed debugging info
42
+ * Defaults to true in non-production environments
43
+ */
44
+ debug = process.env.NODE_ENV !== "production";
45
+ /**
46
+ * Controls whether to render custom status pages for unhandled errors
47
+ * When enabled, errors with matching status codes use configured status page renderers
48
+ * Defaults to true in production environments
49
+ */
50
+ renderStatusPages = process.env.NODE_ENV === "production";
51
+ /**
52
+ * Mapping of HTTP status code ranges to their corresponding page renderers
53
+ * Supports ranges like '400-499' or individual codes like '404'
54
+ */
55
+ statusPages = {};
56
+ /**
57
+ * Controls whether errors should be reported to logging systems
58
+ * When disabled, errors are handled but not logged or reported
59
+ */
60
+ reportErrors = true;
61
+ /**
62
+ * Array of exception class constructors to exclude from error reporting
63
+ * These exceptions are handled but not logged or reported to external systems
64
+ */
65
+ ignoreExceptions = [
66
+ E_HTTP_EXCEPTION,
67
+ E_ROUTE_NOT_FOUND,
68
+ E_CANNOT_LOOKUP_ROUTE,
69
+ E_HTTP_REQUEST_ABORTED
70
+ ];
71
+ /**
72
+ * Array of HTTP status codes to exclude from error reporting
73
+ * Errors with these status codes are handled but not logged
74
+ */
75
+ ignoreStatuses = [
76
+ 400,
77
+ 422,
78
+ 401
79
+ ];
80
+ /**
81
+ * Array of custom error codes to exclude from error reporting
82
+ * Errors with these codes are handled but not logged
83
+ */
84
+ ignoreCodes = [];
85
+ /**
86
+ * Expands status page ranges into individual status code mappings
87
+ * Creates a cached lookup table for faster status page resolution
88
+ * @returns Mapping of status codes to renderers
89
+ */
90
+ #expandStatusPages() {
91
+ if (!this.#expandedStatusPages) this.#expandedStatusPages = Object.keys(this.statusPages).reduce((result, range) => {
92
+ const renderer = this.statusPages[range];
93
+ result = Object.assign(result, parseRange(range, renderer));
94
+ return result;
95
+ }, {});
96
+ return this.#expandedStatusPages;
97
+ }
98
+ /**
99
+ * Normalizes any thrown value into a standardized HttpError object
100
+ * Ensures the error has required properties like message and status
101
+ * @param error - Any thrown value (Error, string, object, etc.)
102
+ * @returns {HttpError} Normalized error object with status and message
103
+ */
104
+ toHttpError(error) {
105
+ const httpError = is.object(error) ? error : new Error(String(error));
106
+ if (!httpError.message) httpError.message = "Internal server error";
107
+ if (!httpError.status) httpError.status = 500;
108
+ return httpError;
109
+ }
110
+ /**
111
+ * Provides additional context information for error reporting
112
+ * Includes request ID when available for correlation across logs
113
+ * @param ctx - HTTP context containing request information
114
+ * @returns Additional context data for error reporting
115
+ */
116
+ context(ctx) {
117
+ const requestId = ctx.request.id();
118
+ return requestId ? { "x-request-id": requestId } : {};
119
+ }
120
+ /**
121
+ * Determines the appropriate log level based on HTTP status code
122
+ * 5xx errors are logged as 'error', 4xx as 'warn', others as 'info'
123
+ * @param error - HTTP error object with status code
124
+ * @returns {Level} Appropriate logging level for the error
125
+ */
126
+ getErrorLogLevel(error) {
127
+ if (error.status >= 500) return "error";
128
+ if (error.status >= 400) return "warn";
129
+ return "info";
130
+ }
131
+ /**
132
+ * Determines whether debug information should be included in error responses
133
+ * Override this method to implement context-specific debug control
134
+ * @param _ - HTTP context (unused in base implementation)
135
+ * @returns {boolean} True if debugging should be enabled
136
+ */
137
+ isDebuggingEnabled(_) {
138
+ return this.debug;
139
+ }
140
+ /**
141
+ * Determines whether an error should be reported to logging systems
142
+ * Checks against ignore lists for exceptions, status codes, and error codes
143
+ * @param error - HTTP error to evaluate for reporting
144
+ * @returns {boolean} True if the error should be reported
145
+ */
146
+ shouldReport(error) {
147
+ if (this.reportErrors === false) return false;
148
+ if (this.ignoreStatuses.includes(error.status)) return false;
149
+ if (error.code && this.ignoreCodes.includes(error.code)) return false;
150
+ if (this.ignoreExceptions.find((exception) => error instanceof exception)) return false;
151
+ return true;
152
+ }
153
+ /**
154
+ * Renders an error as a JSON response
155
+ * In debug mode, includes full stack trace using Youch
156
+ * @param error - HTTP error to render
157
+ * @param ctx - HTTP context for the request
158
+ */
159
+ async renderErrorAsJSON(error, ctx) {
160
+ if (this.isDebuggingEnabled(ctx)) {
161
+ const { Youch } = await import("youch");
162
+ const json = await new Youch().toJSON(error);
163
+ ctx.response.status(error.status).send(json);
164
+ return;
165
+ }
166
+ ctx.response.status(error.status).send({ message: error.message });
167
+ }
168
+ /**
169
+ * Renders an error as a JSON API compliant response
170
+ * Follows JSON API specification for error objects
171
+ * @param error - HTTP error to render
172
+ * @param ctx - HTTP context for the request
173
+ */
174
+ async renderErrorAsJSONAPI(error, ctx) {
175
+ if (this.isDebuggingEnabled(ctx)) {
176
+ const { Youch } = await import("youch");
177
+ const json = await new Youch().toJSON(error);
178
+ ctx.response.status(error.status).send(json);
179
+ return;
180
+ }
181
+ ctx.response.status(error.status).send({ errors: [{
182
+ title: error.message,
183
+ code: error.code,
184
+ status: error.status
185
+ }] });
186
+ }
187
+ /**
188
+ * Renders an error as an HTML response
189
+ * Uses status pages if configured, otherwise shows debug info or simple message
190
+ * @param error - HTTP error to render
191
+ * @param ctx - HTTP context for the request
192
+ */
193
+ async renderErrorAsHTML(error, ctx) {
194
+ /**
195
+ * Render status page
196
+ */
197
+ const statusPages = this.#expandStatusPages();
198
+ if (this.renderStatusPages && statusPages[error.status]) {
199
+ const statusPageResponse = await statusPages[error.status](error, ctx);
200
+ /**
201
+ * Use return value and convert it into a response
202
+ */
203
+ if (canWriteResponseBody(statusPageResponse, ctx)) return ctx.response.safeStatus(error.status).send(statusPageResponse);
204
+ return statusPageResponse;
205
+ }
206
+ if (this.isDebuggingEnabled(ctx)) {
207
+ const { Youch } = await import("youch");
208
+ const html = await new Youch().toHTML(error, {
209
+ request: ctx.request.request,
210
+ cspNonce: "nonce" in ctx.response ? ctx.response.nonce : void 0
211
+ });
212
+ ctx.response.status(error.status).send(html);
213
+ return;
214
+ }
215
+ ctx.response.status(error.status).send(`<p> ${error.message} </p>`);
216
+ }
217
+ /**
218
+ * Renders validation error messages as a JSON response
219
+ * Returns errors in a simple format with field-specific messages
220
+ * @param error - Validation error containing messages array
221
+ * @param ctx - HTTP context for the request
222
+ */
223
+ async renderValidationErrorAsJSON(error, ctx) {
224
+ ctx.response.status(error.status).send({ errors: error.messages });
225
+ }
226
+ /**
227
+ * Renders validation error messages as JSON API compliant response
228
+ * Transforms validation messages to JSON API error object format
229
+ * @param error - Validation error containing messages array
230
+ * @param ctx - HTTP context for the request
231
+ */
232
+ async renderValidationErrorAsJSONAPI(error, ctx) {
233
+ ctx.response.status(error.status).send({ errors: error.messages.map((message) => {
234
+ return {
235
+ title: message.message,
236
+ code: message.rule,
237
+ source: { pointer: message.field },
238
+ meta: message.meta
239
+ };
240
+ }) });
241
+ }
242
+ /**
243
+ * Renders validation error messages as an HTML response
244
+ * Creates simple HTML list of field errors separated by line breaks
245
+ * @param error - Validation error containing messages array
246
+ * @param ctx - HTTP context for the request
247
+ */
248
+ async renderValidationErrorAsHTML(error, ctx) {
249
+ ctx.response.status(error.status).type("html").send(error.messages.map((message) => {
250
+ return `${message.field} - ${message.message}`;
251
+ }).join("<br />"));
252
+ }
253
+ /**
254
+ * Renders an error to the appropriate response format based on content negotiation
255
+ * Supports HTML, JSON API, and JSON formats based on Accept headers
256
+ * @param error - HTTP error to render
257
+ * @param ctx - HTTP context for the request
258
+ */
259
+ renderError(error, ctx) {
260
+ switch (ctx.request.accepts([
261
+ "html",
262
+ "application/vnd.api+json",
263
+ "json"
264
+ ])) {
265
+ case "application/vnd.api+json": return this.renderErrorAsJSONAPI(error, ctx);
266
+ case "json": return this.renderErrorAsJSON(error, ctx);
267
+ default: return this.renderErrorAsHTML(error, ctx);
268
+ }
269
+ }
270
+ /**
271
+ * Renders validation errors to the appropriate response format based on content negotiation
272
+ * Supports HTML, JSON API, and JSON formats for validation error messages
273
+ * @param error - Validation error to render
274
+ * @param ctx - HTTP context for the request
275
+ */
276
+ renderValidationError(error, ctx) {
277
+ switch (ctx.request.accepts([
278
+ "html",
279
+ "application/vnd.api+json",
280
+ "json"
281
+ ])) {
282
+ case "application/vnd.api+json": return this.renderValidationErrorAsJSONAPI(error, ctx);
283
+ case "json": return this.renderValidationErrorAsJSON(error, ctx);
284
+ default: return this.renderValidationErrorAsHTML(error, ctx);
285
+ }
286
+ }
287
+ /**
288
+ * Reports an error to logging systems if reporting is enabled
289
+ * Allows errors to self-report via their own report method if available
290
+ * @param error - Any error object to report
291
+ * @param ctx - HTTP context for additional reporting context
292
+ */
293
+ async report(error, ctx) {
294
+ const httpError = this.toHttpError(error);
295
+ if (!this.shouldReport(httpError)) return;
296
+ if (typeof httpError.report === "function") {
297
+ httpError.report(httpError, ctx);
298
+ return;
299
+ }
300
+ /**
301
+ * Log the error using the logger
302
+ */
303
+ const level = this.getErrorLogLevel(httpError);
304
+ ctx.logger.log(level, {
305
+ ...level === "error" || level === "fatal" ? { err: httpError } : {},
306
+ ...this.context(ctx)
307
+ }, httpError.message);
308
+ }
309
+ /**
310
+ * Handles errors during HTTP request processing
311
+ * Delegates to error's own handle method if available, otherwise renders response
312
+ * @param error - Any error object to handle
313
+ * @param ctx - HTTP context for error handling
314
+ */
315
+ async handle(error, ctx) {
316
+ const httpError = this.toHttpError(error);
317
+ /**
318
+ * Self handle exception
319
+ */
320
+ if (typeof httpError.handle === "function") return httpError.handle(httpError, ctx);
321
+ /**
322
+ * Handle validation error using the validation error
323
+ * renderers
324
+ */
325
+ if (httpError.code === "E_VALIDATION_ERROR" && "messages" in httpError) return this.renderValidationError(httpError, ctx);
326
+ /**
327
+ * Use the format renderers.
328
+ */
329
+ return this.renderError(httpError, ctx);
330
+ }
376
331
  };
332
+ //#endregion
333
+ export { BriskRoute, CookieClient, CookieParser, CookieSerializer, ExceptionHandler, HttpContext, HttpRequest, HttpResponse, Qs, Redirect, ResponseStatus, Route, RouteGroup, RouteResource, Router, Server, defineConfig, errors_exports as errors, tracing_channels_exports as tracingChannels };