@adobe/aio-commerce-lib-core 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +106 -113
- package/dist/cjs/error-DkqwTy7o.cjs +225 -0
- package/dist/cjs/index-Bde1DS_G.d.cts +45 -0
- package/dist/cjs/index.cjs +3 -10
- package/dist/cjs/index.d.cts +2 -3
- package/dist/cjs/lib/error/index.cjs +4 -0
- package/dist/cjs/lib/error/index.d.cts +2 -0
- package/dist/es/error-CPCTGRuV.js +180 -0
- package/dist/es/index-C9-P5X0V.d.ts +43 -0
- package/dist/es/index.d.ts +2 -3
- package/dist/es/index.js +2 -3
- package/dist/es/lib/error/index.d.ts +2 -0
- package/dist/es/lib/error/index.js +3 -0
- package/package.json +12 -6
- package/.turbo/turbo-build.log +0 -36
- package/dist/cjs/chunk-CsX-DzYB.cjs +0 -42
- package/dist/cjs/lib/result.cjs +0 -10
- package/dist/cjs/lib/validation.cjs +0 -4
- package/dist/cjs/result-B_qN_g1r.d.cts +0 -26
- package/dist/cjs/result-fxmTDycS.cjs +0 -160
- package/dist/cjs/validation-12iMy71C.cjs +0 -71
- package/dist/cjs/validation-BonM9W9Z.d.cts +0 -21
- package/dist/es/lib/result.d.ts +0 -2
- package/dist/es/lib/result.js +0 -3
- package/dist/es/lib/validation.d.ts +0 -3
- package/dist/es/lib/validation.js +0 -3
- package/dist/es/result-B394X-_m.js +0 -107
- package/dist/es/result-BooxElIJ.d.ts +0 -26
- package/dist/es/validation-B2JQpDTl.d.ts +0 -21
- package/dist/es/validation-DmGRxtJ7.js +0 -54
- package/dist/lib/result.d.cts +0 -2
- package/dist/lib/validation.d.cts +0 -3
- package/source/index.ts +0 -15
- package/source/lib/result.ts +0 -156
- package/source/lib/validation.ts +0 -97
- package/test/result.test.ts +0 -56
- package/test/validation.test.ts +0 -71
- package/tsconfig.json +0 -15
- package/tsdown.config.ts +0 -11
- package/vitest.config.ts +0 -16
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @adobe/aio-commerce-lib-core
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#22](https://github.com/adobe/aio-commerce-sdk/pull/22) [`9885eee`](https://github.com/adobe/aio-commerce-sdk/commit/9885eee5849ba7939b2067d3357e677beced3774) Thanks [@iivvaannxx](https://github.com/iivvaannxx)! - Changes include:
|
|
8
|
+
- Removed the `Result` utilities.
|
|
9
|
+
- Removed the `Validation` utilities.
|
|
10
|
+
- Added `CommerceSdkValidationError` and `CommerceSdkErrorBase` as new error types for better error handling
|
|
11
|
+
|
|
3
12
|
## 0.2.0
|
|
4
13
|
|
|
5
14
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> [!WARNING]
|
|
4
4
|
> This package is a work in progress and is not yet ready for use yet. You may be able to install it, but if you do, expect breaking changes.
|
|
5
5
|
|
|
6
|
-
This package provides core utilities
|
|
6
|
+
This package provides core utilities used across the Adobe Commerce SDK.
|
|
7
7
|
|
|
8
8
|
## Installation
|
|
9
9
|
|
|
@@ -15,140 +15,133 @@ pnpm install @adobe/aio-commerce-lib-core
|
|
|
15
15
|
|
|
16
16
|
This package provides core utilities for the Adobe Commerce SDK libraries.
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
- **[`Result`](./source/lib/result.ts)**: A type-safe way to handle success and failure cases, without relying on exceptions. Inspired by the Rust [`Result` type](https://doc.rust-lang.org/std/result/).
|
|
21
|
-
- **[`Validation`](./source/lib/validation.ts)**: A set of utilities regarding validation, such as creating and pretty-printing validation errors.
|
|
18
|
+
- **Error Handling**: Custom error classes with enhanced debugging capabilities
|
|
19
|
+
- **Validation Errors**: Pretty-printable validation errors.
|
|
22
20
|
|
|
23
21
|
## Usage
|
|
24
22
|
|
|
25
|
-
###
|
|
26
|
-
|
|
27
|
-
Within every AppBuilder action you can use the `Result<T, E>` type to handle success and failure cases. The `Result` type is a union type that can either be a success with a value of type `T` or a failure with an error of type `E` that extends `ErrorType`.
|
|
28
|
-
|
|
29
|
-
#### Signatures
|
|
30
|
-
|
|
31
|
-
- `isOk(result: Result<T, E>): result is { ok: true, value: T }`
|
|
32
|
-
- `isErr(result: Result<T, E>): result is { ok: false, error: E }`
|
|
33
|
-
- `unwrap(result: Result<T, E>): T throws Error`
|
|
34
|
-
- `unwrapErr(result: Result<T, E>): E throws Error`
|
|
35
|
-
- `ok(value: T): Result<T, E>`
|
|
36
|
-
- `err(error: E): Result<T, E>`
|
|
23
|
+
### Error Classes
|
|
37
24
|
|
|
38
|
-
|
|
25
|
+
The library provides custom error classes for consistent error handling across the SDK.
|
|
39
26
|
|
|
40
|
-
|
|
41
|
-
- `unwrap` returns the value if the result is a success, otherwise it throws.
|
|
42
|
-
- `unwrapErr` returns the error if the result is a failure, otherwise it throws.
|
|
43
|
-
- `ok` and `err` are helpers to create success and failure results.
|
|
27
|
+
#### CommerceSdkErrorBase
|
|
44
28
|
|
|
45
|
-
|
|
29
|
+
Base class for all SDK errors with enhanced debugging capabilities:
|
|
46
30
|
|
|
47
|
-
```
|
|
48
|
-
import {
|
|
49
|
-
type Result,
|
|
50
|
-
err,
|
|
51
|
-
isOk,
|
|
52
|
-
ok,
|
|
53
|
-
unwrap,
|
|
54
|
-
} from "@adobe/aio-commerce-lib-core/result";
|
|
31
|
+
```typescript
|
|
32
|
+
import { CommerceSdkErrorBase } from "@adobe/aio-commerce-lib-core/error";
|
|
55
33
|
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return ok(response.data);
|
|
34
|
+
// Create a custom error class
|
|
35
|
+
class MyCustomError extends CommerceSdkErrorBase {
|
|
36
|
+
constructor(message: string, options?: { traceId?: string }) {
|
|
37
|
+
super(message, options || {});
|
|
61
38
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
//
|
|
74
|
-
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Use the error
|
|
42
|
+
const error = new MyCustomError("Something went wrong", {
|
|
43
|
+
traceId: "trace-123",
|
|
44
|
+
cause: originalError,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Check if error is an SDK error
|
|
48
|
+
if (CommerceSdkErrorBase.isSdkError(error)) {
|
|
49
|
+
console.log(error.fullStack); // Full stack trace including causes
|
|
50
|
+
console.log(error.rootCause); // Get the root cause
|
|
51
|
+
console.log(error.toJSON()); // JSON representation
|
|
52
|
+
}
|
|
75
53
|
```
|
|
76
54
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
Here you can use the isErr TypeGuard to check if the result is a failure, then handle the error by returning an appropriate response. Result is now typesafe, and you can access the value properties directly.
|
|
80
|
-
|
|
81
|
-
```ts
|
|
82
|
-
export const main = async function (params: Record<string, unknown>) {
|
|
83
|
-
// ... [PREVIOUS CODE]
|
|
84
|
-
// Use a the isOk TypeGuard to check if the result is a success
|
|
85
|
-
if (isOk(result)) {
|
|
86
|
-
// typesafe access to the value properties
|
|
87
|
-
const product = result.value;
|
|
88
|
-
// product: { id: 'valid', name: 'Sample Product' }
|
|
89
|
-
return {
|
|
90
|
-
status: 200,
|
|
91
|
-
body: product,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
### Result - isErr
|
|
98
|
-
|
|
99
|
-
In another example you can use the isErr TypeGuard to check if the result is a failure, then handle the error by returning an appropriate response. Result is now typesafe, and you can access the error properties directly.
|
|
100
|
-
|
|
101
|
-
```ts
|
|
102
|
-
export const main = async function (params: Record<string, unknown>) {
|
|
103
|
-
// Further down within the same action you can
|
|
104
|
-
// use a the isOk TypeGuard to check if the result is a success
|
|
105
|
-
if (isErr(result)) {
|
|
106
|
-
// typesafe access to the error properties
|
|
107
|
-
const error = result.error;
|
|
108
|
-
// error: {
|
|
109
|
-
// _tag: "ApiError",
|
|
110
|
-
// error: new Error(response.error),
|
|
111
|
-
// statusCode: response.statusCode
|
|
112
|
-
// }
|
|
113
|
-
return {
|
|
114
|
-
status: error.statusCode,
|
|
115
|
-
body: {
|
|
116
|
-
error: `CommerceApiError: ${error.error.message}`,
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
## Validation
|
|
55
|
+
#### CommerceSdkValidationError
|
|
124
56
|
|
|
125
|
-
|
|
57
|
+
Specialized error for validation failures with pretty-printing support:
|
|
126
58
|
|
|
127
|
-
|
|
59
|
+
```typescript
|
|
60
|
+
import { CommerceSdkValidationError } from "@adobe/aio-commerce-lib-core/error";
|
|
61
|
+
import { safeParse } from "valibot";
|
|
128
62
|
|
|
129
|
-
|
|
130
|
-
|
|
63
|
+
// When validation fails (`schema` and `data` are in scope somewhere)
|
|
64
|
+
const result = safeParse(schema, data);
|
|
65
|
+
if (!result.success) {
|
|
66
|
+
const error = new CommerceSdkValidationError("Invalid configuration", {
|
|
67
|
+
issues: result.issues,
|
|
68
|
+
});
|
|
131
69
|
|
|
132
|
-
|
|
70
|
+
// Pretty-print the validation errors
|
|
71
|
+
console.error(error.display());
|
|
133
72
|
|
|
134
|
-
|
|
73
|
+
// Output:
|
|
74
|
+
// Invalid configuration
|
|
75
|
+
// ├── Schema validation error at clientId → Expected a non-empty string value
|
|
76
|
+
// ├── Schema validation error at email → Expected a valid email format
|
|
77
|
+
// └── Input error at age → Expected a number greater than 18
|
|
78
|
+
}
|
|
79
|
+
```
|
|
135
80
|
|
|
136
|
-
|
|
81
|
+
### Working with Validation Errors
|
|
137
82
|
|
|
138
|
-
|
|
139
|
-
import { summarize } from "@adobe/aio-commerce-lib-core/validation";
|
|
83
|
+
The validation error display shows a tree structure of all validation issues:
|
|
140
84
|
|
|
141
|
-
|
|
142
|
-
|
|
85
|
+
```typescript
|
|
86
|
+
try {
|
|
87
|
+
// Some validation logic that throws CommerceSdkValidationError
|
|
88
|
+
validateConfig(params);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error instanceof CommerceSdkValidationError) {
|
|
91
|
+
console.error(error.display()); // with colors (default)
|
|
92
|
+
console.error(error.display(false)); // without colors (for logs)
|
|
143
93
|
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
94
|
+
// Access raw valibot issues
|
|
95
|
+
error.issues.forEach((issue) => {
|
|
96
|
+
console.log(`Issue at ${issue.path}: ${issue.message}`);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
150
100
|
```
|
|
151
101
|
|
|
102
|
+
## Best Practices
|
|
103
|
+
|
|
104
|
+
1. **Extend base error class** for custom errors:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
class ApiError extends CommerceSdkErrorBase {
|
|
108
|
+
constructor(
|
|
109
|
+
message: string,
|
|
110
|
+
public statusCode: number,
|
|
111
|
+
options?: CommerceSdkErrorBaseOptions,
|
|
112
|
+
) {
|
|
113
|
+
super(message, options);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
2. **Use validation errors** for anything related to validation:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { safeParse } from "valibot";
|
|
122
|
+
|
|
123
|
+
function validateParams(params: unknown) {
|
|
124
|
+
const result = safeParse(schema, params);
|
|
125
|
+
if (!result.success) {
|
|
126
|
+
throw new CommerceSdkValidationError("Invalid parameters", {
|
|
127
|
+
issues: result.issues,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return result.output;
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
3. **Chain errors properly** using the `cause` option:
|
|
135
|
+
```typescript
|
|
136
|
+
try {
|
|
137
|
+
await riskyOperation();
|
|
138
|
+
} catch (error) {
|
|
139
|
+
throw new MyCustomError("High-level operation failed", {
|
|
140
|
+
cause: error,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
152
145
|
## Contributing
|
|
153
146
|
|
|
154
147
|
This package is part of the Adobe Commerce SDK monorepo. See the [Contributing Guide](https://github.com/adobe/aio-commerce-sdk/blob/main/.github/CONTRIBUTING.md) and [Development Guide](https://github.com/adobe/aio-commerce-sdk/blob/main/.github/DEVELOPMENT.md) for development setup and guidelines.
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all) __defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
16
|
+
key = keys[i];
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
18
|
+
get: ((k) => from[k]).bind(null, key),
|
|
19
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
25
|
+
value: mod,
|
|
26
|
+
enumerable: true
|
|
27
|
+
}) : target, mod));
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
const node_util = __toESM(require("node:util"));
|
|
31
|
+
const ansis = __toESM(require("ansis"));
|
|
32
|
+
const valibot = __toESM(require("valibot"));
|
|
33
|
+
|
|
34
|
+
//#region source/lib/error/base-error.ts
|
|
35
|
+
/**
|
|
36
|
+
* Base class for all AioCommerceSdk errors.
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* class ValidationError extends CommerceSdkErrorBase {
|
|
40
|
+
* constructor(message: string, options: ValidationErrorOptions) {
|
|
41
|
+
* super(message, options);
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
* const err = new ValidationError("Invalid value", {
|
|
46
|
+
* tag: "ValidationError",
|
|
47
|
+
* field: "name",
|
|
48
|
+
* value: "John Doe",
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* console.log(err.toJSON());
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
var CommerceSdkErrorBase = class CommerceSdkErrorBase extends Error {
|
|
55
|
+
traceId;
|
|
56
|
+
/**
|
|
57
|
+
* Constructs a new CommerceSdkErrorBase instance.
|
|
58
|
+
*
|
|
59
|
+
* @param message - A human-friendly description of the error.
|
|
60
|
+
* @param traceId - An optional trace ID for tracking the error in logs.
|
|
61
|
+
* @param options - Required error options.
|
|
62
|
+
*/
|
|
63
|
+
constructor(message, { traceId,...options }) {
|
|
64
|
+
super(message, options);
|
|
65
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
66
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
67
|
+
this.name = new.target.name || "CommerceSdkError";
|
|
68
|
+
this.traceId = traceId;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Checks if the error is any CommerceSdkErrorBase instance.
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* class ValidationError extends CommerceSdkErrorBase {}
|
|
75
|
+
* const err = new ValidationError("Invalid", {});
|
|
76
|
+
*
|
|
77
|
+
* CommerceSdkErrorBase.isSdkError(err); // true
|
|
78
|
+
* ValidationError.isSdkError(err); // true
|
|
79
|
+
* CommerceSdkErrorBase.isSdkError(new Error("Regular")); // false
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
static isSdkError(error) {
|
|
83
|
+
return error instanceof CommerceSdkErrorBase;
|
|
84
|
+
}
|
|
85
|
+
/** Returns the full stack trace of the error and its causes. */
|
|
86
|
+
get fullStack() {
|
|
87
|
+
let out = this.stack ?? "";
|
|
88
|
+
let cause = this.cause;
|
|
89
|
+
while (cause instanceof Error) {
|
|
90
|
+
out += `\nCaused by: ${cause.stack ?? cause.message}`;
|
|
91
|
+
cause = cause.cause;
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
/** Returns the root cause of the error. */
|
|
96
|
+
get rootCause() {
|
|
97
|
+
let cause = this.cause;
|
|
98
|
+
while (cause) if (typeof cause === "object" && cause !== null && "cause" in cause) cause = cause.cause;
|
|
99
|
+
else break;
|
|
100
|
+
return cause;
|
|
101
|
+
}
|
|
102
|
+
/** Converts the error to a JSON-like representation. */
|
|
103
|
+
toJSON() {
|
|
104
|
+
return {
|
|
105
|
+
name: this.name,
|
|
106
|
+
message: this.message,
|
|
107
|
+
stack: this.fullStack,
|
|
108
|
+
cause: this.cause,
|
|
109
|
+
traceId: this.traceId
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** Returns a pretty string representation of the error. */
|
|
113
|
+
toString(inspect = true) {
|
|
114
|
+
if (inspect) return node_util.default.inspect(this, {
|
|
115
|
+
depth: null,
|
|
116
|
+
colors: true,
|
|
117
|
+
sorted: true,
|
|
118
|
+
maxStringLength: Number.POSITIVE_INFINITY
|
|
119
|
+
});
|
|
120
|
+
return super.toString();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region source/lib/error/validation-error.ts
|
|
126
|
+
const LAST_RETURN_CHAR = "└── ";
|
|
127
|
+
const RETURN_CHAR = "├── ";
|
|
128
|
+
/** Maps issue kinds to their corresponding error titles for display purposes. */
|
|
129
|
+
const ISSUE_KIND_TO_ERROR_TITLE = {
|
|
130
|
+
schema: "Schema validation error",
|
|
131
|
+
transformation: "Transformation error",
|
|
132
|
+
validation: "Input error"
|
|
133
|
+
};
|
|
134
|
+
function getReturnChar(isLastItem, withColor) {
|
|
135
|
+
const char = isLastItem ? LAST_RETURN_CHAR : RETURN_CHAR;
|
|
136
|
+
return withColor ? (0, ansis.cyanBright)(char) : char;
|
|
137
|
+
}
|
|
138
|
+
function getKindText(kind, withColor) {
|
|
139
|
+
const text = ISSUE_KIND_TO_ERROR_TITLE[kind] ?? "Unknown issue kind";
|
|
140
|
+
return withColor ? (0, ansis.yellowBright)(text) : text;
|
|
141
|
+
}
|
|
142
|
+
function getPathText(dotPath, withColor) {
|
|
143
|
+
if (!dotPath) return "";
|
|
144
|
+
return withColor ? `${(0, ansis.cyanBright)(dotPath)}${(0, ansis.whiteBright)((0, ansis.dim)(" →"))}` : `${dotPath} →`;
|
|
145
|
+
}
|
|
146
|
+
function issueToDisplay(issues, withColor = true) {
|
|
147
|
+
const lines = issues?.map((issue, index) => {
|
|
148
|
+
const returnChar = getReturnChar(index === issues.length - 1, withColor);
|
|
149
|
+
const kindText = getKindText(issue.kind, withColor);
|
|
150
|
+
const path = getPathText((0, valibot.getDotPath)(issue), withColor);
|
|
151
|
+
const message = withColor ? (0, ansis.whiteBright)(issue.message) : issue.message;
|
|
152
|
+
const at = withColor ? (0, ansis.whiteBright)("at") : "at";
|
|
153
|
+
const issueLine = `${kindText} ${at} ${path} ${message}`;
|
|
154
|
+
return `${returnChar} ${issueLine}`;
|
|
155
|
+
});
|
|
156
|
+
return lines?.join("\n") ?? "";
|
|
157
|
+
}
|
|
158
|
+
function displayValidationError(error, withColor = true) {
|
|
159
|
+
const display = issueToDisplay(error.issues, withColor);
|
|
160
|
+
const message = withColor ? (0, ansis.whiteBright)(error.message) : error.message;
|
|
161
|
+
return `${message}\n${display}`;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Represents a validation error in the Commerce SDK.
|
|
165
|
+
* This error is thrown when the input does not conform to the expected schema.
|
|
166
|
+
* It contains a list of issues that describe the validation errors.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* const error = new CommerceSdkValidationError("Invalid input", {
|
|
171
|
+
* // `someIssues` is in scope, returned by some `valibot` operation.
|
|
172
|
+
* issues: someIssues
|
|
173
|
+
* });
|
|
174
|
+
*
|
|
175
|
+
* console.log(error.display());
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
var CommerceSdkValidationError = class extends CommerceSdkErrorBase {
|
|
179
|
+
issues;
|
|
180
|
+
/**
|
|
181
|
+
* Constructs a new {@link CommerceSdkValidationError} instance.
|
|
182
|
+
*
|
|
183
|
+
* @param message - A human-friendly description of the validation error.
|
|
184
|
+
* @param options - Options for the validation error, including the issues that caused the error.
|
|
185
|
+
*/
|
|
186
|
+
constructor(message, { issues,...options }) {
|
|
187
|
+
super(message, options);
|
|
188
|
+
this.issues = issues;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Returns a pretty string (and colored) representation of the validation error.
|
|
192
|
+
* @param withColor Whether to use color in the output.
|
|
193
|
+
*/
|
|
194
|
+
display(withColor = true) {
|
|
195
|
+
return displayValidationError(this, withColor);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region source/lib/error/index.ts
|
|
201
|
+
var error_exports = {};
|
|
202
|
+
__export(error_exports, {
|
|
203
|
+
CommerceSdkErrorBase: () => CommerceSdkErrorBase,
|
|
204
|
+
CommerceSdkValidationError: () => CommerceSdkValidationError
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
Object.defineProperty(exports, 'CommerceSdkErrorBase', {
|
|
209
|
+
enumerable: true,
|
|
210
|
+
get: function () {
|
|
211
|
+
return CommerceSdkErrorBase;
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
Object.defineProperty(exports, 'CommerceSdkValidationError', {
|
|
215
|
+
enumerable: true,
|
|
216
|
+
get: function () {
|
|
217
|
+
return CommerceSdkValidationError;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
Object.defineProperty(exports, 'error_exports', {
|
|
221
|
+
enumerable: true,
|
|
222
|
+
get: function () {
|
|
223
|
+
return error_exports;
|
|
224
|
+
}
|
|
225
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { GenericIssue } from "valibot";
|
|
2
|
+
|
|
3
|
+
//#region rolldown:runtime
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region source/lib/error/base-error.d.ts
|
|
6
|
+
type CommerceSdkErrorBaseOptions = ErrorOptions & {
|
|
7
|
+
traceId?: string;
|
|
8
|
+
};
|
|
9
|
+
type CommerceSdkErrorOptions<T extends Record<string, unknown> = Record<string, unknown>> = CommerceSdkErrorBaseOptions & T;
|
|
10
|
+
declare abstract class CommerceSdkErrorBase extends Error {
|
|
11
|
+
readonly traceId?: string;
|
|
12
|
+
constructor(message: string, {
|
|
13
|
+
traceId,
|
|
14
|
+
...options
|
|
15
|
+
}: CommerceSdkErrorBaseOptions);
|
|
16
|
+
static isSdkError(error: unknown): error is CommerceSdkErrorBase;
|
|
17
|
+
get fullStack(): string;
|
|
18
|
+
get rootCause(): unknown;
|
|
19
|
+
toJSON(): {
|
|
20
|
+
name: string;
|
|
21
|
+
message: string;
|
|
22
|
+
stack: string;
|
|
23
|
+
cause: unknown;
|
|
24
|
+
traceId: string | undefined;
|
|
25
|
+
};
|
|
26
|
+
toString(inspect?: boolean): string;
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region source/lib/error/validation-error.d.ts
|
|
30
|
+
type CommerceSdkValidationErrorOptions = CommerceSdkErrorOptions<{
|
|
31
|
+
issues: GenericIssue[];
|
|
32
|
+
}>;
|
|
33
|
+
declare class CommerceSdkValidationError extends CommerceSdkErrorBase {
|
|
34
|
+
readonly issues: GenericIssue[];
|
|
35
|
+
constructor(message: string, {
|
|
36
|
+
issues,
|
|
37
|
+
...options
|
|
38
|
+
}: CommerceSdkValidationErrorOptions);
|
|
39
|
+
display(withColor?: boolean): string;
|
|
40
|
+
}
|
|
41
|
+
declare namespace index_d_exports {
|
|
42
|
+
export { CommerceSdkErrorBase, CommerceSdkValidationError };
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { CommerceSdkErrorBase, CommerceSdkValidationError, index_d_exports };
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
const
|
|
2
|
-
const require_validation = require('./validation-12iMy71C.cjs');
|
|
1
|
+
const require_error = require('./error-DkqwTy7o.cjs');
|
|
3
2
|
|
|
4
|
-
Object.defineProperty(exports, '
|
|
3
|
+
Object.defineProperty(exports, 'Error', {
|
|
5
4
|
enumerable: true,
|
|
6
5
|
get: function () {
|
|
7
|
-
return
|
|
8
|
-
}
|
|
9
|
-
});
|
|
10
|
-
Object.defineProperty(exports, 'Validation', {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
get: function () {
|
|
13
|
-
return require_validation.validation_exports;
|
|
6
|
+
return require_error.error_exports;
|
|
14
7
|
}
|
|
15
8
|
});
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export { result_d_exports as Result, validation_d_exports as Validation };
|
|
1
|
+
import { index_d_exports } from "./index-Bde1DS_G.cjs";
|
|
2
|
+
export { index_d_exports as Error };
|