@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.
- package/build/chunk-B2GA45YG.js +35 -0
- package/build/define_config-t7GL92UR.js +5470 -0
- package/build/factories/http_context.d.ts +10 -4
- package/build/factories/main.d.ts +2 -2
- package/build/factories/main.js +328 -345
- package/build/factories/request.d.ts +4 -4
- package/build/factories/response.d.ts +4 -4
- package/build/factories/router.d.ts +1 -1
- package/build/factories/server_factory.d.ts +1 -1
- package/build/factories/url_builder_factory.d.ts +3 -3
- package/build/helpers-XD4_pfkD.js +108 -0
- package/build/helpers-aa47NQc6.js +1426 -0
- package/build/index.d.ts +2 -2
- package/build/index.js +330 -373
- package/build/src/client/helpers.d.ts +37 -0
- package/build/src/client/types.d.ts +194 -0
- package/build/src/client/url_builder.d.ts +15 -0
- package/build/src/client/url_builder.js +131 -0
- package/build/src/cookies/client.d.ts +28 -5
- package/build/src/cookies/drivers/encrypted.d.ts +1 -1
- package/build/src/cookies/drivers/signed.d.ts +1 -1
- package/build/src/cookies/parser.d.ts +1 -1
- package/build/src/cookies/serializer.d.ts +2 -2
- package/build/src/debug.d.ts +14 -1
- package/build/src/define_config.d.ts +19 -1
- package/build/src/define_middleware.d.ts +19 -3
- package/build/src/errors.d.ts +60 -5
- package/build/src/exception_handler.d.ts +28 -8
- package/build/src/helpers.d.ts +5 -17
- package/build/src/helpers.js +4 -24
- package/build/src/http_context/main.d.ts +67 -17
- package/build/src/qs.d.ts +17 -3
- package/build/src/redirect.d.ts +22 -3
- package/build/src/request.d.ts +12 -5
- package/build/src/response.d.ts +5 -5
- package/build/src/response_status.d.ts +14 -0
- package/build/src/router/main.d.ts +6 -2
- package/build/src/router/route.d.ts +130 -32
- package/build/src/router/signed_url_builder.d.ts +1 -1
- package/build/src/server/main.d.ts +6 -6
- package/build/src/types/main.js +2 -0
- package/build/src/types/request.d.ts +2 -1
- package/build/src/types/response.d.ts +6 -1
- package/build/src/types/route.d.ts +3 -27
- package/build/src/types/url_builder.d.ts +2 -140
- package/build/src/utils.d.ts +71 -6
- package/package.json +57 -48
- package/build/chunk-CVZAIRWJ.js +0 -1222
- package/build/chunk-JD6QW4NQ.js +0 -4428
- package/build/src/router/url_builder.d.ts +0 -9
package/build/index.js
CHANGED
|
@@ -1,376 +1,333 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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 };
|