@bessemer/cornerstone 0.5.97 → 0.5.99
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/array.d.ts +62 -1
- package/dist/array.d.ts.map +1 -1
- package/dist/array.js +24 -11
- package/dist/array.js.map +1 -1
- package/dist/aspect-ratio.d.ts +1 -1
- package/dist/aspect-ratio.d.ts.map +1 -1
- package/dist/aspect-ratio.js +4 -4
- package/dist/aspect-ratio.js.map +1 -1
- package/dist/async-value.d.ts +10 -2
- package/dist/async-value.d.ts.map +1 -1
- package/dist/async-value.js +33 -2
- package/dist/async-value.js.map +1 -1
- package/dist/cache.js +1 -1
- package/dist/cache.js.map +1 -1
- package/dist/equalitor.d.ts +1 -0
- package/dist/equalitor.d.ts.map +1 -1
- package/dist/equalitor.js +3 -0
- package/dist/equalitor.js.map +1 -1
- package/dist/error/error-cause.d.ts +35 -0
- package/dist/error/error-cause.d.ts.map +1 -0
- package/dist/error/error-cause.js +97 -0
- package/dist/error/error-cause.js.map +1 -0
- package/dist/error/error-code.d.ts +17 -0
- package/dist/error/error-code.d.ts.map +1 -0
- package/dist/error/error-code.js +32 -0
- package/dist/error/error-code.js.map +1 -0
- package/dist/error/error-event.d.ts +29 -48
- package/dist/error/error-event.d.ts.map +1 -1
- package/dist/error/error-event.js +62 -151
- package/dist/error/error-event.js.map +1 -1
- package/dist/error/error-type.d.ts +1 -0
- package/dist/error/error-type.d.ts.map +1 -1
- package/dist/error/error-type.js +4 -2
- package/dist/error/error-type.js.map +1 -1
- package/dist/function.d.ts +1 -1
- package/dist/function.d.ts.map +1 -1
- package/dist/function.js +3 -2
- package/dist/function.js.map +1 -1
- package/dist/glob.d.ts +1 -1
- package/dist/glob.d.ts.map +1 -1
- package/dist/glob.js +4 -4
- package/dist/glob.js.map +1 -1
- package/dist/hex-code.d.ts +1 -1
- package/dist/hex-code.d.ts.map +1 -1
- package/dist/hex-code.js +4 -4
- package/dist/hex-code.js.map +1 -1
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -5
- package/dist/index.js.map +1 -1
- package/dist/intl/country-code.d.ts +1 -1
- package/dist/intl/country-code.d.ts.map +1 -1
- package/dist/intl/country-code.js +4 -4
- package/dist/intl/country-code.js.map +1 -1
- package/dist/intl/country-subdivision-code.d.ts +1 -1
- package/dist/intl/country-subdivision-code.d.ts.map +1 -1
- package/dist/intl/country-subdivision-code.js +4 -4
- package/dist/intl/country-subdivision-code.js.map +1 -1
- package/dist/intl/currency-code.d.ts +1 -1
- package/dist/intl/currency-code.d.ts.map +1 -1
- package/dist/intl/currency-code.js +4 -4
- package/dist/intl/currency-code.js.map +1 -1
- package/dist/intl/language-code.d.ts +1 -1
- package/dist/intl/language-code.d.ts.map +1 -1
- package/dist/intl/language-code.js +4 -4
- package/dist/intl/language-code.js.map +1 -1
- package/dist/intl/locale.d.ts +1 -1
- package/dist/intl/locale.d.ts.map +1 -1
- package/dist/intl/locale.js +6 -6
- package/dist/intl/locale.js.map +1 -1
- package/dist/mime-type.d.ts +1 -1
- package/dist/mime-type.d.ts.map +1 -1
- package/dist/mime-type.js +4 -4
- package/dist/mime-type.js.map +1 -1
- package/dist/net/domain-name.d.ts +10 -0
- package/dist/net/domain-name.d.ts.map +1 -0
- package/dist/net/domain-name.js +30 -0
- package/dist/net/domain-name.js.map +1 -0
- package/dist/net/ipv4-address.d.ts +10 -0
- package/dist/net/ipv4-address.d.ts.map +1 -0
- package/dist/net/ipv4-address.js +30 -0
- package/dist/net/ipv4-address.js.map +1 -0
- package/dist/net/ipv6-address.d.ts +10 -0
- package/dist/net/ipv6-address.d.ts.map +1 -0
- package/dist/net/ipv6-address.js +30 -0
- package/dist/net/ipv6-address.js.map +1 -0
- package/dist/net/uri-host-name.d.ts +12 -0
- package/dist/net/uri-host-name.d.ts.map +1 -0
- package/dist/net/uri-host-name.js +44 -0
- package/dist/net/uri-host-name.js.map +1 -0
- package/dist/net/uri.d.ts +249 -0
- package/dist/net/uri.d.ts.map +1 -0
- package/dist/net/uri.js +444 -0
- package/dist/net/uri.js.map +1 -0
- package/dist/net/url.d.ts +58 -0
- package/dist/net/url.d.ts.map +1 -0
- package/dist/net/url.js +211 -0
- package/dist/net/url.js.map +1 -0
- package/dist/object/object-path.d.ts +1 -1
- package/dist/object/object-path.d.ts.map +1 -1
- package/dist/object/object-path.js +4 -4
- package/dist/object/object-path.js.map +1 -1
- package/dist/object/type-path.d.ts +1 -1
- package/dist/object/type-path.d.ts.map +1 -1
- package/dist/object/type-path.js +3 -3
- package/dist/object/type-path.js.map +1 -1
- package/dist/object.d.ts +1 -1
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +8 -6
- package/dist/object.js.map +1 -1
- package/dist/property.d.ts +2 -3
- package/dist/property.d.ts.map +1 -1
- package/dist/property.js.map +1 -1
- package/dist/resource-key.d.ts +7 -5
- package/dist/resource-key.d.ts.map +1 -1
- package/dist/resource-key.js +27 -15
- package/dist/resource-key.js.map +1 -1
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +3 -3
- package/dist/result.js.map +1 -1
- package/dist/string.d.ts +10 -21
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +54 -25
- package/dist/string.js.map +1 -1
- package/dist/tag.d.ts +2 -2
- package/dist/tag.d.ts.map +1 -1
- package/dist/tag.js +11 -12
- package/dist/tag.js.map +1 -1
- package/dist/temporal/duration.d.ts +4 -5
- package/dist/temporal/duration.d.ts.map +1 -1
- package/dist/temporal/duration.js +3 -7
- package/dist/temporal/duration.js.map +1 -1
- package/dist/temporal/instant.d.ts +4 -5
- package/dist/temporal/instant.d.ts.map +1 -1
- package/dist/temporal/instant.js +3 -7
- package/dist/temporal/instant.js.map +1 -1
- package/dist/temporal/plain-date-time.d.ts +4 -5
- package/dist/temporal/plain-date-time.d.ts.map +1 -1
- package/dist/temporal/plain-date-time.js +3 -7
- package/dist/temporal/plain-date-time.js.map +1 -1
- package/dist/temporal/plain-date.d.ts +8 -9
- package/dist/temporal/plain-date.d.ts.map +1 -1
- package/dist/temporal/plain-date.js +3 -7
- package/dist/temporal/plain-date.js.map +1 -1
- package/dist/temporal/plain-time.d.ts +6 -7
- package/dist/temporal/plain-time.d.ts.map +1 -1
- package/dist/temporal/plain-time.js +4 -8
- package/dist/temporal/plain-time.js.map +1 -1
- package/dist/temporal/time-zone-id.d.ts +1 -1
- package/dist/temporal/time-zone-id.d.ts.map +1 -1
- package/dist/temporal/time-zone-id.js +4 -4
- package/dist/temporal/time-zone-id.js.map +1 -1
- package/dist/temporal/time-zone-offset.d.ts +1 -1
- package/dist/temporal/time-zone-offset.d.ts.map +1 -1
- package/dist/temporal/time-zone-offset.js +4 -4
- package/dist/temporal/time-zone-offset.js.map +1 -1
- package/dist/temporal/zoned-date-time.d.ts +51 -0
- package/dist/temporal/zoned-date-time.d.ts.map +1 -0
- package/dist/temporal/zoned-date-time.js +106 -0
- package/dist/temporal/zoned-date-time.js.map +1 -0
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/{ulid.d.ts → uuid/ulid.d.ts} +2 -2
- package/dist/uuid/ulid.d.ts.map +1 -0
- package/dist/uuid/ulid.js +29 -0
- package/dist/uuid/ulid.js.map +1 -0
- package/dist/uuid/uuid-v4.d.ts +11 -0
- package/dist/uuid/uuid-v4.d.ts.map +1 -0
- package/dist/{uuid.js → uuid/uuid-v4.js} +11 -11
- package/dist/uuid/uuid-v4.js.map +1 -0
- package/dist/uuid/uuid-v7.d.ts +18 -0
- package/dist/uuid/uuid-v7.d.ts.map +1 -0
- package/dist/uuid/uuid-v7.js +44 -0
- package/dist/uuid/uuid-v7.js.map +1 -0
- package/package.json +1 -1
- package/dist/ulid.d.ts.map +0 -1
- package/dist/ulid.js +0 -29
- package/dist/ulid.js.map +0 -1
- package/dist/uri.d.ts +0 -52
- package/dist/uri.d.ts.map +0 -1
- package/dist/uri.js +0 -222
- package/dist/uri.js.map +0 -1
- package/dist/url.d.ts +0 -26
- package/dist/url.d.ts.map +0 -1
- package/dist/url.js +0 -120
- package/dist/url.js.map +0 -1
- package/dist/uuid.d.ts +0 -11
- package/dist/uuid.d.ts.map +0 -1
- package/dist/uuid.js.map +0 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/error/error-cause.ts
|
|
2
|
+
import Zod from "zod";
|
|
3
|
+
import { BadRequest, Forbidden, InvalidValue, Required, Unauthorized, Unhandled } from "@bessemer/cornerstone/error/error-type";
|
|
4
|
+
import { deepMerge } from "@bessemer/cornerstone/object";
|
|
5
|
+
import * as ErrorCodes from "@bessemer/cornerstone/error/error-code";
|
|
6
|
+
var ValueAttribute = "value";
|
|
7
|
+
var HttpStatusCodeAttribute = "httpStatusCode";
|
|
8
|
+
var BaseErrorCauseSchema = Zod.object({
|
|
9
|
+
code: ErrorCodes.Schema,
|
|
10
|
+
message: Zod.string(),
|
|
11
|
+
attributes: Zod.record(Zod.string(), Zod.unknown())
|
|
12
|
+
});
|
|
13
|
+
var Schema2 = BaseErrorCauseSchema.extend({
|
|
14
|
+
causes: Zod.lazy(() => Zod.array(Schema2))
|
|
15
|
+
});
|
|
16
|
+
var from2 = (builder) => {
|
|
17
|
+
const cause = {
|
|
18
|
+
code: ErrorCodes.from(builder.code),
|
|
19
|
+
message: builder.message,
|
|
20
|
+
attributes: builder.attributes ?? {},
|
|
21
|
+
causes: builder.causes?.map(from2) ?? []
|
|
22
|
+
};
|
|
23
|
+
return cause;
|
|
24
|
+
};
|
|
25
|
+
var unhandled = (builder) => from2({
|
|
26
|
+
code: ErrorCodes.from({ type: Unhandled, namespace: builder?.namespace }),
|
|
27
|
+
...deepMerge(
|
|
28
|
+
{
|
|
29
|
+
message: "An Unhandled Error has occurred.",
|
|
30
|
+
attributes: { [HttpStatusCodeAttribute]: 500 }
|
|
31
|
+
},
|
|
32
|
+
builder
|
|
33
|
+
)
|
|
34
|
+
});
|
|
35
|
+
var required = (builder) => from2({
|
|
36
|
+
code: ErrorCodes.from({ type: Required, namespace: builder?.namespace }),
|
|
37
|
+
...deepMerge(
|
|
38
|
+
{
|
|
39
|
+
message: "The resource is required.",
|
|
40
|
+
attributes: { [HttpStatusCodeAttribute]: 404 }
|
|
41
|
+
},
|
|
42
|
+
builder
|
|
43
|
+
)
|
|
44
|
+
});
|
|
45
|
+
var unauthorized = (builder) => from2({
|
|
46
|
+
code: ErrorCodes.from({ type: Unauthorized, namespace: builder?.namespace }),
|
|
47
|
+
...deepMerge(
|
|
48
|
+
{
|
|
49
|
+
message: "The requested Resource requires authentication.",
|
|
50
|
+
attributes: { [HttpStatusCodeAttribute]: 401 }
|
|
51
|
+
},
|
|
52
|
+
builder
|
|
53
|
+
)
|
|
54
|
+
});
|
|
55
|
+
var forbidden = (builder) => from2({
|
|
56
|
+
code: ErrorCodes.from({ type: Forbidden, namespace: builder?.namespace }),
|
|
57
|
+
...deepMerge(
|
|
58
|
+
{
|
|
59
|
+
message: "The requested Resource requires additional permissions to access.",
|
|
60
|
+
attributes: { [HttpStatusCodeAttribute]: 403 }
|
|
61
|
+
},
|
|
62
|
+
builder
|
|
63
|
+
)
|
|
64
|
+
});
|
|
65
|
+
var badRequest = (builder) => from2({
|
|
66
|
+
code: ErrorCodes.from({ type: BadRequest, namespace: builder?.namespace }),
|
|
67
|
+
...deepMerge(
|
|
68
|
+
{
|
|
69
|
+
message: "The format is invalid and cannot be processed.",
|
|
70
|
+
attributes: { [HttpStatusCodeAttribute]: 400 }
|
|
71
|
+
},
|
|
72
|
+
builder
|
|
73
|
+
)
|
|
74
|
+
});
|
|
75
|
+
var invalidValue = (value, builder) => from2({
|
|
76
|
+
code: ErrorCodes.from({ type: InvalidValue, namespace: builder?.namespace }),
|
|
77
|
+
...deepMerge(
|
|
78
|
+
{
|
|
79
|
+
message: "The format is invalid and cannot be processed.",
|
|
80
|
+
attributes: { [ValueAttribute]: value }
|
|
81
|
+
},
|
|
82
|
+
builder
|
|
83
|
+
)
|
|
84
|
+
});
|
|
85
|
+
export {
|
|
86
|
+
HttpStatusCodeAttribute,
|
|
87
|
+
Schema2 as Schema,
|
|
88
|
+
ValueAttribute,
|
|
89
|
+
badRequest,
|
|
90
|
+
forbidden,
|
|
91
|
+
from2 as from,
|
|
92
|
+
invalidValue,
|
|
93
|
+
required,
|
|
94
|
+
unauthorized,
|
|
95
|
+
unhandled
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=error-cause.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/error/error-cause.ts"],"sourcesContent":["import Zod, { ZodType } from 'zod'\nimport { ResourceNamespace } from '@bessemer/cornerstone/resource-key'\nimport { BadRequest, Forbidden, InvalidValue, Required, Unauthorized, Unhandled } from '@bessemer/cornerstone/error/error-type'\nimport { Dictionary } from '@bessemer/cornerstone/types'\nimport { deepMerge, RecordAttribute } from '@bessemer/cornerstone/object'\nimport * as ErrorCodes from '@bessemer/cornerstone/error/error-code'\nimport { ErrorCode, ErrorCodeLike } from '@bessemer/cornerstone/error/error-code'\n\nexport type ErrorAttribute<Type = unknown> = RecordAttribute<Type, 'ErrorAttribute'>\nexport const ValueAttribute: ErrorAttribute = 'value'\nexport const HttpStatusCodeAttribute: ErrorAttribute<number> = 'httpStatusCode'\n\nconst BaseErrorCauseSchema = Zod.object({\n code: ErrorCodes.Schema,\n message: Zod.string(),\n attributes: Zod.record(Zod.string(), Zod.unknown()),\n})\n\nexport type ErrorCause = {\n code: ErrorCode\n message: string\n attributes: Dictionary<unknown>\n causes: Array<ErrorCause>\n}\n\nexport const Schema: ZodType<ErrorCause> = BaseErrorCauseSchema.extend({\n causes: Zod.lazy(() => Zod.array(Schema)),\n})\n\nexport type ErrorCauseBuilder = {\n code: ErrorCodeLike\n message: string\n attributes?: Dictionary<unknown>\n causes?: Array<ErrorCauseBuilder>\n}\n\nexport type ErrorCauseAugment = {\n namespace?: ResourceNamespace\n message?: string\n attributes?: Dictionary<unknown>\n causes?: Array<ErrorCauseBuilder>\n}\n\nexport const from = (builder: ErrorCauseBuilder): ErrorCause => {\n const cause: ErrorCause = {\n code: ErrorCodes.from(builder.code),\n message: builder.message,\n attributes: builder.attributes ?? {},\n causes: builder.causes?.map(from) ?? [],\n }\n\n return cause\n}\n\nexport const unhandled = (builder?: ErrorCauseAugment): ErrorCause =>\n from({\n code: ErrorCodes.from({ type: Unhandled, namespace: builder?.namespace }),\n ...deepMerge(\n {\n message: 'An Unhandled Error has occurred.',\n attributes: { [HttpStatusCodeAttribute]: 500 },\n },\n builder\n ),\n })\n\nexport const required = (builder?: ErrorCauseAugment): ErrorCause =>\n from({\n code: ErrorCodes.from({ type: Required, namespace: builder?.namespace }),\n ...deepMerge(\n {\n message: 'The resource is required.',\n attributes: { [HttpStatusCodeAttribute]: 404 },\n },\n builder\n ),\n })\n\nexport const unauthorized = (builder?: ErrorCauseAugment): ErrorCause =>\n from({\n code: ErrorCodes.from({ type: Unauthorized, namespace: builder?.namespace }),\n ...deepMerge(\n {\n message: 'The requested Resource requires authentication.',\n attributes: { [HttpStatusCodeAttribute]: 401 },\n },\n builder\n ),\n })\n\nexport const forbidden = (builder?: ErrorCauseAugment): ErrorCause =>\n from({\n code: ErrorCodes.from({ type: Forbidden, namespace: builder?.namespace }),\n ...deepMerge(\n {\n message: 'The requested Resource requires additional permissions to access.',\n attributes: { [HttpStatusCodeAttribute]: 403 },\n },\n builder\n ),\n })\n\nexport const badRequest = (builder?: ErrorCauseAugment): ErrorCause =>\n from({\n code: ErrorCodes.from({ type: BadRequest, namespace: builder?.namespace }),\n ...deepMerge(\n {\n message: 'The format is invalid and cannot be processed.',\n attributes: { [HttpStatusCodeAttribute]: 400 },\n },\n builder\n ),\n })\n\nexport const invalidValue = (value: unknown, builder?: ErrorCauseAugment): ErrorCause =>\n from({\n code: ErrorCodes.from({ type: InvalidValue, namespace: builder?.namespace }),\n ...deepMerge(\n {\n message: 'The format is invalid and cannot be processed.',\n attributes: { [ValueAttribute]: value },\n },\n builder\n ),\n })\n"],"mappings":";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,MAAM,cAAc,WAAW,SAAS,UAAU,CAAC;AAAA,EAC3E,GAAG;AAAA,IACD;AAAA,MACE,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,IAAI;AAAA,IAC/C;AAAA,IACA;AAAA,EACF;AACF,CAAC;AAEI,IAAM,YAAY,CAAC,YACxBA,MAAK;AAAA,EACH,MAAiB,gBAAK,EAAE,MAAM,WAAW,WAAW,SAAS,UAAU,CAAC;AAAA,EACxE,GAAG;AAAA,IACD;AAAA,MACE,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,IAAI;AAAA,IAC/C;AAAA,IACA;AAAA,EACF;AACF,CAAC;AAEI,IAAM,aAAa,CAAC,YACzBA,MAAK;AAAA,EACH,MAAiB,gBAAK,EAAE,MAAM,YAAY,WAAW,SAAS,UAAU,CAAC;AAAA,EACzE,GAAG;AAAA,IACD;AAAA,MACE,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,IAAI;AAAA,IAC/C;AAAA,IACA;AAAA,EACF;AACF,CAAC;AAEI,IAAM,eAAe,CAAC,OAAgB,YAC3CA,MAAK;AAAA,EACH,MAAiB,gBAAK,EAAE,MAAM,cAAc,WAAW,SAAS,UAAU,CAAC;AAAA,EAC3E,GAAG;AAAA,IACD;AAAA,MACE,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,cAAc,GAAG,MAAM;AAAA,IACxC;AAAA,IACA;AAAA,EACF;AACF,CAAC;","names":["Schema","from"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as ResourceKeys from '@bessemer/cornerstone/resource-key';
|
|
2
|
+
import { NamespacedKey, ResourceNamespace } from '@bessemer/cornerstone/resource-key';
|
|
3
|
+
import { ErrorType } from '@bessemer/cornerstone/error/error-type';
|
|
4
|
+
import Zod from 'zod';
|
|
5
|
+
export type ErrorCode = NamespacedKey<ErrorType>;
|
|
6
|
+
export declare const Schema: Zod.ZodPipe<Zod.ZodString, Zod.ZodTransform<ResourceKeys.NamespacedKey<ErrorType>, string>>;
|
|
7
|
+
export type ErrorCodeBuilder = {
|
|
8
|
+
type: ErrorType;
|
|
9
|
+
namespace?: ResourceNamespace;
|
|
10
|
+
};
|
|
11
|
+
export type ErrorCodeLike = ErrorCode | ErrorCodeBuilder;
|
|
12
|
+
export declare const from: (code: ErrorCodeLike) => ErrorCode;
|
|
13
|
+
export declare const extendNamespace: (code: ErrorCode, additionalNamespace: ResourceNamespace) => ErrorCode;
|
|
14
|
+
export declare const destructure: (code: ErrorCode) => [ErrorType, ResourceNamespace];
|
|
15
|
+
export declare const getErrorType: (code: ErrorCode) => ErrorType;
|
|
16
|
+
export declare const getNamespace: (code: ErrorCode) => ResourceNamespace;
|
|
17
|
+
//# sourceMappingURL=error-code.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-code.d.ts","sourceRoot":"","sources":["../../src/error/error-code.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,YAAY,MAAM,oCAAoC,CAAA;AAClE,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAA;AACrF,OAAO,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAA;AAClE,OAAO,GAAG,MAAM,KAAK,CAAA;AAQrB,MAAM,MAAM,SAAS,GAAG,aAAa,CAAC,SAAS,CAAC,CAAA;AAChD,eAAO,MAAM,MAAM,6FAAiE,CAAA;AAEpF,MAAM,MAAM,gBAAgB,GAAG;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,SAAS,CAAC,EAAE,iBAAiB,CAAA;CAAE,CAAA;AACjF,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG,gBAAgB,CAAA;AAExD,eAAO,MAAM,IAAI,GAAI,MAAM,aAAa,KAAG,SAM1C,CAAA;AAED,eAAO,MAAM,eAAe,GAAI,MAAM,SAAS,EAAE,qBAAqB,iBAAiB,KAAG,SAEzF,CAAA;AAED,eAAO,MAAM,WAAW,GAAI,MAAM,SAAS,KAAG,CAAC,SAAS,EAAE,iBAAiB,CAE1E,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,MAAM,SAAS,KAAG,SAE9C,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,MAAM,SAAS,KAAG,iBAE9C,CAAA"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/error/error-code.ts
|
|
2
|
+
import * as ResourceKeys from "@bessemer/cornerstone/resource-key";
|
|
3
|
+
import Zod from "zod";
|
|
4
|
+
import { isObject } from "@bessemer/cornerstone/object";
|
|
5
|
+
var Schema = Zod.string().transform((it) => it);
|
|
6
|
+
var from = (code) => {
|
|
7
|
+
if (isObject(code)) {
|
|
8
|
+
return ResourceKeys.namespaceKey(code.type, code.namespace ?? ResourceKeys.emptyNamespace());
|
|
9
|
+
}
|
|
10
|
+
return code;
|
|
11
|
+
};
|
|
12
|
+
var extendNamespace2 = (code, additionalNamespace) => {
|
|
13
|
+
return ResourceKeys.extendNamespace(code, additionalNamespace);
|
|
14
|
+
};
|
|
15
|
+
var destructure = (code) => {
|
|
16
|
+
return ResourceKeys.destructureKey(code);
|
|
17
|
+
};
|
|
18
|
+
var getErrorType = (code) => {
|
|
19
|
+
return ResourceKeys.getKey(code);
|
|
20
|
+
};
|
|
21
|
+
var getNamespace2 = (code) => {
|
|
22
|
+
return ResourceKeys.getNamespace(code);
|
|
23
|
+
};
|
|
24
|
+
export {
|
|
25
|
+
Schema,
|
|
26
|
+
destructure,
|
|
27
|
+
extendNamespace2 as extendNamespace,
|
|
28
|
+
from,
|
|
29
|
+
getErrorType,
|
|
30
|
+
getNamespace2 as getNamespace
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=error-code.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/error/error-code.ts"],"sourcesContent":["import * as ResourceKeys from '@bessemer/cornerstone/resource-key'\nimport { NamespacedKey, ResourceNamespace } from '@bessemer/cornerstone/resource-key'\nimport { ErrorType } from '@bessemer/cornerstone/error/error-type'\nimport Zod from 'zod'\nimport { isObject } from '@bessemer/cornerstone/object'\n\n/*\n Represents a structured error event. The code can be mapped to a unique type of error while the\n message and attributes can provide contextual information about the error. Finally,\n an ErrorEvent could have multiple causes which get aggregated into a single parent error.\n */\nexport type ErrorCode = NamespacedKey<ErrorType>\nexport const Schema = Zod.string().transform((it) => it as NamespacedKey<ErrorType>)\n\nexport type ErrorCodeBuilder = { type: ErrorType; namespace?: ResourceNamespace }\nexport type ErrorCodeLike = ErrorCode | ErrorCodeBuilder\n\nexport const from = (code: ErrorCodeLike): ErrorCode => {\n if (isObject(code)) {\n return ResourceKeys.namespaceKey(code.type, code.namespace ?? ResourceKeys.emptyNamespace())\n }\n\n return code\n}\n\nexport const extendNamespace = (code: ErrorCode, additionalNamespace: ResourceNamespace): ErrorCode => {\n return ResourceKeys.extendNamespace(code, additionalNamespace)\n}\n\nexport const destructure = (code: ErrorCode): [ErrorType, ResourceNamespace] => {\n return ResourceKeys.destructureKey(code)\n}\n\nexport const getErrorType = (code: ErrorCode): ErrorType => {\n return ResourceKeys.getKey(code)\n}\n\nexport const getNamespace = (code: ErrorCode): ResourceNamespace => {\n return ResourceKeys.getNamespace(code)\n}\n"],"mappings":";AAAA,YAAY,kBAAkB;AAG9B,OAAO,SAAS;AAChB,SAAS,gBAAgB;AAQlB,IAAM,SAAS,IAAI,OAAO,EAAE,UAAU,CAAC,OAAO,EAA8B;AAK5E,IAAM,OAAO,CAAC,SAAmC;AACtD,MAAI,SAAS,IAAI,GAAG;AAClB,WAAoB,0BAAa,KAAK,MAAM,KAAK,aAA0B,4BAAe,CAAC;AAAA,EAC7F;AAEA,SAAO;AACT;AAEO,IAAMA,mBAAkB,CAAC,MAAiB,wBAAsD;AACrG,SAAoB,6BAAgB,MAAM,mBAAmB;AAC/D;AAEO,IAAM,cAAc,CAAC,SAAoD;AAC9E,SAAoB,4BAAe,IAAI;AACzC;AAEO,IAAM,eAAe,CAAC,SAA+B;AAC1D,SAAoB,oBAAO,IAAI;AACjC;AAEO,IAAMC,gBAAe,CAAC,SAAuC;AAClE,SAAoB,0BAAa,IAAI;AACvC;","names":["extendNamespace","getNamespace"]}
|
|
@@ -1,66 +1,46 @@
|
|
|
1
1
|
import { Dictionary, Throwable } from '@bessemer/cornerstone/types';
|
|
2
|
-
import
|
|
3
|
-
import Zod, { ZodType } from 'zod';
|
|
2
|
+
import Zod from 'zod';
|
|
4
3
|
import { LazyValue } from '@bessemer/cornerstone/lazy';
|
|
5
|
-
import { NamespacedKey, ResourceNamespace } from '@bessemer/cornerstone/resource-key';
|
|
6
|
-
import { ErrorType } from '@bessemer/cornerstone/error/error-type';
|
|
7
4
|
import { Result } from '@bessemer/cornerstone/result';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export declare const
|
|
12
|
-
export type ErrorCodeBuilder = {
|
|
13
|
-
code: ErrorCode;
|
|
14
|
-
} | {
|
|
15
|
-
type: ErrorType;
|
|
16
|
-
namespace?: ResourceNamespace;
|
|
17
|
-
};
|
|
18
|
-
export declare const buildErrorCode: (builder: ErrorCodeBuilder) => ErrorCode;
|
|
19
|
-
export type ErrorAttribute<Type = unknown> = RecordAttribute<Type, 'ErrorAttribute'>;
|
|
20
|
-
export declare const ErrorAttributeSchema: ZodType<ErrorAttribute, string>;
|
|
21
|
-
export declare const ErrorEventCauseSchema: Zod.ZodObject<{
|
|
22
|
-
code: Zod.ZodPipe<Zod.ZodString, Zod.ZodTransform<NamespacedKey<ErrorType>, string>>;
|
|
23
|
-
message: Zod.ZodString;
|
|
24
|
-
attributes: Zod.ZodRecord<Zod.ZodString, Zod.ZodUnknown>;
|
|
25
|
-
}, Zod.core.$strip>;
|
|
26
|
-
export type ErrorEventCause = Zod.infer<typeof ErrorEventCauseSchema>;
|
|
5
|
+
import * as ErrorCauses from '@bessemer/cornerstone/error/error-cause';
|
|
6
|
+
import { ErrorCause, ErrorCauseAugment, ErrorCauseBuilder } from '@bessemer/cornerstone/error/error-cause';
|
|
7
|
+
import { MergeExclusive } from 'type-fest';
|
|
8
|
+
export declare const Namespace: import("@bessemer/cornerstone/resource-key").ResourceNamespace<"error-event">;
|
|
27
9
|
export declare const Schema: Zod.ZodObject<{
|
|
10
|
+
_type: import("@bessemer/cornerstone/resource-key").ResourceNamespace<"error-event">;
|
|
28
11
|
message: Zod.ZodString;
|
|
29
|
-
causes: Zod.ZodArray<Zod.
|
|
30
|
-
|
|
31
|
-
message: Zod.ZodString;
|
|
32
|
-
attributes: Zod.ZodRecord<Zod.ZodString, Zod.ZodUnknown>;
|
|
33
|
-
}, Zod.core.$strip>>;
|
|
34
|
-
attributes: Zod.ZodRecord<Zod.ZodType<ErrorAttribute<unknown>, string, Zod.core.$ZodTypeInternals<ErrorAttribute<unknown>, string>>, Zod.ZodUnknown>;
|
|
12
|
+
causes: Zod.ZodArray<Zod.ZodType<ErrorCauses.ErrorCause, unknown, Zod.core.$ZodTypeInternals<ErrorCauses.ErrorCause, unknown>>>;
|
|
13
|
+
context: Zod.ZodRecord<Zod.ZodString, Zod.ZodUnknown>;
|
|
35
14
|
}, Zod.core.$strip>;
|
|
36
|
-
export type ErrorEvent =
|
|
37
|
-
|
|
15
|
+
export type ErrorEvent = {
|
|
16
|
+
_type: typeof Namespace;
|
|
38
17
|
message: string;
|
|
39
|
-
|
|
18
|
+
causes: Array<ErrorCause>;
|
|
19
|
+
context: Dictionary<unknown>;
|
|
40
20
|
};
|
|
41
|
-
|
|
42
|
-
attributes?: Record<ErrorAttribute, unknown>;
|
|
43
|
-
} & ({
|
|
44
|
-
causes: Array<ErrorEventCauseBuilder>;
|
|
45
|
-
message: string;
|
|
46
|
-
} | (ErrorCodeBuilder & {
|
|
21
|
+
type ErrorEventCommonBuilder = {
|
|
47
22
|
message: string;
|
|
48
|
-
|
|
49
|
-
|
|
23
|
+
context?: Dictionary<unknown>;
|
|
24
|
+
};
|
|
25
|
+
type ErrorEventFullBuilder = ErrorEventCommonBuilder & {
|
|
26
|
+
causes: Array<ErrorCauseBuilder>;
|
|
27
|
+
};
|
|
28
|
+
type ErrorEventSingletonBuilder = ErrorEventCommonBuilder & ErrorCauseBuilder;
|
|
29
|
+
export type ErrorEventBuilder = MergeExclusive<ErrorEventFullBuilder, ErrorEventSingletonBuilder>;
|
|
50
30
|
export declare class ErrorEventException extends Error {
|
|
51
31
|
readonly errorEvent: ErrorEvent;
|
|
52
32
|
constructor(errorEvent: ErrorEvent, cause?: unknown);
|
|
53
33
|
}
|
|
54
|
-
export declare const
|
|
55
|
-
export declare const
|
|
56
|
-
export declare const
|
|
57
|
-
export declare const
|
|
34
|
+
export declare const from: (builder: ErrorEventBuilder) => ErrorEvent;
|
|
35
|
+
export declare const fromThrowable: (throwable: Throwable) => ErrorEvent;
|
|
36
|
+
export declare const isErrorEvent: (value: unknown) => value is ErrorEvent;
|
|
37
|
+
export declare const isErrorEventException: (value: unknown) => value is ErrorEventException;
|
|
58
38
|
export declare function withPropagation<ReturnType>(runnable: () => ReturnType, attributes: LazyValue<Dictionary<unknown>>): ReturnType;
|
|
59
39
|
export declare function withPropagation<ReturnType>(runnable: () => Promise<ReturnType>, attributes: LazyValue<Dictionary<unknown>>): Promise<ReturnType>;
|
|
60
|
-
export declare const propagate: (throwable: Throwable,
|
|
61
|
-
export
|
|
62
|
-
|
|
63
|
-
|
|
40
|
+
export declare const propagate: (throwable: Throwable, context: Dictionary<unknown>) => never;
|
|
41
|
+
export type ErrorEventAugment = ErrorCauseAugment & {
|
|
42
|
+
context?: Dictionary<unknown>;
|
|
43
|
+
};
|
|
64
44
|
export declare const unhandled: (builder?: ErrorEventAugment) => ErrorEvent;
|
|
65
45
|
export declare const required: (builder?: ErrorEventAugment) => ErrorEvent;
|
|
66
46
|
export declare const unauthorized: (builder?: ErrorEventAugment) => ErrorEvent;
|
|
@@ -73,4 +53,5 @@ export declare function assertPermitted(value: boolean, builder?: LazyValue<Erro
|
|
|
73
53
|
export declare function assertValid(value: boolean, builder?: LazyValue<ErrorEventAugment | undefined>): asserts value is true;
|
|
74
54
|
export declare function assert(value: boolean, builder: LazyValue<ErrorEventBuilder>): asserts value is true;
|
|
75
55
|
export declare const unpackResult: <T>(result: Result<T, ErrorEvent>) => T;
|
|
56
|
+
export {};
|
|
76
57
|
//# sourceMappingURL=error-event.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error-event.d.ts","sourceRoot":"","sources":["../../src/error/error-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;
|
|
1
|
+
{"version":3,"file":"error-event.d.ts","sourceRoot":"","sources":["../../src/error/error-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAEnE,OAAO,GAAG,MAAM,KAAK,CAAA;AACrB,OAAO,EAAY,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAIhE,OAAO,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAA;AACrD,OAAO,KAAK,WAAW,MAAM,yCAAyC,CAAA;AACtE,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAA;AAE1G,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAG1C,eAAO,MAAM,SAAS,+EAAiC,CAAA;AAEvD,eAAO,MAAM,MAAM;;;;;mBAKjB,CAAA;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,KAAK,EAAE,OAAO,SAAS,CAAA;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,KAAK,CAAC,UAAU,CAAC,CAAA;IACzB,OAAO,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;CAC7B,CAAA;AAED,KAAK,uBAAuB,GAAG;IAC7B,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA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33 +4,16 @@ import Zod from "zod";
|
|
|
4
4
|
import { evaluate } from "@bessemer/cornerstone/lazy";
|
|
5
5
|
import { findInCausalChain as errorsFindInCausalChain, isError } from "@bessemer/cornerstone/error/error";
|
|
6
6
|
import { isPromise } from "@bessemer/cornerstone/promise";
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const [errorType] = splitErrorCode(code);
|
|
15
|
-
return errorType;
|
|
16
|
-
};
|
|
17
|
-
var buildErrorCode = (builder) => {
|
|
18
|
-
if ("code" in builder) {
|
|
19
|
-
return builder.code;
|
|
20
|
-
} else {
|
|
21
|
-
return applyNamespace(builder.type, builder.namespace ?? emptyNamespace());
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
var ErrorAttributeSchema = Zod.string();
|
|
25
|
-
var ErrorEventCauseSchema = Zod.object({
|
|
26
|
-
code: ErrorCodeSchema,
|
|
27
|
-
message: Zod.string(),
|
|
28
|
-
attributes: Zod.record(Zod.string(), Zod.unknown())
|
|
29
|
-
});
|
|
30
|
-
var Schema = Zod.object({
|
|
7
|
+
import { createNamespace } from "@bessemer/cornerstone/resource-key";
|
|
8
|
+
import * as ErrorCauses from "@bessemer/cornerstone/error/error-cause";
|
|
9
|
+
import * as Assertions from "@bessemer/cornerstone/assertion";
|
|
10
|
+
import { isEmpty } from "@bessemer/cornerstone/array";
|
|
11
|
+
var Namespace = createNamespace("error-event");
|
|
12
|
+
var Schema2 = Zod.object({
|
|
13
|
+
_type: Namespace,
|
|
31
14
|
message: Zod.string(),
|
|
32
|
-
causes: Zod.array(
|
|
33
|
-
|
|
15
|
+
causes: Zod.array(ErrorCauses.Schema),
|
|
16
|
+
context: Zod.record(Zod.string(), Zod.unknown())
|
|
34
17
|
});
|
|
35
18
|
var ErrorEventException = class extends Error {
|
|
36
19
|
errorEvent;
|
|
@@ -40,56 +23,49 @@ var ErrorEventException = class extends Error {
|
|
|
40
23
|
this.errorEvent = errorEvent;
|
|
41
24
|
}
|
|
42
25
|
};
|
|
43
|
-
var
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
const errorEvent = throwable;
|
|
48
|
-
return isPresent(errorEvent.message) && isPresent(errorEvent.attributes) && isPresent(errorEvent.causes);
|
|
49
|
-
};
|
|
50
|
-
var isErrorEventException = (throwable) => {
|
|
51
|
-
return throwable instanceof ErrorEventException;
|
|
52
|
-
};
|
|
53
|
-
var of = (builder) => {
|
|
54
|
-
if ("causes" in builder) {
|
|
26
|
+
var from2 = (builder) => {
|
|
27
|
+
if (isPresent(builder.code)) {
|
|
28
|
+
const code = builder.code;
|
|
55
29
|
return {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
code: buildErrorCode(it),
|
|
59
|
-
message: it.message,
|
|
60
|
-
attributes: it.attributes ?? {}
|
|
61
|
-
};
|
|
62
|
-
}),
|
|
30
|
+
_type: Namespace,
|
|
31
|
+
causes: [ErrorCauses.from({ code, ...builder })],
|
|
63
32
|
message: builder.message,
|
|
64
|
-
|
|
33
|
+
context: builder.context ?? {}
|
|
65
34
|
};
|
|
66
35
|
} else {
|
|
36
|
+
Assertions.assertPresent(builder.causes);
|
|
37
|
+
Assertions.assert(!isEmpty(builder.causes), () => "ErrorEvent - Unable to construct ErrorEvent with empty causes array.");
|
|
67
38
|
return {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
code: buildErrorCode(builder),
|
|
71
|
-
message: builder.message,
|
|
72
|
-
attributes: {}
|
|
73
|
-
}
|
|
74
|
-
],
|
|
39
|
+
_type: Namespace,
|
|
40
|
+
causes: builder.causes.map(ErrorCauses.from),
|
|
75
41
|
message: builder.message,
|
|
76
|
-
|
|
42
|
+
context: builder.context ?? {}
|
|
77
43
|
};
|
|
78
44
|
}
|
|
79
45
|
};
|
|
80
|
-
var
|
|
46
|
+
var fromThrowable = (throwable) => {
|
|
81
47
|
if (isErrorEvent(throwable)) {
|
|
82
48
|
return throwable;
|
|
83
49
|
}
|
|
84
50
|
if (!isError(throwable)) {
|
|
85
|
-
return
|
|
51
|
+
return unhandled2();
|
|
86
52
|
}
|
|
87
53
|
const errorEventException = errorsFindInCausalChain(throwable, isErrorEventException);
|
|
88
54
|
if (isNil(errorEventException)) {
|
|
89
|
-
return
|
|
55
|
+
return unhandled2();
|
|
90
56
|
}
|
|
91
57
|
return errorEventException.errorEvent;
|
|
92
58
|
};
|
|
59
|
+
var isErrorEvent = (value) => {
|
|
60
|
+
if (!isObject(value)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const errorEvent = value;
|
|
64
|
+
return errorEvent._type === Namespace;
|
|
65
|
+
};
|
|
66
|
+
var isErrorEventException = (value) => {
|
|
67
|
+
return value instanceof ErrorEventException;
|
|
68
|
+
};
|
|
93
69
|
function withPropagation(runnable, attributes) {
|
|
94
70
|
try {
|
|
95
71
|
let result = runnable();
|
|
@@ -102,103 +78,46 @@ function withPropagation(runnable, attributes) {
|
|
|
102
78
|
throw propagate(throwable, evaluate(attributes));
|
|
103
79
|
}
|
|
104
80
|
}
|
|
105
|
-
var propagate = (throwable,
|
|
81
|
+
var propagate = (throwable, context) => {
|
|
106
82
|
if (isErrorEventException(throwable)) {
|
|
107
83
|
const errorEvent = throwable.errorEvent;
|
|
108
|
-
errorEvent.
|
|
84
|
+
errorEvent.context = { ...errorEvent.context, ...context };
|
|
109
85
|
throw throwable;
|
|
110
86
|
} else {
|
|
111
|
-
const errorEvent =
|
|
112
|
-
const contextualizedEvent =
|
|
87
|
+
const errorEvent = fromThrowable(throwable);
|
|
88
|
+
const contextualizedEvent = from2({ ...errorEvent, context: { ...errorEvent.context, ...context } });
|
|
113
89
|
throw new ErrorEventException(contextualizedEvent, throwable);
|
|
114
90
|
}
|
|
115
91
|
};
|
|
116
|
-
var
|
|
117
|
-
var
|
|
118
|
-
var
|
|
119
|
-
var
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
message: "An Unhandled Error has occurred.",
|
|
124
|
-
attributes: { [HttpStatusCodeAttribute]: 500 }
|
|
125
|
-
},
|
|
126
|
-
builder
|
|
127
|
-
)
|
|
128
|
-
);
|
|
129
|
-
var required = (builder) => of(
|
|
130
|
-
deepMerge(
|
|
131
|
-
{
|
|
132
|
-
type: Required,
|
|
133
|
-
message: "The resource is required.",
|
|
134
|
-
attributes: { [HttpStatusCodeAttribute]: 404 }
|
|
135
|
-
},
|
|
136
|
-
builder
|
|
137
|
-
)
|
|
138
|
-
);
|
|
139
|
-
var unauthorized = (builder) => of(
|
|
140
|
-
deepMerge(
|
|
141
|
-
{
|
|
142
|
-
type: Unauthorized,
|
|
143
|
-
message: "The requested Resource requires authentication.",
|
|
144
|
-
attributes: { [HttpStatusCodeAttribute]: 401 }
|
|
145
|
-
},
|
|
146
|
-
builder
|
|
147
|
-
)
|
|
148
|
-
);
|
|
149
|
-
var forbidden = (builder) => of(
|
|
150
|
-
deepMerge(
|
|
151
|
-
{
|
|
152
|
-
type: Forbidden,
|
|
153
|
-
message: "The requested Resource requires additional permissions to access.",
|
|
154
|
-
attributes: { [HttpStatusCodeAttribute]: 403 }
|
|
155
|
-
},
|
|
156
|
-
builder
|
|
157
|
-
)
|
|
158
|
-
);
|
|
159
|
-
var badRequest = (builder) => of(
|
|
160
|
-
deepMerge(
|
|
161
|
-
{
|
|
162
|
-
type: InvalidValue,
|
|
163
|
-
message: "The format is invalid and cannot be processed.",
|
|
164
|
-
attributes: { [HttpStatusCodeAttribute]: 400 }
|
|
165
|
-
},
|
|
166
|
-
builder
|
|
167
|
-
)
|
|
168
|
-
);
|
|
169
|
-
var invalidValue = (value, builder) => of(
|
|
170
|
-
deepMerge(
|
|
171
|
-
{
|
|
172
|
-
type: InvalidValue,
|
|
173
|
-
message: "The format is invalid and cannot be processed.",
|
|
174
|
-
attributes: { [HttpStatusCodeAttribute]: 400, [ValueAttribute]: value }
|
|
175
|
-
},
|
|
176
|
-
builder
|
|
177
|
-
)
|
|
178
|
-
);
|
|
179
|
-
function assertPresent(value, builder = () => void 0) {
|
|
92
|
+
var unhandled2 = (builder) => from2(deepMerge(ErrorCauses.unhandled(builder), builder));
|
|
93
|
+
var required2 = (builder) => from2(deepMerge(ErrorCauses.required(builder), builder));
|
|
94
|
+
var unauthorized2 = (builder) => from2(deepMerge(ErrorCauses.unauthorized(builder), builder));
|
|
95
|
+
var forbidden2 = (builder) => from2(deepMerge(ErrorCauses.forbidden(builder), builder));
|
|
96
|
+
var badRequest2 = (builder) => from2(deepMerge(ErrorCauses.badRequest(builder), builder));
|
|
97
|
+
var invalidValue2 = (value, builder) => from2(deepMerge(ErrorCauses.invalidValue(value, builder), builder));
|
|
98
|
+
function assertPresent2(value, builder = () => void 0) {
|
|
180
99
|
if (isNil(value)) {
|
|
181
|
-
throw new ErrorEventException(
|
|
100
|
+
throw new ErrorEventException(required2(evaluate(builder)));
|
|
182
101
|
}
|
|
183
102
|
}
|
|
184
103
|
function assertAuthorized(value, builder = () => void 0) {
|
|
185
104
|
if (!value) {
|
|
186
|
-
throw new ErrorEventException(
|
|
105
|
+
throw new ErrorEventException(unauthorized2(evaluate(builder)));
|
|
187
106
|
}
|
|
188
107
|
}
|
|
189
108
|
function assertPermitted(value, builder = () => void 0) {
|
|
190
109
|
if (!value) {
|
|
191
|
-
throw new ErrorEventException(
|
|
110
|
+
throw new ErrorEventException(forbidden2(evaluate(builder)));
|
|
192
111
|
}
|
|
193
112
|
}
|
|
194
113
|
function assertValid(value, builder = () => void 0) {
|
|
195
114
|
if (!value) {
|
|
196
|
-
throw new ErrorEventException(
|
|
115
|
+
throw new ErrorEventException(badRequest2(evaluate(builder)));
|
|
197
116
|
}
|
|
198
117
|
}
|
|
199
|
-
function
|
|
118
|
+
function assert2(value, builder) {
|
|
200
119
|
if (!value) {
|
|
201
|
-
throw new ErrorEventException(
|
|
120
|
+
throw new ErrorEventException(from2(evaluate(builder)));
|
|
202
121
|
}
|
|
203
122
|
}
|
|
204
123
|
var unpackResult = (result) => {
|
|
@@ -208,33 +127,25 @@ var unpackResult = (result) => {
|
|
|
208
127
|
return result.value;
|
|
209
128
|
};
|
|
210
129
|
export {
|
|
211
|
-
ErrorAttributeSchema,
|
|
212
|
-
ErrorCodeSchema,
|
|
213
|
-
ErrorEventCauseSchema,
|
|
214
130
|
ErrorEventException,
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
ValueAttribute,
|
|
219
|
-
assert,
|
|
131
|
+
Namespace,
|
|
132
|
+
Schema2 as Schema,
|
|
133
|
+
assert2 as assert,
|
|
220
134
|
assertAuthorized,
|
|
221
135
|
assertPermitted,
|
|
222
|
-
assertPresent,
|
|
136
|
+
assertPresent2 as assertPresent,
|
|
223
137
|
assertValid,
|
|
224
|
-
badRequest,
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
invalidValue,
|
|
138
|
+
badRequest2 as badRequest,
|
|
139
|
+
forbidden2 as forbidden,
|
|
140
|
+
from2 as from,
|
|
141
|
+
fromThrowable,
|
|
142
|
+
invalidValue2 as invalidValue,
|
|
230
143
|
isErrorEvent,
|
|
231
144
|
isErrorEventException,
|
|
232
|
-
of,
|
|
233
145
|
propagate,
|
|
234
|
-
required,
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
unhandled,
|
|
146
|
+
required2 as required,
|
|
147
|
+
unauthorized2 as unauthorized,
|
|
148
|
+
unhandled2 as unhandled,
|
|
238
149
|
unpackResult,
|
|
239
150
|
withPropagation
|
|
240
151
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/error/error-event.ts"],"sourcesContent":["import { Dictionary, Throwable } from '@bessemer/cornerstone/types'\nimport { deepMerge, isNil, isObject, isPresent, RecordAttribute } from '@bessemer/cornerstone/object'\nimport Zod, { ZodType } from 'zod'\nimport { evaluate, LazyValue } from '@bessemer/cornerstone/lazy'\nimport { findInCausalChain as errorsFindInCausalChain, isError } from '@bessemer/cornerstone/error/error'\nimport { isPromise } from '@bessemer/cornerstone/promise'\nimport { applyNamespace, emptyNamespace, NamespacedKey, ResourceNamespace, splitNamespace } from '@bessemer/cornerstone/resource-key'\nimport { ErrorType, Forbidden, InvalidValue, Required, Unauthorized, Unhandled } from '@bessemer/cornerstone/error/error-type'\nimport { Result } from '@bessemer/cornerstone/result'\n\n/*\n Represents a structured error event. The code can be mapped to a unique type of error while the\n message and attributes can provide contextual information about the error. Finally,\n an ErrorEvent could have multiple causes which get aggregated into a single parent error.\n */\nexport type ErrorCode = NamespacedKey<ErrorType>\nexport const ErrorCodeSchema = Zod.string().transform((it) => it as NamespacedKey<ErrorType>)\n\nexport const splitErrorCode = (code: ErrorCode): [ErrorType, ResourceNamespace] => {\n return splitNamespace(code)\n}\n\nexport const getErrorCodeType = (code: ErrorCode): ErrorType => {\n const [errorType] = splitErrorCode(code)\n return errorType\n}\n\nexport type ErrorCodeBuilder =\n | {\n code: ErrorCode\n }\n | { type: ErrorType; namespace?: ResourceNamespace }\n\nexport const buildErrorCode = (builder: ErrorCodeBuilder): ErrorCode => {\n if ('code' in builder) {\n return builder.code\n } else {\n return applyNamespace(builder.type, builder.namespace ?? emptyNamespace())\n }\n}\n\nexport type ErrorAttribute<Type = unknown> = RecordAttribute<Type, 'ErrorAttribute'>\nexport const ErrorAttributeSchema: ZodType<ErrorAttribute, string> = Zod.string()\n\nexport const ErrorEventCauseSchema = Zod.object({\n code: ErrorCodeSchema,\n message: Zod.string(),\n attributes: Zod.record(Zod.string(), Zod.unknown()),\n})\n\nexport type ErrorEventCause = Zod.infer<typeof ErrorEventCauseSchema>\n\nexport const Schema = Zod.object({\n message: Zod.string(),\n causes: Zod.array(ErrorEventCauseSchema),\n attributes: Zod.record(ErrorAttributeSchema, Zod.unknown()),\n})\n\nexport type ErrorEvent = Zod.infer<typeof Schema>\n\nexport type ErrorEventCauseBuilder = ErrorCodeBuilder & {\n message: string\n attributes?: Dictionary<unknown>\n}\n\nexport type ErrorEventBuilder = {\n attributes?: Record<ErrorAttribute, unknown>\n} & (\n | {\n causes: Array<ErrorEventCauseBuilder>\n message: string\n }\n | (ErrorCodeBuilder & {\n message: string\n })\n)\n\nexport type ErrorEventAugment = Partial<ErrorEventBuilder>\n\n// An exception type that contains an ErrorEvent\nexport class ErrorEventException extends Error {\n readonly errorEvent: ErrorEvent\n\n constructor(errorEvent: ErrorEvent, cause?: unknown) {\n super(errorEvent.message ?? '', { cause })\n this.name = this.constructor.name\n this.errorEvent = errorEvent\n }\n}\n\nexport const isErrorEvent = (throwable: Throwable): throwable is ErrorEvent => {\n if (!isObject(throwable)) {\n return false\n }\n\n const errorEvent = throwable as ErrorEvent\n return isPresent(errorEvent.message) && isPresent(errorEvent.attributes) && isPresent(errorEvent.causes)\n}\n\nexport const isErrorEventException = (throwable: Throwable): throwable is ErrorEventException => {\n return throwable instanceof ErrorEventException\n}\n\nexport const of = (builder: ErrorEventBuilder): ErrorEvent => {\n if ('causes' in builder) {\n return {\n causes: builder.causes.map((it) => {\n return {\n code: buildErrorCode(it),\n message: it.message,\n attributes: it.attributes ?? {},\n }\n }),\n message: builder.message,\n attributes: builder.attributes ?? {},\n }\n } else {\n return {\n causes: [\n {\n code: buildErrorCode(builder),\n message: builder.message,\n attributes: {},\n },\n ],\n message: builder.message,\n attributes: builder.attributes ?? {},\n }\n }\n}\n\nexport const from = (throwable: Throwable): ErrorEvent => {\n if (isErrorEvent(throwable)) {\n return throwable\n }\n\n if (!isError(throwable)) {\n return unhandled()\n }\n\n const errorEventException = errorsFindInCausalChain(throwable, isErrorEventException) as ErrorEventException | undefined\n if (isNil(errorEventException)) {\n return unhandled()\n }\n\n return errorEventException.errorEvent\n}\n\nexport function withPropagation<ReturnType>(runnable: () => ReturnType, attributes: LazyValue<Dictionary<unknown>>): ReturnType\nexport function withPropagation<ReturnType>(runnable: () => Promise<ReturnType>, attributes: LazyValue<Dictionary<unknown>>): Promise<ReturnType>\nexport function withPropagation<ReturnType>(\n runnable: () => ReturnType | Promise<ReturnType>,\n attributes: LazyValue<Dictionary<unknown>>\n): ReturnType | Promise<ReturnType> {\n try {\n let result = runnable()\n if (isPromise(result)) {\n return result.then((it) => it).catch((it) => propagate(it, evaluate(attributes)))\n } else {\n return result\n }\n } catch (throwable: Throwable) {\n throw propagate(throwable, evaluate(attributes))\n }\n}\n\nexport const propagate = (throwable: Throwable, attributes: Dictionary<unknown>): never => {\n if (isErrorEventException(throwable)) {\n // We just mutate the existing error event to avoid nested exceptions\n const errorEvent = throwable.errorEvent\n errorEvent.attributes = { ...errorEvent.attributes, ...attributes }\n throw throwable\n } else {\n const errorEvent = from(throwable)\n const contextualizedEvent = of({ ...errorEvent, attributes: { ...errorEvent.attributes, ...attributes } })\n throw new ErrorEventException(contextualizedEvent, throwable)\n }\n}\n\nexport const ValueAttribute: ErrorAttribute = 'value'\nexport const RequestCorrelationIdAttribute: ErrorAttribute<string> = 'requestCorrelationId'\nexport const HttpStatusCodeAttribute: ErrorAttribute<number> = 'httpStatusCode'\n\nexport const unhandled = (builder?: ErrorEventAugment): ErrorEvent =>\n of(\n deepMerge(\n {\n type: Unhandled,\n message: 'An Unhandled Error has occurred.',\n attributes: { [HttpStatusCodeAttribute]: 500 },\n },\n builder\n )\n )\n\nexport const required = (builder?: ErrorEventAugment): ErrorEvent =>\n of(\n deepMerge(\n {\n type: Required,\n message: 'The resource is required.',\n attributes: { [HttpStatusCodeAttribute]: 404 },\n },\n builder\n )\n )\n\nexport const unauthorized = (builder?: ErrorEventAugment): ErrorEvent =>\n of(\n deepMerge(\n {\n type: Unauthorized,\n message: 'The requested Resource requires authentication.',\n attributes: { [HttpStatusCodeAttribute]: 401 },\n },\n builder\n )\n )\n\nexport const forbidden = (builder?: ErrorEventAugment): ErrorEvent =>\n of(\n deepMerge(\n {\n type: Forbidden,\n message: 'The requested Resource requires additional permissions to access.',\n attributes: { [HttpStatusCodeAttribute]: 403 },\n },\n builder\n )\n )\n\nexport const badRequest = (builder?: ErrorEventAugment): ErrorEvent =>\n of(\n deepMerge(\n {\n type: InvalidValue,\n message: 'The format is invalid and cannot be processed.',\n attributes: { [HttpStatusCodeAttribute]: 400 },\n },\n builder\n )\n )\n\nexport const invalidValue = (value: unknown, builder?: ErrorEventAugment): ErrorEvent =>\n of(\n deepMerge(\n {\n type: InvalidValue,\n message: 'The format is invalid and cannot be processed.',\n attributes: { [HttpStatusCodeAttribute]: 400, [ValueAttribute]: value },\n },\n builder\n )\n )\n\nexport function assertPresent<T>(value: T, builder: LazyValue<ErrorEventAugment | undefined> = () => undefined): asserts value is NonNullable<T> {\n if (isNil(value)) {\n throw new ErrorEventException(required(evaluate(builder)))\n }\n}\n\nexport function assertAuthorized(value: boolean, builder: LazyValue<ErrorEventAugment | undefined> = () => undefined): asserts value is true {\n if (!value) {\n throw new ErrorEventException(unauthorized(evaluate(builder)))\n }\n}\n\nexport function assertPermitted(value: boolean, builder: LazyValue<ErrorEventAugment | undefined> = () => undefined): asserts value is true {\n if (!value) {\n throw new ErrorEventException(forbidden(evaluate(builder)))\n }\n}\n\nexport function assertValid(value: boolean, builder: LazyValue<ErrorEventAugment | undefined> = () => undefined): asserts value is true {\n if (!value) {\n throw new ErrorEventException(badRequest(evaluate(builder)))\n }\n}\n\nexport function assert(value: boolean, builder: LazyValue<ErrorEventBuilder>): asserts value is true {\n if (!value) {\n throw new ErrorEventException(of(evaluate(builder)))\n }\n}\n\nexport const unpackResult = <T>(result: Result<T, ErrorEvent>): T => {\n if (!result.isSuccess) {\n throw new ErrorEventException(result.value)\n }\n\n return result.value\n}\n"],"mappings":";AACA,SAAS,WAAW,OAAO,UAAU,iBAAkC;AACvE,OAAO,SAAsB;AAC7B,SAAS,gBAA2B;AACpC,SAAS,qBAAqB,yBAAyB,eAAe;AACtE,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB,gBAAkD,sBAAsB;AACjG,SAAoB,WAAW,cAAc,UAAU,cAAc,iBAAiB;AAS/E,IAAM,kBAAkB,IAAI,OAAO,EAA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AAAA,IACE;AAAA,MACE,MAAM;AAAA,MACN,SAAS;AAAA,MACT,YAAY,EAAE,CAAC,uBAAuB,GAAG,KAAK,CAAC,cAAc,GAAG,MAAM;AAAA,IACxE;AAAA,IACA;AAAA,EACF;AACF;AAEK,SAAS,cAAiB,OAAU,UAAoD,MAAM,QAA4C;AAC/I,MAAI,MAAM,KAAK,GAAG;AAChB,UAAM,IAAI,oBAAoB,SAAS,SAAS,OAAO,CAAC,CAAC;AAAA,EAC3D;AACF;AAEO,SAAS,iBAAiB,OAAgB,UAAoD,MAAM,QAAkC;AAC3I,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,oBAAoB,aAAa,SAAS,OAAO,CAAC,CAAC;AAAA,EAC/D;AACF;AAEO,SAAS,gBAAgB,OAAgB,UAAoD,MAAM,QAAkC;AAC1I,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,oBAAoB,UAAU,SAAS,OAAO,CAAC,CAAC;AAAA,EAC5D;AACF;AAEO,SAAS,YAAY,OAAgB,UAAoD,MAAM,QAAkC;AACtI,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,oBAAoB,WAAW,SAAS,OAAO,CAAC,CAAC;AAAA,EAC7D;AACF;AAEO,SAAS,OAAO,OAAgB,SAA8D;AACnG,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,oBAAoB,GAAG,SAAS,OAAO,CAAC,CAAC;AAAA,EACrD;AACF;AAEO,IAAM,eAAe,CAAI,WAAqC;AACnE,MAAI,CAAC,OAAO,WAAW;AACrB,UAAM,IAAI,oBAAoB,OAAO,KAAK;AAAA,EAC5C;AAEA,SAAO,OAAO;AAChB;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/error/error-event.ts"],"sourcesContent":["import { Dictionary, Throwable } from '@bessemer/cornerstone/types'\nimport { deepMerge, isNil, isObject, isPresent } from '@bessemer/cornerstone/object'\nimport Zod from 'zod'\nimport { evaluate, LazyValue } from '@bessemer/cornerstone/lazy'\nimport { findInCausalChain as errorsFindInCausalChain, isError } from '@bessemer/cornerstone/error/error'\nimport { isPromise } from '@bessemer/cornerstone/promise'\nimport { createNamespace } from '@bessemer/cornerstone/resource-key'\nimport { Result } from '@bessemer/cornerstone/result'\nimport * as ErrorCauses from '@bessemer/cornerstone/error/error-cause'\nimport { ErrorCause, ErrorCauseAugment, ErrorCauseBuilder } from '@bessemer/cornerstone/error/error-cause'\nimport * as Assertions from '@bessemer/cornerstone/assertion'\nimport { MergeExclusive } from 'type-fest'\nimport { isEmpty } from '@bessemer/cornerstone/array'\n\nexport const Namespace = createNamespace('error-event')\n\nexport const Schema = Zod.object({\n _type: Namespace,\n message: Zod.string(),\n causes: Zod.array(ErrorCauses.Schema),\n context: Zod.record(Zod.string(), Zod.unknown()),\n})\n\nexport type ErrorEvent = {\n _type: typeof Namespace\n message: string\n causes: Array<ErrorCause>\n context: Dictionary<unknown>\n}\n\ntype ErrorEventCommonBuilder = {\n message: string\n context?: Dictionary<unknown>\n}\n\ntype ErrorEventFullBuilder = ErrorEventCommonBuilder & {\n causes: Array<ErrorCauseBuilder>\n}\ntype ErrorEventSingletonBuilder = ErrorEventCommonBuilder & ErrorCauseBuilder\nexport type ErrorEventBuilder = MergeExclusive<ErrorEventFullBuilder, ErrorEventSingletonBuilder>\n\n// An exception type that contains an ErrorEvent\nexport class ErrorEventException extends Error {\n readonly errorEvent: ErrorEvent\n\n constructor(errorEvent: ErrorEvent, cause?: unknown) {\n super(errorEvent.message ?? '', { cause })\n this.name = this.constructor.name\n this.errorEvent = errorEvent\n }\n}\n\nexport const from = (builder: ErrorEventBuilder): ErrorEvent => {\n if (isPresent(builder.code)) {\n const code = builder.code\n return {\n _type: Namespace,\n causes: [ErrorCauses.from({ code, ...builder })],\n message: builder.message,\n context: builder.context ?? {},\n }\n } else {\n Assertions.assertPresent(builder.causes)\n Assertions.assert(!isEmpty(builder.causes), () => 'ErrorEvent - Unable to construct ErrorEvent with empty causes array.')\n\n return {\n _type: Namespace,\n causes: builder.causes.map(ErrorCauses.from),\n message: builder.message,\n context: builder.context ?? {},\n }\n }\n}\n\nexport const fromThrowable = (throwable: Throwable): ErrorEvent => {\n if (isErrorEvent(throwable)) {\n return throwable\n }\n\n if (!isError(throwable)) {\n return unhandled()\n }\n\n const errorEventException = errorsFindInCausalChain(throwable, isErrorEventException) as ErrorEventException | undefined\n if (isNil(errorEventException)) {\n return unhandled()\n }\n\n return errorEventException.errorEvent\n}\n\nexport const isErrorEvent = (value: unknown): value is ErrorEvent => {\n if (!isObject(value)) {\n return false\n }\n\n const errorEvent = value as ErrorEvent\n return errorEvent._type === Namespace\n}\n\nexport const isErrorEventException = (value: unknown): value is ErrorEventException => {\n return value instanceof ErrorEventException\n}\n\nexport function withPropagation<ReturnType>(runnable: () => ReturnType, attributes: LazyValue<Dictionary<unknown>>): ReturnType\nexport function withPropagation<ReturnType>(runnable: () => Promise<ReturnType>, attributes: LazyValue<Dictionary<unknown>>): Promise<ReturnType>\nexport function withPropagation<ReturnType>(\n runnable: () => ReturnType | Promise<ReturnType>,\n attributes: LazyValue<Dictionary<unknown>>\n): ReturnType | Promise<ReturnType> {\n try {\n let result = runnable()\n if (isPromise(result)) {\n return result.then((it) => it).catch((it) => propagate(it, evaluate(attributes)))\n } else {\n return result\n }\n } catch (throwable: Throwable) {\n throw propagate(throwable, evaluate(attributes))\n }\n}\n\nexport const propagate = (throwable: Throwable, context: Dictionary<unknown>): never => {\n if (isErrorEventException(throwable)) {\n // We just mutate the existing error event to avoid nested exceptions\n const errorEvent = throwable.errorEvent\n errorEvent.context = { ...errorEvent.context, ...context }\n throw throwable\n } else {\n const errorEvent = fromThrowable(throwable)\n const contextualizedEvent = from({ ...errorEvent, context: { ...errorEvent.context, ...context } })\n throw new ErrorEventException(contextualizedEvent, throwable)\n }\n}\n\nexport type ErrorEventAugment = ErrorCauseAugment & {\n context?: Dictionary<unknown>\n}\n\nexport const unhandled = (builder?: ErrorEventAugment): ErrorEvent => from(deepMerge(ErrorCauses.unhandled(builder), builder))\n\nexport const required = (builder?: ErrorEventAugment): ErrorEvent => from(deepMerge(ErrorCauses.required(builder), builder))\n\nexport const unauthorized = (builder?: ErrorEventAugment): ErrorEvent => from(deepMerge(ErrorCauses.unauthorized(builder), builder))\n\nexport const forbidden = (builder?: ErrorEventAugment): ErrorEvent => from(deepMerge(ErrorCauses.forbidden(builder), builder))\n\nexport const badRequest = (builder?: ErrorEventAugment): ErrorEvent => from(deepMerge(ErrorCauses.badRequest(builder), builder))\n\nexport const invalidValue = (value: unknown, builder?: ErrorEventAugment): ErrorEvent =>\n from(deepMerge(ErrorCauses.invalidValue(value, builder), builder))\n\nexport function assertPresent<T>(value: T, builder: LazyValue<ErrorEventAugment | undefined> = () => undefined): asserts value is NonNullable<T> {\n if (isNil(value)) {\n throw new ErrorEventException(required(evaluate(builder)))\n }\n}\n\nexport function assertAuthorized(value: boolean, builder: LazyValue<ErrorEventAugment | undefined> = () => undefined): asserts value is true {\n if (!value) {\n throw new ErrorEventException(unauthorized(evaluate(builder)))\n }\n}\n\nexport function assertPermitted(value: boolean, builder: LazyValue<ErrorEventAugment | undefined> = () => undefined): asserts value is true {\n if (!value) {\n throw new ErrorEventException(forbidden(evaluate(builder)))\n }\n}\n\nexport function assertValid(value: boolean, builder: LazyValue<ErrorEventAugment | undefined> = () => undefined): asserts value is true {\n if (!value) {\n throw new ErrorEventException(badRequest(evaluate(builder)))\n }\n}\n\nexport function assert(value: boolean, builder: LazyValue<ErrorEventBuilder>): asserts value is true {\n if (!value) {\n throw new ErrorEventException(from(evaluate(builder)))\n }\n}\n\nexport const unpackResult = <T>(result: Result<T, ErrorEvent>): T => {\n if (!result.isSuccess) {\n throw new ErrorEventException(result.value)\n }\n\n return result.value\n}\n"],"mappings":";AACA,SAAS,WAAW,OAAO,UAAU,iBAAiB;AACtD,OAAO,SAAS;AAChB,SAAS,gBAA2B;AACpC,SAAS,qBAAqB,yBAAyB,eAAe;AACtE,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAEhC,Y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