elysia 2.0.0-beta.2 → 2.0.0-beta.21
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/dist/adapter/bun/index.d.ts +18 -2
- package/dist/adapter/bun/index.js +467 -110
- package/dist/adapter/bun/index.mjs +467 -112
- package/dist/adapter/types.d.ts +2 -21
- package/dist/adapter/utils.d.ts +14 -3
- package/dist/adapter/utils.js +146 -87
- package/dist/adapter/utils.mjs +148 -90
- package/dist/adapter/web-standard/handler.d.ts +1 -1
- package/dist/adapter/web-standard/handler.js +47 -36
- package/dist/adapter/web-standard/handler.mjs +48 -37
- package/dist/adapter/web-standard/index.js +2 -30
- package/dist/adapter/web-standard/index.mjs +2 -30
- package/dist/adapter/web-standard/utils.js +5 -18
- package/dist/adapter/web-standard/utils.mjs +5 -18
- package/dist/base.d.ts +99 -67
- package/dist/base.js +678 -510
- package/dist/base.mjs +682 -514
- package/dist/compile/aot-capture.d.ts +2 -13
- package/dist/compile/aot-capture.js +26 -69
- package/dist/compile/aot-capture.mjs +28 -67
- package/dist/compile/aot-emit.d.ts +9 -7
- package/dist/compile/aot-emit.js +42 -19
- package/dist/compile/aot-emit.mjs +42 -20
- package/dist/compile/aot.d.ts +17 -19
- package/dist/compile/aot.js +41 -51
- package/dist/compile/aot.mjs +39 -49
- package/dist/compile/handler/descriptor.d.ts +9 -12
- package/dist/compile/handler/descriptor.js +19 -44
- package/dist/compile/handler/descriptor.mjs +20 -42
- package/dist/compile/handler/frozen-validator.d.ts +5 -10
- package/dist/compile/handler/frozen-validator.js +55 -105
- package/dist/compile/handler/frozen-validator.mjs +53 -105
- package/dist/compile/handler/index.d.ts +7 -5
- package/dist/compile/handler/index.js +270 -111
- package/dist/compile/handler/index.mjs +270 -113
- package/dist/compile/handler/jit.d.ts +19 -17
- package/dist/compile/handler/jit.js +222 -149
- package/dist/compile/handler/jit.mjs +224 -152
- package/dist/compile/handler/params.d.ts +1 -4
- package/dist/compile/handler/params.js +10 -5
- package/dist/compile/handler/params.mjs +14 -8
- package/dist/compile/handler/reconstruct.d.ts +2 -2
- package/dist/compile/handler/reconstruct.js +6 -4
- package/dist/compile/handler/reconstruct.mjs +6 -4
- package/dist/compile/handler/static-clone-resolver.d.ts +6 -0
- package/dist/compile/handler/static-clone-resolver.js +8 -0
- package/dist/compile/handler/static-clone-resolver.mjs +7 -0
- package/dist/compile/handler/utils.d.ts +22 -11
- package/dist/compile/handler/utils.js +159 -253
- package/dist/compile/handler/utils.mjs +155 -252
- package/dist/compile/jit-probe.d.ts +2 -2
- package/dist/compile/jit-probe.js +2 -3
- package/dist/compile/jit-probe.mjs +2 -3
- package/dist/compile/lexer.d.ts +7 -4
- package/dist/compile/lexer.js +268 -6
- package/dist/compile/lexer.mjs +268 -4
- package/dist/compile/utils.js +7 -4
- package/dist/compile/utils.mjs +7 -4
- package/dist/compiled.d.ts +0 -1
- package/dist/compiled.js +0 -1
- package/dist/compiled.mjs +0 -1
- package/dist/context.d.ts +14 -4
- package/dist/context.js +17 -32
- package/dist/context.mjs +19 -34
- package/dist/cookie/config.d.ts +7 -2
- package/dist/cookie/config.js +76 -16
- package/dist/cookie/config.mjs +77 -18
- package/dist/cookie/cookie.d.ts +1 -1
- package/dist/cookie/cookie.js +8 -10
- package/dist/cookie/cookie.mjs +8 -10
- package/dist/cookie/crypto.d.ts +8 -7
- package/dist/cookie/crypto.js +51 -40
- package/dist/cookie/crypto.mjs +51 -41
- package/dist/cookie/error.d.ts +1 -2
- package/dist/cookie/error.js +6 -4
- package/dist/cookie/error.mjs +6 -4
- package/dist/cookie/lib.js +7 -2
- package/dist/cookie/lib.mjs +7 -2
- package/dist/cookie/serialize.js +1 -1
- package/dist/cookie/serialize.mjs +1 -1
- package/dist/cookie/types.d.ts +10 -0
- package/dist/cookie/utils.d.ts +4 -8
- package/dist/cookie/utils.js +86 -52
- package/dist/cookie/utils.mjs +89 -54
- package/dist/error.d.ts +113 -103
- package/dist/error.js +181 -117
- package/dist/error.mjs +179 -118
- package/dist/generation.d.ts +2 -2
- package/dist/handler/error.d.ts +26 -15
- package/dist/handler/error.js +77 -96
- package/dist/handler/error.mjs +74 -97
- package/dist/handler/fetch.js +87 -108
- package/dist/handler/fetch.mjs +89 -110
- package/dist/handler/utils.d.ts +12 -6
- package/dist/handler/utils.js +84 -28
- package/dist/handler/utils.mjs +80 -28
- package/dist/index.d.ts +12 -12
- package/dist/index.js +2 -9
- package/dist/index.mjs +1 -1
- package/dist/memory.js +1 -1
- package/dist/memory.mjs +1 -1
- package/dist/package.js +1 -1
- package/dist/package.mjs +1 -1
- package/dist/plugin/aot/bun.d.ts +8 -1
- package/dist/plugin/aot/bun.js +16 -9
- package/dist/plugin/aot/bun.mjs +17 -10
- package/dist/plugin/aot/core.d.ts +52 -60
- package/dist/plugin/aot/core.js +208 -109
- package/dist/plugin/aot/core.mjs +205 -111
- package/dist/plugin/aot/esbuild.js +8 -5
- package/dist/plugin/aot/esbuild.mjs +9 -6
- package/dist/plugin/aot/hooks.d.ts +6 -17
- package/dist/plugin/aot/hooks.js +36 -14
- package/dist/plugin/aot/hooks.mjs +37 -16
- package/dist/plugin/aot/rspack-loader.d.ts +1 -5
- package/dist/plugin/aot/rspack-loader.js +1 -10
- package/dist/plugin/aot/rspack-loader.mjs +1 -10
- package/dist/plugin/aot/rspack.js +2 -5
- package/dist/plugin/aot/rspack.mjs +3 -6
- package/dist/plugin/aot/source.d.ts +6 -42
- package/dist/plugin/aot/source.js +71 -71
- package/dist/plugin/aot/source.mjs +72 -72
- package/dist/plugin/aot/treeshake.js +2 -6
- package/dist/plugin/aot/treeshake.mjs +2 -6
- package/dist/plugin/aot/unplugin.d.ts +1 -4
- package/dist/plugin/aot/unplugin.js +4 -13
- package/dist/plugin/aot/unplugin.mjs +4 -13
- package/dist/plugin/aot/vite.d.ts +2 -6
- package/dist/plugin/aot/worker.js +2 -2
- package/dist/plugin/aot/worker.mjs +2 -2
- package/dist/plugin/auto-head.js +1 -10
- package/dist/plugin/auto-head.mjs +1 -10
- package/dist/plugin/trace.d.ts +2 -1
- package/dist/route-table.d.ts +3 -0
- package/dist/route-table.js +16 -11
- package/dist/route-table.mjs +16 -11
- package/dist/schema-snapshot.d.ts +0 -4
- package/dist/schema-snapshot.js +32 -49
- package/dist/schema-snapshot.mjs +32 -49
- package/dist/sucrose.d.ts +6 -55
- package/dist/sucrose.js +103 -731
- package/dist/sucrose.mjs +102 -724
- package/dist/trace.d.ts +5 -8
- package/dist/trace.js +23 -134
- package/dist/trace.mjs +24 -135
- package/dist/type/bridge-live.d.ts +4 -2
- package/dist/type/bridge-live.js +6 -3
- package/dist/type/bridge-live.mjs +4 -2
- package/dist/type/bridge.d.ts +6 -4
- package/dist/type/bridge.js +17 -9
- package/dist/type/bridge.mjs +5 -3
- package/dist/type/coerce-plan.d.ts +5 -5
- package/dist/type/coerce-plan.js +41 -39
- package/dist/type/coerce-plan.mjs +43 -39
- package/dist/type/coerce.d.ts +3 -7
- package/dist/type/coerce.js +18 -75
- package/dist/type/coerce.mjs +19 -76
- package/dist/type/compat.d.ts +3 -0
- package/dist/type/compat.js +23 -14
- package/dist/type/compat.mjs +20 -10
- package/dist/type/elysia/array-buffer.d.ts +4 -1
- package/dist/type/elysia/array-buffer.js +17 -17
- package/dist/type/elysia/array-buffer.mjs +17 -17
- package/dist/type/elysia/array-string.js +2 -40
- package/dist/type/elysia/array-string.mjs +2 -39
- package/dist/type/elysia/array.js +5 -9
- package/dist/type/elysia/array.mjs +5 -9
- package/dist/type/elysia/boolean-string.js +2 -3
- package/dist/type/elysia/boolean-string.mjs +1 -1
- package/dist/type/elysia/boolean.js +3 -12
- package/dist/type/elysia/boolean.mjs +3 -11
- package/dist/type/elysia/cookie.d.ts +3 -4
- package/dist/type/elysia/cookie.js +5 -15
- package/dist/type/elysia/cookie.mjs +5 -15
- package/dist/type/elysia/date.js +13 -20
- package/dist/type/elysia/date.mjs +10 -16
- package/dist/type/elysia/file-type.d.ts +11 -4
- package/dist/type/elysia/file-type.js +17 -2
- package/dist/type/elysia/file-type.mjs +17 -3
- package/dist/type/elysia/file.d.ts +1 -3
- package/dist/type/elysia/file.js +11 -27
- package/dist/type/elysia/file.mjs +11 -12
- package/dist/type/elysia/files.js +7 -11
- package/dist/type/elysia/files.mjs +7 -10
- package/dist/type/elysia/form.js +2 -3
- package/dist/type/elysia/form.mjs +3 -3
- package/dist/type/elysia/integer-string.d.ts +1 -1
- package/dist/type/elysia/integer-string.js +4 -30
- package/dist/type/elysia/integer-string.mjs +5 -30
- package/dist/type/elysia/integer.js +3 -12
- package/dist/type/elysia/integer.mjs +3 -11
- package/dist/type/elysia/intersect.js +0 -5
- package/dist/type/elysia/intersect.mjs +0 -5
- package/dist/type/elysia/json-string.d.ts +7 -0
- package/dist/type/elysia/json-string.js +47 -0
- package/dist/type/elysia/json-string.mjs +46 -0
- package/dist/type/elysia/keep-config.d.ts +14 -0
- package/dist/type/elysia/keep-config.js +62 -0
- package/dist/type/elysia/keep-config.mjs +53 -0
- package/dist/type/elysia/maybe-empty.js +3 -4
- package/dist/type/elysia/maybe-empty.mjs +1 -1
- package/dist/type/elysia/nullable.js +2 -3
- package/dist/type/elysia/nullable.mjs +1 -1
- package/dist/type/elysia/number.d.ts +2 -1
- package/dist/type/elysia/number.js +19 -11
- package/dist/type/elysia/number.mjs +18 -10
- package/dist/type/elysia/numeric-enum.d.ts +1 -1
- package/dist/type/elysia/numeric-enum.js +4 -5
- package/dist/type/elysia/numeric-enum.mjs +3 -3
- package/dist/type/elysia/numeric.d.ts +4 -2
- package/dist/type/elysia/numeric.js +20 -17
- package/dist/type/elysia/numeric.mjs +19 -16
- package/dist/type/elysia/object-string.js +2 -37
- package/dist/type/elysia/object-string.mjs +2 -36
- package/dist/type/elysia/object.js +5 -9
- package/dist/type/elysia/object.mjs +5 -9
- package/dist/type/elysia/optional.js +8 -8
- package/dist/type/elysia/optional.mjs +7 -7
- package/dist/type/elysia/problem.d.ts +20 -0
- package/dist/type/elysia/problem.js +25 -0
- package/dist/type/elysia/problem.mjs +24 -0
- package/dist/type/elysia/string.js +4 -3
- package/dist/type/elysia/string.mjs +4 -2
- package/dist/type/elysia/uint8-array.d.ts +1 -1
- package/dist/type/elysia/uint8-array.js +5 -20
- package/dist/type/elysia/uint8-array.mjs +5 -19
- package/dist/type/elysia/union-enum.js +7 -8
- package/dist/type/elysia/union-enum.mjs +6 -6
- package/dist/type/elysia/union.js +0 -5
- package/dist/type/elysia/union.mjs +0 -5
- package/dist/type/elysia/utils.d.ts +2 -4
- package/dist/type/elysia/utils.js +10 -21
- package/dist/type/elysia/utils.mjs +7 -15
- package/dist/type/exports.d.ts +4 -2
- package/dist/type/exports.js +23 -2
- package/dist/type/exports.mjs +11 -2
- package/dist/type/index.d.ts +10 -2
- package/dist/type/index.js +62 -15
- package/dist/type/index.mjs +65 -11
- package/dist/type/shared.d.ts +3 -9
- package/dist/type/shared.js +44 -97
- package/dist/type/shared.mjs +43 -97
- package/dist/type/sync-require.d.ts +11 -0
- package/dist/type/sync-require.js +14 -0
- package/dist/type/sync-require.mjs +12 -0
- package/dist/type/typebox-system-lite.d.ts +2 -0
- package/dist/type/typebox-system-lite.js +34 -0
- package/dist/type/typebox-system-lite.mjs +3 -0
- package/dist/type/typebox-system-require.d.ts +7 -0
- package/dist/type/typebox-system-require.js +24 -0
- package/dist/type/typebox-system-require.mjs +23 -0
- package/dist/type/typebox-type-live.d.ts +11 -0
- package/dist/type/typebox-type-live.js +45 -0
- package/dist/type/typebox-type-live.mjs +20 -0
- package/dist/type/typebox-type.d.ts +34 -0
- package/dist/type/typebox-type.js +199 -0
- package/dist/type/typebox-type.mjs +86 -0
- package/dist/type/typebox-value-live.d.ts +11 -0
- package/dist/type/typebox-value-live.js +31 -0
- package/dist/type/typebox-value-live.mjs +13 -0
- package/dist/type/typebox-value-ops.d.ts +4 -0
- package/dist/type/typebox-value-ops.js +90 -0
- package/dist/type/typebox-value-ops.mjs +5 -0
- package/dist/type/typebox-value-require.d.ts +8 -0
- package/dist/type/typebox-value-require.js +28 -0
- package/dist/type/typebox-value-require.mjs +25 -0
- package/dist/type/typebox-value.d.ts +35 -0
- package/dist/type/typebox-value.js +161 -0
- package/dist/type/typebox-value.mjs +74 -0
- package/dist/type/utils.js +1 -10
- package/dist/type/utils.mjs +1 -10
- package/dist/type/validator/clean-safe.d.ts +2 -4
- package/dist/type/validator/clean-safe.js +15 -11
- package/dist/type/validator/clean-safe.mjs +16 -10
- package/dist/type/validator/custom-error.js +12 -16
- package/dist/type/validator/custom-error.mjs +11 -14
- package/dist/type/validator/default-precompute.d.ts +9 -1
- package/dist/type/validator/default-precompute.js +40 -77
- package/dist/type/validator/default-precompute.mjs +37 -74
- package/dist/type/validator/exact-mirror-live.d.ts +8 -0
- package/dist/type/validator/exact-mirror-live.js +22 -0
- package/dist/type/validator/exact-mirror-live.mjs +17 -0
- package/dist/type/validator/exact-mirror-require.d.ts +4 -0
- package/dist/type/validator/exact-mirror-require.js +11 -0
- package/dist/type/validator/exact-mirror-require.mjs +9 -0
- package/dist/type/validator/exact-mirror.js +8 -8
- package/dist/type/validator/exact-mirror.mjs +9 -8
- package/dist/type/validator/frozen-check.js +5 -6
- package/dist/type/validator/frozen-check.mjs +6 -7
- package/dist/type/validator/index.d.ts +1 -0
- package/dist/type/validator/index.js +169 -166
- package/dist/type/validator/index.mjs +162 -158
- package/dist/type/validator/string-codec-aot.d.ts +2 -1
- package/dist/type/validator/string-codec-aot.js +8 -2
- package/dist/type/validator/string-codec-aot.mjs +7 -2
- package/dist/type/validator/validator-cache.d.ts +5 -4
- package/dist/type/validator/validator-cache.js +41 -24
- package/dist/type/validator/validator-cache.mjs +40 -22
- package/dist/types.d.ts +263 -95
- package/dist/universal/constants.d.ts +2 -1
- package/dist/universal/constants.js +3 -1
- package/dist/universal/constants.mjs +2 -1
- package/dist/universal/env.js +2 -3
- package/dist/universal/env.mjs +2 -3
- package/dist/universal/file.d.ts +6 -4
- package/dist/universal/file.js +11 -16
- package/dist/universal/file.mjs +12 -16
- package/dist/universal/index.d.ts +2 -2
- package/dist/universal/is-production.d.ts +1 -2
- package/dist/universal/is-production.js +1 -3
- package/dist/universal/is-production.mjs +2 -3
- package/dist/universal/server.d.ts +0 -272
- package/dist/utils.d.ts +43 -7
- package/dist/utils.js +181 -126
- package/dist/utils.mjs +168 -126
- package/dist/validator/index.d.ts +1 -1
- package/dist/validator/index.js +17 -30
- package/dist/validator/index.mjs +18 -31
- package/dist/validator/route.js +2 -1
- package/dist/validator/route.mjs +2 -1
- package/dist/ws/context.d.ts +8 -1
- package/dist/ws/context.js +68 -44
- package/dist/ws/context.mjs +67 -44
- package/dist/ws/parser.js +10 -20
- package/dist/ws/parser.mjs +10 -20
- package/dist/ws/route.d.ts +2 -2
- package/dist/ws/route.js +249 -141
- package/dist/ws/route.mjs +254 -146
- package/dist/ws/types.d.ts +2 -2
- package/package.json +23 -49
- package/dist/compile/analysis-cache.d.ts +0 -6
- package/dist/compile/analysis-cache.js +0 -12
- package/dist/compile/analysis-cache.mjs +0 -11
- package/dist/cookie/index.d.ts +0 -7
- package/dist/cookie/index.js +0 -13
- package/dist/cookie/index.mjs +0 -7
- package/dist/handler/index.d.ts +0 -2
- package/dist/handler/index.js +0 -5
- package/dist/handler/index.mjs +0 -3
package/dist/handler/error.js
CHANGED
|
@@ -8,6 +8,12 @@ const require_parse_query = require('../parse-query.js');
|
|
|
8
8
|
const require_handler_utils = require('./utils.js');
|
|
9
9
|
|
|
10
10
|
//#region src/handler/error.ts
|
|
11
|
+
const defineQuery = (context, value) => Object.defineProperty(context, "query", {
|
|
12
|
+
value,
|
|
13
|
+
writable: true,
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true
|
|
16
|
+
});
|
|
11
17
|
function parseQuery(context) {
|
|
12
18
|
const c = context;
|
|
13
19
|
if (c.query !== void 0 || c.qi === void 0) return;
|
|
@@ -16,42 +22,23 @@ function parseQuery(context) {
|
|
|
16
22
|
enumerable: true,
|
|
17
23
|
get() {
|
|
18
24
|
const value = require_parse_query.parseQueryFromURL(c.request.url, c.qi);
|
|
19
|
-
|
|
20
|
-
value,
|
|
21
|
-
writable: true,
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true
|
|
24
|
-
});
|
|
25
|
+
defineQuery(context, value);
|
|
25
26
|
return value;
|
|
26
27
|
},
|
|
27
28
|
set(value) {
|
|
28
|
-
|
|
29
|
-
value,
|
|
30
|
-
writable: true,
|
|
31
|
-
enumerable: true,
|
|
32
|
-
configurable: true
|
|
33
|
-
});
|
|
29
|
+
defineQuery(context, value);
|
|
34
30
|
}
|
|
35
31
|
});
|
|
36
32
|
}
|
|
37
33
|
const isPristineNotFound = (context, error) => error instanceof require_error.NotFound && error.response === "Not Found" && !context.set.cookie && !require_utils.isNotEmpty(context.set.headers);
|
|
38
|
-
|
|
39
|
-
* Adopt the intercepted error's `type` into a problem the hook produced
|
|
40
|
-
* without one.
|
|
41
|
-
*
|
|
42
|
-
* `'about:blank'` is RFC 9457's "unspecified" default, so it's the only value
|
|
43
|
-
* worth replacing — an explicit type the handler set always wins.
|
|
44
|
-
*
|
|
45
|
-
* The result is copied rather than mutated: `.error(Class, value)` wraps a
|
|
46
|
-
* non-function into `() => value`, so the very same `ElysiaStatus` is replayed
|
|
47
|
-
* on every request and an in-place write would leak across requests
|
|
48
|
-
*/
|
|
34
|
+
const claimsProblemType = (error) => typeof error?.type === "string" ? !(error instanceof require_error.ValidationError) && error.constructor?.name !== "ValidationError" : error instanceof require_error.HTTPError;
|
|
49
35
|
function adoptErrorType(result, error) {
|
|
50
36
|
const body = result?.response;
|
|
51
|
-
if (typeof error?.type !== "string" || body?.type !== "about:blank" || result.headers?.["content-type"] !== "application/problem+json") return result;
|
|
52
|
-
return new require_error.ElysiaStatus(result.
|
|
37
|
+
if (typeof error?.type !== "string" || !claimsProblemType(error) || body?.type !== "about:blank" || result.headers?.["content-type"] !== "application/problem+json") return result;
|
|
38
|
+
return new require_error.ElysiaStatus(result.status, {
|
|
53
39
|
...body,
|
|
54
|
-
type: error.type
|
|
40
|
+
type: error.type,
|
|
41
|
+
...typeof error.code === "string" ? { code: error.code } : {}
|
|
55
42
|
}, result.headers);
|
|
56
43
|
}
|
|
57
44
|
/**
|
|
@@ -60,13 +47,13 @@ function adoptErrorType(result, error) {
|
|
|
60
47
|
* Exported for the JIT: a route with error hooks emits its own catch block,
|
|
61
48
|
* which must land on *this* function rather than re-implementing it
|
|
62
49
|
*/
|
|
63
|
-
function fallbackResponse(context, error, mapResponse
|
|
50
|
+
function fallbackResponse(context, error, mapResponse) {
|
|
64
51
|
if (typeof error?.toResponse === "function") try {
|
|
65
52
|
const r = error.toResponse();
|
|
66
|
-
if (r instanceof Promise) return r.then((resolved) => resolved instanceof Response ? mapResponse(resolved, context.set, context) : fallbackErrorResponse(context, error, mapResponse
|
|
53
|
+
if (r instanceof Promise) return r.then((resolved) => resolved instanceof Response ? mapResponse(resolved, context.set, context) : fallbackErrorResponse(context, error, mapResponse), () => fallbackErrorResponse(context, error, mapResponse));
|
|
67
54
|
if (r instanceof Response) return mapResponse(r, context.set, context);
|
|
68
55
|
} catch {}
|
|
69
|
-
return fallbackErrorResponse(context, error, mapResponse
|
|
56
|
+
return fallbackErrorResponse(context, error, mapResponse);
|
|
70
57
|
}
|
|
71
58
|
/**
|
|
72
59
|
* Numeric form of an annotated status, which may be written as a name.
|
|
@@ -78,16 +65,17 @@ const resolveStatus = (status) => typeof status === "string" ? require_constants
|
|
|
78
65
|
* Body served by an error that carries a status but no usable body:
|
|
79
66
|
* its declared `response`, otherwise its message
|
|
80
67
|
*/
|
|
81
|
-
|
|
68
|
+
function statusFallbackBody(error, status) {
|
|
69
|
+
const masked = require_universal_is_production.isProduction() && status >= 500;
|
|
70
|
+
const declared = error.response;
|
|
71
|
+
return declared !== void 0 && !(masked && typeof declared === "object") ? declared : masked ? "Internal Server Error" : error.message ?? "";
|
|
72
|
+
}
|
|
82
73
|
/**
|
|
83
74
|
* RFC 9457 problem document carrying `detail` verbatim, mirroring `problem()`.
|
|
84
|
-
*
|
|
85
|
-
* `detail` is served exactly as annotated — objects included — it is never
|
|
86
|
-
* spread into the envelope. `problemBody` only defaults `type` when the key is
|
|
87
|
-
* *absent*, so an error without one has to supply `'about:blank'` itself
|
|
88
75
|
*/
|
|
89
|
-
const problemOf = (self, detail, status) => new require_error.ElysiaStatus(status, require_error.problemBody({
|
|
76
|
+
const problemOf = (self, detail, status, claimsProblem) => new require_error.ElysiaStatus(status, require_error.problemBody({
|
|
90
77
|
type: self.type ?? "about:blank",
|
|
78
|
+
...claimsProblem && typeof self.code === "string" ? { code: self.code } : {},
|
|
91
79
|
detail,
|
|
92
80
|
status
|
|
93
81
|
}), { "content-type": require_error.PROBLEM_JSON });
|
|
@@ -104,114 +92,107 @@ const readAnnotation = (self, key, claimsProblem) => {
|
|
|
104
92
|
const annotation = self[key];
|
|
105
93
|
return typeof annotation === "function" ? claimsProblem ? annotation.call(self) : void 0 : annotation;
|
|
106
94
|
};
|
|
107
|
-
function fallbackErrorResponse(context, error, mapResponse
|
|
95
|
+
function fallbackErrorResponse(context, error, mapResponse) {
|
|
108
96
|
if (error instanceof require_error.ElysiaStatus) return mapResponse(error, context.set, context);
|
|
109
|
-
|
|
97
|
+
if (error instanceof Response) return mapResponse(error, context.set, context);
|
|
98
|
+
const self = error ?? {};
|
|
110
99
|
const status = resolveStatus(self.status);
|
|
111
100
|
const owned = error instanceof require_error.HTTPError;
|
|
112
|
-
const claimsProblem =
|
|
101
|
+
const claimsProblem = claimsProblemType(error);
|
|
113
102
|
const served = typeof status === "number" ? status : resolveStatus(context.set.status);
|
|
114
103
|
const failed = (cause) => {
|
|
115
104
|
context.set.status = 500;
|
|
116
105
|
return mapResponse(require_error.internalServerErrorResponse(cause), context.set, context);
|
|
117
106
|
};
|
|
118
|
-
|
|
107
|
+
function mergeHeaders() {
|
|
119
108
|
if (self.headers) Object.assign(require_adapter_utils.materializeSetHeaders(context.set), self.headers);
|
|
120
|
-
}
|
|
109
|
+
}
|
|
121
110
|
const legacy = () => {
|
|
122
111
|
if (error?.status) return mapResponse(statusFallbackBody(error, status), context.set, context);
|
|
123
112
|
if (error?.message != null) {
|
|
124
113
|
if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
125
114
|
return mapResponse(require_error.internalServerErrorResponse(error), context.set, context);
|
|
126
115
|
}
|
|
127
|
-
return
|
|
116
|
+
return mapResponse(require_error.internalServerErrorResponse(error), context.set, context);
|
|
128
117
|
};
|
|
129
|
-
const serveMessage = () =>
|
|
130
|
-
|
|
131
|
-
let detail;
|
|
132
|
-
try {
|
|
133
|
-
detail = readAnnotation(self, "detail", claimsProblem);
|
|
134
|
-
} catch (cause) {
|
|
135
|
-
return failed(cause);
|
|
136
|
-
}
|
|
137
|
-
if (detail === void 0) return serveMessage();
|
|
138
|
-
if (detail instanceof Promise) return detail.then((resolved) => {
|
|
139
|
-
if (resolved === void 0) return serveMessage();
|
|
140
|
-
mergeHeaders();
|
|
141
|
-
return mapResponse(problemOf(self, resolved, served), context.set, context);
|
|
142
|
-
}, failed);
|
|
118
|
+
const serveMessage = () => {
|
|
119
|
+
if (!claimsProblem) return legacy();
|
|
143
120
|
mergeHeaders();
|
|
144
|
-
return mapResponse(problemOf(self,
|
|
121
|
+
return mapResponse(problemOf(self, statusFallbackBody(error, served), served, claimsProblem), context.set, context);
|
|
145
122
|
};
|
|
146
|
-
|
|
147
|
-
let
|
|
123
|
+
const tier = (key) => {
|
|
124
|
+
let annotation;
|
|
148
125
|
try {
|
|
149
|
-
|
|
126
|
+
annotation = readAnnotation(self, key, claimsProblem);
|
|
150
127
|
} catch (cause) {
|
|
151
128
|
return failed(cause);
|
|
152
129
|
}
|
|
153
|
-
if (
|
|
154
|
-
if (
|
|
155
|
-
if (resolved === void 0) return
|
|
130
|
+
if (annotation === void 0) return key === "value" ? tier("detail") : serveMessage();
|
|
131
|
+
if (annotation instanceof Promise) return annotation.then((resolved) => {
|
|
132
|
+
if (resolved === void 0) return key === "value" ? tier("detail") : serveMessage();
|
|
156
133
|
mergeHeaders();
|
|
157
|
-
return mapResponse(resolved, context.set, context);
|
|
134
|
+
return mapResponse(key === "value" ? resolved : problemOf(self, resolved, served, claimsProblem), context.set, context);
|
|
158
135
|
}, failed);
|
|
159
136
|
mergeHeaders();
|
|
160
|
-
return mapResponse(value, context.set, context);
|
|
161
|
-
}
|
|
137
|
+
return mapResponse(key === "value" ? annotation : problemOf(self, annotation, served, claimsProblem), context.set, context);
|
|
138
|
+
};
|
|
139
|
+
if (error instanceof require_error.ValidationError) return mapResponse(error.type === "response" ? require_error.internalServerErrorResponse(void 0) : new require_error.ElysiaStatus(422, require_error.problemBody({
|
|
140
|
+
type: "validation",
|
|
141
|
+
code: "validation",
|
|
142
|
+
title: "Validation Error",
|
|
143
|
+
status: 422
|
|
144
|
+
}), { "content-type": require_error.PROBLEM_JSON }), context.set, context);
|
|
145
|
+
if (owned || error instanceof Error && typeof status === "number" && status >= 100 && !(require_universal_is_production.isProduction() && status >= 500)) return tier("value");
|
|
162
146
|
return legacy();
|
|
163
147
|
}
|
|
164
148
|
function applyErrorStatus(context, error) {
|
|
165
149
|
if (error?.status) context.set.status = error.status;
|
|
166
150
|
else if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
167
151
|
}
|
|
168
|
-
function createErrorHandler(onErrors, mapResponse,
|
|
169
|
-
|
|
152
|
+
function createErrorHandler(onErrors, mapResponse, allowUnsafe = false) {
|
|
153
|
+
const enter = (context, error) => {
|
|
170
154
|
context.error = error;
|
|
171
155
|
if (allowUnsafe && error instanceof require_error.ValidationError) error.allowUnsafeValidationDetails = true;
|
|
172
156
|
applyErrorStatus(context, error);
|
|
173
157
|
parseQuery(context);
|
|
174
|
-
return fallbackResponse(context, error, mapResponse, defaultError);
|
|
175
158
|
};
|
|
176
|
-
const
|
|
177
|
-
if (
|
|
159
|
+
const mapWith = (sign) => sign ? (response, set, context) => mapResponse(response, set, context, sign) : mapResponse;
|
|
160
|
+
if (!onErrors) return (context, error, sign) => {
|
|
161
|
+
enter(context, error);
|
|
162
|
+
return fallbackResponse(context, error, mapWith(sign));
|
|
163
|
+
};
|
|
164
|
+
const respond = (context, error, result, sign) => {
|
|
165
|
+
if (result instanceof require_error.ElysiaStatus || result instanceof Response) context.set.status = result.status;
|
|
166
|
+
else if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
167
|
+
return mapWith(sign)(adoptErrorType(result, error), context.set, context);
|
|
168
|
+
};
|
|
169
|
+
const settle = (context, error, sign) => isPristineNotFound(context, error) ? require_handler_utils.getNotFound() : fallbackResponse(context, error, mapWith(sign));
|
|
170
|
+
if (require_handler_utils.hasAsync(onErrors)) return async (context, error, sign) => {
|
|
178
171
|
require_adapter_utils.materializeSetHeaders(context.set);
|
|
179
|
-
context
|
|
180
|
-
if (allowUnsafe && error instanceof require_error.ValidationError) error.allowUnsafeValidationDetails = true;
|
|
181
|
-
applyErrorStatus(context, error);
|
|
182
|
-
parseQuery(context);
|
|
172
|
+
enter(context, error);
|
|
183
173
|
for (let i = 0; i < onErrors.length; i++) {
|
|
184
|
-
let result =
|
|
185
|
-
if (result
|
|
186
|
-
if (result !== void 0)
|
|
187
|
-
if (result instanceof require_error.ElysiaStatus || result instanceof Response) context.set.status = result.status;
|
|
188
|
-
else if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
189
|
-
return mapResponse(adoptErrorType(result, error), context.set, context);
|
|
190
|
-
}
|
|
174
|
+
let result = onErrors[i](context);
|
|
175
|
+
if (typeof result?.then === "function") result = await result;
|
|
176
|
+
if (result !== void 0) return respond(context, error, result, sign);
|
|
191
177
|
}
|
|
192
|
-
|
|
193
|
-
return fallbackResponse(context, error, mapResponse, defaultError);
|
|
178
|
+
return settle(context, error, sign);
|
|
194
179
|
};
|
|
195
|
-
return (context, error) => {
|
|
180
|
+
return (context, error, sign) => {
|
|
196
181
|
require_adapter_utils.materializeSetHeaders(context.set);
|
|
197
|
-
context
|
|
198
|
-
if (allowUnsafe && error instanceof require_error.ValidationError) error.allowUnsafeValidationDetails = true;
|
|
199
|
-
applyErrorStatus(context, error);
|
|
200
|
-
parseQuery(context);
|
|
182
|
+
enter(context, error);
|
|
201
183
|
for (let i = 0; i < onErrors.length; i++) {
|
|
202
184
|
const result = onErrors[i](context);
|
|
203
|
-
if (result !== void 0)
|
|
204
|
-
if (result instanceof require_error.ElysiaStatus || result instanceof Response) context.set.status = result.status;
|
|
205
|
-
else if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
206
|
-
return mapResponse(adoptErrorType(result, error), context.set, context);
|
|
207
|
-
}
|
|
185
|
+
if (result !== void 0) return respond(context, error, result, sign);
|
|
208
186
|
}
|
|
209
|
-
|
|
210
|
-
return fallbackResponse(context, error, mapResponse, defaultError);
|
|
187
|
+
return settle(context, error, sign);
|
|
211
188
|
};
|
|
212
189
|
}
|
|
213
190
|
|
|
214
191
|
//#endregion
|
|
215
192
|
exports.adoptErrorType = adoptErrorType;
|
|
193
|
+
exports.claimsProblemType = claimsProblemType;
|
|
216
194
|
exports.createErrorHandler = createErrorHandler;
|
|
217
|
-
exports.fallbackResponse = fallbackResponse;
|
|
195
|
+
exports.fallbackResponse = fallbackResponse;
|
|
196
|
+
exports.readAnnotation = readAnnotation;
|
|
197
|
+
exports.resolveStatus = resolveStatus;
|
|
198
|
+
exports.statusFallbackBody = statusFallbackBody;
|
package/dist/handler/error.mjs
CHANGED
|
@@ -4,9 +4,15 @@ import { isProduction } from "../universal/is-production.mjs";
|
|
|
4
4
|
import { ElysiaStatus, HTTPError, NotFound, PROBLEM_JSON, ValidationError, internalServerErrorResponse, problemBody } from "../error.mjs";
|
|
5
5
|
import { materializeSetHeaders } from "../adapter/utils.mjs";
|
|
6
6
|
import { parseQueryFromURL } from "../parse-query.mjs";
|
|
7
|
-
import {
|
|
7
|
+
import { getNotFound, hasAsync } from "./utils.mjs";
|
|
8
8
|
|
|
9
9
|
//#region src/handler/error.ts
|
|
10
|
+
const defineQuery = (context, value) => Object.defineProperty(context, "query", {
|
|
11
|
+
value,
|
|
12
|
+
writable: true,
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true
|
|
15
|
+
});
|
|
10
16
|
function parseQuery(context) {
|
|
11
17
|
const c = context;
|
|
12
18
|
if (c.query !== void 0 || c.qi === void 0) return;
|
|
@@ -15,42 +21,23 @@ function parseQuery(context) {
|
|
|
15
21
|
enumerable: true,
|
|
16
22
|
get() {
|
|
17
23
|
const value = parseQueryFromURL(c.request.url, c.qi);
|
|
18
|
-
|
|
19
|
-
value,
|
|
20
|
-
writable: true,
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true
|
|
23
|
-
});
|
|
24
|
+
defineQuery(context, value);
|
|
24
25
|
return value;
|
|
25
26
|
},
|
|
26
27
|
set(value) {
|
|
27
|
-
|
|
28
|
-
value,
|
|
29
|
-
writable: true,
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true
|
|
32
|
-
});
|
|
28
|
+
defineQuery(context, value);
|
|
33
29
|
}
|
|
34
30
|
});
|
|
35
31
|
}
|
|
36
32
|
const isPristineNotFound = (context, error) => error instanceof NotFound && error.response === "Not Found" && !context.set.cookie && !isNotEmpty(context.set.headers);
|
|
37
|
-
|
|
38
|
-
* Adopt the intercepted error's `type` into a problem the hook produced
|
|
39
|
-
* without one.
|
|
40
|
-
*
|
|
41
|
-
* `'about:blank'` is RFC 9457's "unspecified" default, so it's the only value
|
|
42
|
-
* worth replacing — an explicit type the handler set always wins.
|
|
43
|
-
*
|
|
44
|
-
* The result is copied rather than mutated: `.error(Class, value)` wraps a
|
|
45
|
-
* non-function into `() => value`, so the very same `ElysiaStatus` is replayed
|
|
46
|
-
* on every request and an in-place write would leak across requests
|
|
47
|
-
*/
|
|
33
|
+
const claimsProblemType = (error) => typeof error?.type === "string" ? !(error instanceof ValidationError) && error.constructor?.name !== "ValidationError" : error instanceof HTTPError;
|
|
48
34
|
function adoptErrorType(result, error) {
|
|
49
35
|
const body = result?.response;
|
|
50
|
-
if (typeof error?.type !== "string" || body?.type !== "about:blank" || result.headers?.["content-type"] !== "application/problem+json") return result;
|
|
51
|
-
return new ElysiaStatus(result.
|
|
36
|
+
if (typeof error?.type !== "string" || !claimsProblemType(error) || body?.type !== "about:blank" || result.headers?.["content-type"] !== "application/problem+json") return result;
|
|
37
|
+
return new ElysiaStatus(result.status, {
|
|
52
38
|
...body,
|
|
53
|
-
type: error.type
|
|
39
|
+
type: error.type,
|
|
40
|
+
...typeof error.code === "string" ? { code: error.code } : {}
|
|
54
41
|
}, result.headers);
|
|
55
42
|
}
|
|
56
43
|
/**
|
|
@@ -59,13 +46,13 @@ function adoptErrorType(result, error) {
|
|
|
59
46
|
* Exported for the JIT: a route with error hooks emits its own catch block,
|
|
60
47
|
* which must land on *this* function rather than re-implementing it
|
|
61
48
|
*/
|
|
62
|
-
function fallbackResponse(context, error, mapResponse
|
|
49
|
+
function fallbackResponse(context, error, mapResponse) {
|
|
63
50
|
if (typeof error?.toResponse === "function") try {
|
|
64
51
|
const r = error.toResponse();
|
|
65
|
-
if (r instanceof Promise) return r.then((resolved) => resolved instanceof Response ? mapResponse(resolved, context.set, context) : fallbackErrorResponse(context, error, mapResponse
|
|
52
|
+
if (r instanceof Promise) return r.then((resolved) => resolved instanceof Response ? mapResponse(resolved, context.set, context) : fallbackErrorResponse(context, error, mapResponse), () => fallbackErrorResponse(context, error, mapResponse));
|
|
66
53
|
if (r instanceof Response) return mapResponse(r, context.set, context);
|
|
67
54
|
} catch {}
|
|
68
|
-
return fallbackErrorResponse(context, error, mapResponse
|
|
55
|
+
return fallbackErrorResponse(context, error, mapResponse);
|
|
69
56
|
}
|
|
70
57
|
/**
|
|
71
58
|
* Numeric form of an annotated status, which may be written as a name.
|
|
@@ -77,16 +64,17 @@ const resolveStatus = (status) => typeof status === "string" ? StatusMap[status]
|
|
|
77
64
|
* Body served by an error that carries a status but no usable body:
|
|
78
65
|
* its declared `response`, otherwise its message
|
|
79
66
|
*/
|
|
80
|
-
|
|
67
|
+
function statusFallbackBody(error, status) {
|
|
68
|
+
const masked = isProduction() && status >= 500;
|
|
69
|
+
const declared = error.response;
|
|
70
|
+
return declared !== void 0 && !(masked && typeof declared === "object") ? declared : masked ? "Internal Server Error" : error.message ?? "";
|
|
71
|
+
}
|
|
81
72
|
/**
|
|
82
73
|
* RFC 9457 problem document carrying `detail` verbatim, mirroring `problem()`.
|
|
83
|
-
*
|
|
84
|
-
* `detail` is served exactly as annotated — objects included — it is never
|
|
85
|
-
* spread into the envelope. `problemBody` only defaults `type` when the key is
|
|
86
|
-
* *absent*, so an error without one has to supply `'about:blank'` itself
|
|
87
74
|
*/
|
|
88
|
-
const problemOf = (self, detail, status) => new ElysiaStatus(status, problemBody({
|
|
75
|
+
const problemOf = (self, detail, status, claimsProblem) => new ElysiaStatus(status, problemBody({
|
|
89
76
|
type: self.type ?? "about:blank",
|
|
77
|
+
...claimsProblem && typeof self.code === "string" ? { code: self.code } : {},
|
|
90
78
|
detail,
|
|
91
79
|
status
|
|
92
80
|
}), { "content-type": PROBLEM_JSON });
|
|
@@ -103,112 +91,101 @@ const readAnnotation = (self, key, claimsProblem) => {
|
|
|
103
91
|
const annotation = self[key];
|
|
104
92
|
return typeof annotation === "function" ? claimsProblem ? annotation.call(self) : void 0 : annotation;
|
|
105
93
|
};
|
|
106
|
-
function fallbackErrorResponse(context, error, mapResponse
|
|
94
|
+
function fallbackErrorResponse(context, error, mapResponse) {
|
|
107
95
|
if (error instanceof ElysiaStatus) return mapResponse(error, context.set, context);
|
|
108
|
-
|
|
96
|
+
if (error instanceof Response) return mapResponse(error, context.set, context);
|
|
97
|
+
const self = error ?? {};
|
|
109
98
|
const status = resolveStatus(self.status);
|
|
110
99
|
const owned = error instanceof HTTPError;
|
|
111
|
-
const claimsProblem =
|
|
100
|
+
const claimsProblem = claimsProblemType(error);
|
|
112
101
|
const served = typeof status === "number" ? status : resolveStatus(context.set.status);
|
|
113
102
|
const failed = (cause) => {
|
|
114
103
|
context.set.status = 500;
|
|
115
104
|
return mapResponse(internalServerErrorResponse(cause), context.set, context);
|
|
116
105
|
};
|
|
117
|
-
|
|
106
|
+
function mergeHeaders() {
|
|
118
107
|
if (self.headers) Object.assign(materializeSetHeaders(context.set), self.headers);
|
|
119
|
-
}
|
|
108
|
+
}
|
|
120
109
|
const legacy = () => {
|
|
121
110
|
if (error?.status) return mapResponse(statusFallbackBody(error, status), context.set, context);
|
|
122
111
|
if (error?.message != null) {
|
|
123
112
|
if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
124
113
|
return mapResponse(internalServerErrorResponse(error), context.set, context);
|
|
125
114
|
}
|
|
126
|
-
return
|
|
115
|
+
return mapResponse(internalServerErrorResponse(error), context.set, context);
|
|
127
116
|
};
|
|
128
|
-
const serveMessage = () =>
|
|
129
|
-
|
|
130
|
-
let detail;
|
|
131
|
-
try {
|
|
132
|
-
detail = readAnnotation(self, "detail", claimsProblem);
|
|
133
|
-
} catch (cause) {
|
|
134
|
-
return failed(cause);
|
|
135
|
-
}
|
|
136
|
-
if (detail === void 0) return serveMessage();
|
|
137
|
-
if (detail instanceof Promise) return detail.then((resolved) => {
|
|
138
|
-
if (resolved === void 0) return serveMessage();
|
|
139
|
-
mergeHeaders();
|
|
140
|
-
return mapResponse(problemOf(self, resolved, served), context.set, context);
|
|
141
|
-
}, failed);
|
|
117
|
+
const serveMessage = () => {
|
|
118
|
+
if (!claimsProblem) return legacy();
|
|
142
119
|
mergeHeaders();
|
|
143
|
-
return mapResponse(problemOf(self,
|
|
120
|
+
return mapResponse(problemOf(self, statusFallbackBody(error, served), served, claimsProblem), context.set, context);
|
|
144
121
|
};
|
|
145
|
-
|
|
146
|
-
let
|
|
122
|
+
const tier = (key) => {
|
|
123
|
+
let annotation;
|
|
147
124
|
try {
|
|
148
|
-
|
|
125
|
+
annotation = readAnnotation(self, key, claimsProblem);
|
|
149
126
|
} catch (cause) {
|
|
150
127
|
return failed(cause);
|
|
151
128
|
}
|
|
152
|
-
if (
|
|
153
|
-
if (
|
|
154
|
-
if (resolved === void 0) return
|
|
129
|
+
if (annotation === void 0) return key === "value" ? tier("detail") : serveMessage();
|
|
130
|
+
if (annotation instanceof Promise) return annotation.then((resolved) => {
|
|
131
|
+
if (resolved === void 0) return key === "value" ? tier("detail") : serveMessage();
|
|
155
132
|
mergeHeaders();
|
|
156
|
-
return mapResponse(resolved, context.set, context);
|
|
133
|
+
return mapResponse(key === "value" ? resolved : problemOf(self, resolved, served, claimsProblem), context.set, context);
|
|
157
134
|
}, failed);
|
|
158
135
|
mergeHeaders();
|
|
159
|
-
return mapResponse(value, context.set, context);
|
|
160
|
-
}
|
|
136
|
+
return mapResponse(key === "value" ? annotation : problemOf(self, annotation, served, claimsProblem), context.set, context);
|
|
137
|
+
};
|
|
138
|
+
if (error instanceof ValidationError) return mapResponse(error.type === "response" ? internalServerErrorResponse(void 0) : new ElysiaStatus(422, problemBody({
|
|
139
|
+
type: "validation",
|
|
140
|
+
code: "validation",
|
|
141
|
+
title: "Validation Error",
|
|
142
|
+
status: 422
|
|
143
|
+
}), { "content-type": PROBLEM_JSON }), context.set, context);
|
|
144
|
+
if (owned || error instanceof Error && typeof status === "number" && status >= 100 && !(isProduction() && status >= 500)) return tier("value");
|
|
161
145
|
return legacy();
|
|
162
146
|
}
|
|
163
147
|
function applyErrorStatus(context, error) {
|
|
164
148
|
if (error?.status) context.set.status = error.status;
|
|
165
149
|
else if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
166
150
|
}
|
|
167
|
-
function createErrorHandler(onErrors, mapResponse,
|
|
168
|
-
|
|
151
|
+
function createErrorHandler(onErrors, mapResponse, allowUnsafe = false) {
|
|
152
|
+
const enter = (context, error) => {
|
|
169
153
|
context.error = error;
|
|
170
154
|
if (allowUnsafe && error instanceof ValidationError) error.allowUnsafeValidationDetails = true;
|
|
171
155
|
applyErrorStatus(context, error);
|
|
172
156
|
parseQuery(context);
|
|
173
|
-
return fallbackResponse(context, error, mapResponse, defaultError);
|
|
174
157
|
};
|
|
175
|
-
const
|
|
176
|
-
if (
|
|
158
|
+
const mapWith = (sign) => sign ? (response, set, context) => mapResponse(response, set, context, sign) : mapResponse;
|
|
159
|
+
if (!onErrors) return (context, error, sign) => {
|
|
160
|
+
enter(context, error);
|
|
161
|
+
return fallbackResponse(context, error, mapWith(sign));
|
|
162
|
+
};
|
|
163
|
+
const respond = (context, error, result, sign) => {
|
|
164
|
+
if (result instanceof ElysiaStatus || result instanceof Response) context.set.status = result.status;
|
|
165
|
+
else if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
166
|
+
return mapWith(sign)(adoptErrorType(result, error), context.set, context);
|
|
167
|
+
};
|
|
168
|
+
const settle = (context, error, sign) => isPristineNotFound(context, error) ? getNotFound() : fallbackResponse(context, error, mapWith(sign));
|
|
169
|
+
if (hasAsync(onErrors)) return async (context, error, sign) => {
|
|
177
170
|
materializeSetHeaders(context.set);
|
|
178
|
-
context
|
|
179
|
-
if (allowUnsafe && error instanceof ValidationError) error.allowUnsafeValidationDetails = true;
|
|
180
|
-
applyErrorStatus(context, error);
|
|
181
|
-
parseQuery(context);
|
|
171
|
+
enter(context, error);
|
|
182
172
|
for (let i = 0; i < onErrors.length; i++) {
|
|
183
|
-
let result =
|
|
184
|
-
if (result
|
|
185
|
-
if (result !== void 0)
|
|
186
|
-
if (result instanceof ElysiaStatus || result instanceof Response) context.set.status = result.status;
|
|
187
|
-
else if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
188
|
-
return mapResponse(adoptErrorType(result, error), context.set, context);
|
|
189
|
-
}
|
|
173
|
+
let result = onErrors[i](context);
|
|
174
|
+
if (typeof result?.then === "function") result = await result;
|
|
175
|
+
if (result !== void 0) return respond(context, error, result, sign);
|
|
190
176
|
}
|
|
191
|
-
|
|
192
|
-
return fallbackResponse(context, error, mapResponse, defaultError);
|
|
177
|
+
return settle(context, error, sign);
|
|
193
178
|
};
|
|
194
|
-
return (context, error) => {
|
|
179
|
+
return (context, error, sign) => {
|
|
195
180
|
materializeSetHeaders(context.set);
|
|
196
|
-
context
|
|
197
|
-
if (allowUnsafe && error instanceof ValidationError) error.allowUnsafeValidationDetails = true;
|
|
198
|
-
applyErrorStatus(context, error);
|
|
199
|
-
parseQuery(context);
|
|
181
|
+
enter(context, error);
|
|
200
182
|
for (let i = 0; i < onErrors.length; i++) {
|
|
201
183
|
const result = onErrors[i](context);
|
|
202
|
-
if (result !== void 0)
|
|
203
|
-
if (result instanceof ElysiaStatus || result instanceof Response) context.set.status = result.status;
|
|
204
|
-
else if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
|
|
205
|
-
return mapResponse(adoptErrorType(result, error), context.set, context);
|
|
206
|
-
}
|
|
184
|
+
if (result !== void 0) return respond(context, error, result, sign);
|
|
207
185
|
}
|
|
208
|
-
|
|
209
|
-
return fallbackResponse(context, error, mapResponse, defaultError);
|
|
186
|
+
return settle(context, error, sign);
|
|
210
187
|
};
|
|
211
188
|
}
|
|
212
189
|
|
|
213
190
|
//#endregion
|
|
214
|
-
export { adoptErrorType, createErrorHandler, fallbackResponse };
|
|
191
|
+
export { adoptErrorType, claimsProblemType, createErrorHandler, fallbackResponse, readAnnotation, resolveStatus, statusFallbackBody };
|