@clipboard-health/util-ts 2.9.1 → 2.9.3
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/README.md
CHANGED
|
@@ -33,6 +33,7 @@ See `./src/lib` for each utility.
|
|
|
33
33
|
import { deepEqual, equal } from "node:assert/strict";
|
|
34
34
|
|
|
35
35
|
import { ERROR_CODES, ServiceError } from "@clipboard-health/util-ts";
|
|
36
|
+
import { z } from "zod";
|
|
36
37
|
|
|
37
38
|
{
|
|
38
39
|
const error = new ServiceError("boom");
|
|
@@ -40,14 +41,20 @@ import { ERROR_CODES, ServiceError } from "@clipboard-health/util-ts";
|
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
{
|
|
43
|
-
const
|
|
44
|
-
const serviceError = ServiceError.fromError(error);
|
|
44
|
+
const serviceError = ServiceError.fromUnknown("boom");
|
|
45
45
|
equal(
|
|
46
46
|
serviceError.toString(),
|
|
47
47
|
`ServiceError[${serviceError.id}]: [internal]: boom; [cause]: Error: boom`,
|
|
48
48
|
);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
{
|
|
52
|
+
const serviceError = ServiceError.fromZodError(
|
|
53
|
+
new z.ZodError([{ code: "custom", path: ["foo"], message: "boom" }]),
|
|
54
|
+
);
|
|
55
|
+
equal(serviceError.toString(), `ServiceError[${serviceError.id}]: [unprocessableEntity]: boom`);
|
|
56
|
+
}
|
|
57
|
+
|
|
51
58
|
{
|
|
52
59
|
const errorWithCause = new ServiceError({
|
|
53
60
|
issues: [{ message: "boom" }],
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/util-ts",
|
|
3
3
|
"description": "TypeScript utilities.",
|
|
4
|
-
"version": "2.9.
|
|
4
|
+
"version": "2.9.3",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "2.8.0"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"type-fest": "4.26.1"
|
|
9
|
+
"type-fest": "4.26.1",
|
|
10
|
+
"zod": "3.23.8"
|
|
10
11
|
},
|
|
11
12
|
"keywords": [],
|
|
12
13
|
"license": "MIT",
|
|
@@ -21,10 +21,15 @@ export interface ServiceIssue {
|
|
|
21
21
|
path?: Array<string | number>;
|
|
22
22
|
}
|
|
23
23
|
export type ServiceErrorParams = string | {
|
|
24
|
+
/** Error cause */
|
|
24
25
|
cause?: Readonly<unknown>;
|
|
26
|
+
/** Unique identifier for the issue */
|
|
27
|
+
id?: string;
|
|
28
|
+
/** Array of issues contributing to the error */
|
|
25
29
|
issues: readonly ServiceIssue[];
|
|
26
30
|
};
|
|
27
31
|
export interface ZodLike {
|
|
32
|
+
name: string;
|
|
28
33
|
issues: ReadonlyArray<{
|
|
29
34
|
message?: string | undefined;
|
|
30
35
|
path: Array<string | number>;
|
|
@@ -39,21 +44,20 @@ interface Issue extends ServiceIssue {
|
|
|
39
44
|
*/
|
|
40
45
|
export declare class ServiceError extends Error {
|
|
41
46
|
/**
|
|
42
|
-
* Converts an
|
|
47
|
+
* Converts an unknown value to a `ServiceError`.
|
|
43
48
|
*
|
|
44
49
|
* @param value - The value to convert, which can be any type
|
|
45
50
|
* @returns If the value is already a `ServiceError`, returns it unchanged. Otherwise, convert it
|
|
46
51
|
* to a `ServiceError`.
|
|
47
52
|
*/
|
|
48
|
-
static
|
|
53
|
+
static fromUnknown(value: unknown): ServiceError;
|
|
49
54
|
/**
|
|
50
|
-
* Converts a
|
|
55
|
+
* Converts a ZodError to a `ServiceError`.
|
|
51
56
|
*
|
|
52
|
-
* @param value -
|
|
57
|
+
* @param value - A ZodError
|
|
53
58
|
* @returns The converted `ServiceError`
|
|
54
|
-
* @throws {ServiceError} If the provided value does not match the expected Zod-like structure
|
|
55
59
|
*/
|
|
56
|
-
static
|
|
60
|
+
static fromZodError(value: ZodLike): ServiceError;
|
|
57
61
|
readonly id: string;
|
|
58
62
|
readonly issues: readonly Issue[];
|
|
59
63
|
/**
|
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ServiceError = exports.ERROR_CODES = void 0;
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
const deepFreeze_1 = require("../deepFreeze");
|
|
6
|
-
const isDefined_1 = require("../nullish/isDefined");
|
|
7
6
|
const toError_1 = require("./toError");
|
|
8
7
|
/**
|
|
9
8
|
* Standard error codes used across microservices.
|
|
@@ -52,28 +51,18 @@ const ERROR_METADATA = {
|
|
|
52
51
|
title: "Internal server error",
|
|
53
52
|
},
|
|
54
53
|
};
|
|
55
|
-
function isZodLike(value) {
|
|
56
|
-
const zodLike = value;
|
|
57
|
-
return ((0, isDefined_1.isDefined)(zodLike) &&
|
|
58
|
-
"issues" in zodLike &&
|
|
59
|
-
Array.isArray(zodLike.issues) &&
|
|
60
|
-
zodLike.issues.every((issue) => typeof issue === "object" &&
|
|
61
|
-
(0, isDefined_1.isDefined)(issue) &&
|
|
62
|
-
"path" in issue &&
|
|
63
|
-
Array.isArray(issue.path)));
|
|
64
|
-
}
|
|
65
54
|
/**
|
|
66
55
|
* Error class for service-level errors, convertible to JSON:API errors.
|
|
67
56
|
*/
|
|
68
57
|
class ServiceError extends Error {
|
|
69
58
|
/**
|
|
70
|
-
* Converts an
|
|
59
|
+
* Converts an unknown value to a `ServiceError`.
|
|
71
60
|
*
|
|
72
61
|
* @param value - The value to convert, which can be any type
|
|
73
62
|
* @returns If the value is already a `ServiceError`, returns it unchanged. Otherwise, convert it
|
|
74
63
|
* to a `ServiceError`.
|
|
75
64
|
*/
|
|
76
|
-
static
|
|
65
|
+
static fromUnknown(value) {
|
|
77
66
|
if (value instanceof ServiceError) {
|
|
78
67
|
return value;
|
|
79
68
|
}
|
|
@@ -84,26 +73,18 @@ class ServiceError extends Error {
|
|
|
84
73
|
});
|
|
85
74
|
}
|
|
86
75
|
/**
|
|
87
|
-
* Converts a
|
|
76
|
+
* Converts a ZodError to a `ServiceError`.
|
|
88
77
|
*
|
|
89
|
-
* @param value -
|
|
78
|
+
* @param value - A ZodError
|
|
90
79
|
* @returns The converted `ServiceError`
|
|
91
|
-
* @throws {ServiceError} If the provided value does not match the expected Zod-like structure
|
|
92
80
|
*/
|
|
93
|
-
static
|
|
94
|
-
if (!isZodLike(value)) {
|
|
95
|
-
throw new ServiceError({
|
|
96
|
-
issues: [{ code: exports.ERROR_CODES.internal, message: "Invalid ZodLike" }],
|
|
97
|
-
cause: value,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
81
|
+
static fromZodError(value) {
|
|
100
82
|
return new ServiceError({
|
|
101
83
|
issues: value.issues.map((issue) => ({
|
|
102
84
|
code: exports.ERROR_CODES.unprocessableEntity,
|
|
103
85
|
message: issue.message,
|
|
104
86
|
path: issue.path,
|
|
105
87
|
})),
|
|
106
|
-
cause: value,
|
|
107
88
|
});
|
|
108
89
|
}
|
|
109
90
|
id;
|
|
@@ -114,12 +95,12 @@ class ServiceError extends Error {
|
|
|
114
95
|
*/
|
|
115
96
|
constructor(parameters) {
|
|
116
97
|
const params = typeof parameters === "string"
|
|
117
|
-
? { issues: [toIssue({ message: parameters })] }
|
|
98
|
+
? { cause: undefined, id: undefined, issues: [toIssue({ message: parameters })] }
|
|
118
99
|
: { ...parameters, issues: parameters.issues.map(toIssue) };
|
|
119
100
|
super(createServiceErrorMessage(params.issues));
|
|
120
|
-
this.
|
|
101
|
+
this.cause = params.cause;
|
|
102
|
+
this.id = params.id ?? (0, node_crypto_1.randomUUID)();
|
|
121
103
|
this.issues = (0, deepFreeze_1.deepFreeze)(params.issues);
|
|
122
|
-
this.cause = "cause" in params ? params.cause : undefined;
|
|
123
104
|
this.name = this.constructor.name;
|
|
124
105
|
/**
|
|
125
106
|
* Maintain proper prototype chain in transpiled code
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/serviceError.ts"],"names":[],"mappings":";;;AAAA,6CAAyC;AAEzC,8CAA2C;AAC3C,
|
|
1
|
+
{"version":3,"file":"serviceError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/serviceError.ts"],"names":[],"mappings":";;;AAAA,6CAAyC;AAEzC,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,GAAyD;IAC3E,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,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,2BAA2B;KACnC;IACD,eAAe,EAAE;QACf,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,wBAAwB;KAChC;IACD,QAAQ,EAAE;QACR,MAAM,EAAE,GAAG;QACX,KAAK,EAAE,uBAAuB;KAC/B;CACF,CAAC;AAOF;;GAEG;AACH,MAAa,YAAa,SAAQ,KAAK;IACrC;;;;;;OAMG;IACH,MAAM,CAAC,WAAW,CAAC,KAAc;QAC/B,IAAI,KAAK,YAAY,YAAY,EAAE,CAAC;YAClC,OAAO,KAA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