@clipboard-health/util-ts 2.11.0 → 2.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -21,6 +21,7 @@ export interface ServiceIssue {
|
|
|
21
21
|
/** Path to issue location */
|
|
22
22
|
path?: Array<string | number>;
|
|
23
23
|
}
|
|
24
|
+
type Source = "header" | "parameter" | "pointer";
|
|
24
25
|
export type ServiceErrorParams = string | {
|
|
25
26
|
/** Error cause */
|
|
26
27
|
cause?: Readonly<unknown>;
|
|
@@ -28,6 +29,12 @@ export type ServiceErrorParams = string | {
|
|
|
28
29
|
id?: string;
|
|
29
30
|
/** Array of issues contributing to the error */
|
|
30
31
|
issues: readonly ServiceIssue[];
|
|
32
|
+
/**
|
|
33
|
+
* Source of the error
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://jsonapi.org/format/#error-objects}
|
|
36
|
+
*/
|
|
37
|
+
source?: Source | undefined;
|
|
31
38
|
};
|
|
32
39
|
declare const ERROR_METADATA: {
|
|
33
40
|
readonly badRequest: {
|
|
@@ -83,13 +90,16 @@ export declare class ServiceError extends Error {
|
|
|
83
90
|
/**
|
|
84
91
|
* Converts a ZodError to a `ServiceError`.
|
|
85
92
|
*
|
|
86
|
-
* @param
|
|
93
|
+
* @param error - A ZodError
|
|
87
94
|
* @returns The converted `ServiceError`
|
|
88
95
|
*/
|
|
89
|
-
static fromZodError(
|
|
96
|
+
static fromZodError(error: ZodError, options?: {
|
|
97
|
+
source?: Source;
|
|
98
|
+
}): ServiceError;
|
|
90
99
|
readonly id: string;
|
|
91
100
|
readonly issues: readonly Issue[];
|
|
92
101
|
readonly status: Status;
|
|
102
|
+
readonly source: Source;
|
|
93
103
|
/**
|
|
94
104
|
* Creates a new `ServiceError`
|
|
95
105
|
* @param parameters - Issues contributing to the error or a message string
|
|
@@ -107,7 +117,7 @@ export declare class ServiceError extends Error {
|
|
|
107
117
|
toJsonApi(): {
|
|
108
118
|
errors: {
|
|
109
119
|
source?: {
|
|
110
|
-
|
|
120
|
+
[x: string]: string;
|
|
111
121
|
};
|
|
112
122
|
detail?: string;
|
|
113
123
|
id: string;
|
|
@@ -76,32 +76,40 @@ class ServiceError extends Error {
|
|
|
76
76
|
/**
|
|
77
77
|
* Converts a ZodError to a `ServiceError`.
|
|
78
78
|
*
|
|
79
|
-
* @param
|
|
79
|
+
* @param error - A ZodError
|
|
80
80
|
* @returns The converted `ServiceError`
|
|
81
81
|
*/
|
|
82
|
-
static fromZodError(
|
|
82
|
+
static fromZodError(error, options) {
|
|
83
83
|
return new ServiceError({
|
|
84
|
-
cause:
|
|
85
|
-
issues:
|
|
84
|
+
cause: error,
|
|
85
|
+
issues: error.issues.map(toZodIssue),
|
|
86
|
+
source: options?.source,
|
|
86
87
|
});
|
|
87
88
|
}
|
|
88
89
|
id;
|
|
89
90
|
issues;
|
|
90
91
|
status;
|
|
92
|
+
source;
|
|
91
93
|
/**
|
|
92
94
|
* Creates a new `ServiceError`
|
|
93
95
|
* @param parameters - Issues contributing to the error or a message string
|
|
94
96
|
*/
|
|
95
97
|
constructor(parameters) {
|
|
96
98
|
const params = typeof parameters === "string"
|
|
97
|
-
? {
|
|
99
|
+
? {
|
|
100
|
+
cause: undefined,
|
|
101
|
+
id: undefined,
|
|
102
|
+
issues: [toIssue({ message: parameters })],
|
|
103
|
+
source: undefined,
|
|
104
|
+
}
|
|
98
105
|
: { ...parameters, issues: parameters.issues.map(toIssue) };
|
|
99
106
|
super(createServiceErrorMessage(params.issues));
|
|
100
|
-
const { cause, id, issues } = params;
|
|
107
|
+
const { cause, id, issues, source } = params;
|
|
101
108
|
this.cause = cause;
|
|
102
109
|
this.id = id ?? (0, node_crypto_1.randomUUID)();
|
|
103
110
|
this.issues = (0, deepFreeze_1.deepFreeze)(issues);
|
|
104
111
|
this.name = this.constructor.name;
|
|
112
|
+
this.source = source ?? "pointer";
|
|
105
113
|
this.status = Math.max(...issues.map((issue) => ERROR_METADATA[issue.code].status));
|
|
106
114
|
/**
|
|
107
115
|
* Maintain proper prototype chain in transpiled code
|
|
@@ -130,7 +138,7 @@ class ServiceError extends Error {
|
|
|
130
138
|
...(issue.message && { detail: issue.message }),
|
|
131
139
|
...(issue.path && {
|
|
132
140
|
source: {
|
|
133
|
-
|
|
141
|
+
[this.source]: `/${issue.path.join("/")}`,
|
|
134
142
|
},
|
|
135
143
|
}),
|
|
136
144
|
})),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/serviceError.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"serviceError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/serviceError.ts"],"names":[],"mappings":";;;AAsMA,gCAMC;AA5MD,6CAAyC;AAIzC,8CAA2C;AAC3C,uCAAoC;AAEpC;;GAEG;AACU,QAAA,WAAW,GAAG;IACzB,UAAU,EAAE,YAAY;IACxB,YAAY,EAAE,cAAc;IAC5B,SAAS,EAAE,WAAW;IACtB,QAAQ,EAAE,UAAU;IACpB,QAAQ,EAAE,UAAU;IACpB,mBAAmB,EAAE,qBAAqB;IAC1C,eAAe,EAAE,iBAAiB;IAClC,QAAQ,EAAE,UAAU;CACZ,CAAC;AAmCX,MAAM,cAAc,GAAG;IACrB,UAAU,EAAE;QACV,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,8BAA8B;KACtC;IACD,YAAY,EAAE;QACZ,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,gCAAgC;KACxC;IACD,SAAS,EAAE;QACT,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,2BAA2B;KACnC;IACD,QAAQ,EAAE;QACR,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,oBAAoB;KAC5B;IACD,QAAQ,EAAE;QACR,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,4BAA4B;KACpC;IACD,mBAAmB,EAAE;QACnB,MAA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