@aboutcircles/sdk-utils 0.1.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/dist/errors.js ADDED
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Circles SDK Error Handling
3
+ * Provides a structured error system for consistent error handling across the SDK
4
+ */
5
+ /**
6
+ * Base Circles SDK Error
7
+ * All SDK errors extend from this class
8
+ */
9
+ export class CirclesError extends Error {
10
+ name;
11
+ code;
12
+ source;
13
+ cause;
14
+ context;
15
+ constructor(name, message, options) {
16
+ super(message);
17
+ this.name = name;
18
+ this.code = options?.code;
19
+ this.source = (options?.source ?? 'UNKNOWN');
20
+ this.cause = options?.cause;
21
+ this.context = options?.context;
22
+ // Maintain proper stack trace
23
+ if (Error.captureStackTrace) {
24
+ Error.captureStackTrace(this, this.constructor);
25
+ }
26
+ }
27
+ /**
28
+ * Convert error to JSON for logging/debugging
29
+ */
30
+ toJSON() {
31
+ return {
32
+ name: this.name,
33
+ message: this.message,
34
+ code: this.code,
35
+ source: this.source,
36
+ context: this.context,
37
+ cause: this.cause instanceof Error ? {
38
+ name: this.cause.name,
39
+ message: this.cause.message,
40
+ } : this.cause,
41
+ stack: this.stack,
42
+ };
43
+ }
44
+ /**
45
+ * Get formatted error message with context
46
+ */
47
+ toString() {
48
+ let result = `[${this.source}] ${this.name}: ${this.message}`;
49
+ if (this.code) {
50
+ result += ` (Code: ${this.code})`;
51
+ }
52
+ if (this.context) {
53
+ result += `\nContext: ${JSON.stringify(this.context, null, 2)}`;
54
+ }
55
+ return result;
56
+ }
57
+ }
58
+ /**
59
+ * Validation errors
60
+ */
61
+ export class ValidationError extends CirclesError {
62
+ constructor(message, options) {
63
+ super('ValidationError', message, { ...options, source: 'VALIDATION' });
64
+ }
65
+ /**
66
+ * Create error for invalid address
67
+ */
68
+ static invalidAddress(address) {
69
+ return new ValidationError('Invalid Ethereum address', {
70
+ code: 'VALIDATION_INVALID_ADDRESS',
71
+ context: { address },
72
+ });
73
+ }
74
+ /**
75
+ * Create error for invalid amount
76
+ */
77
+ static invalidAmount(amount, reason) {
78
+ return new ValidationError(reason || 'Invalid amount', {
79
+ code: 'VALIDATION_INVALID_AMOUNT',
80
+ context: { amount, reason },
81
+ });
82
+ }
83
+ /**
84
+ * Create error for missing parameter
85
+ */
86
+ static missingParameter(paramName) {
87
+ return new ValidationError(`Missing required parameter: ${paramName}`, {
88
+ code: 'VALIDATION_MISSING_PARAM',
89
+ context: { paramName },
90
+ });
91
+ }
92
+ /**
93
+ * Create error for invalid parameter
94
+ */
95
+ static invalidParameter(paramName, value, reason) {
96
+ return new ValidationError(`Invalid parameter '${paramName}': ${reason || 'value is invalid'}`, {
97
+ code: 'VALIDATION_INVALID_PARAM',
98
+ context: { paramName, value, reason },
99
+ });
100
+ }
101
+ }
102
+ /**
103
+ * Encoding/Conversion errors for utils package
104
+ */
105
+ export class EncodingError extends CirclesError {
106
+ constructor(message, options) {
107
+ super('EncodingError', message, { ...options, source: 'ENCODING' });
108
+ }
109
+ /**
110
+ * Create error for ABI encoding failures
111
+ */
112
+ static abiEncoding(functionName, cause) {
113
+ return new EncodingError('Failed to encode ABI data', {
114
+ code: 'ENCODING_ABI_FAILED',
115
+ cause,
116
+ context: { functionName },
117
+ });
118
+ }
119
+ /**
120
+ * Create error for CID conversion failures
121
+ */
122
+ static cidConversion(cid, cause) {
123
+ return new EncodingError('Failed to convert CID', {
124
+ code: 'ENCODING_CID_FAILED',
125
+ cause,
126
+ context: { cid },
127
+ });
128
+ }
129
+ }
130
+ /**
131
+ * Helper to wrap unknown errors into CirclesError
132
+ */
133
+ export function wrapError(error, source = 'UNKNOWN') {
134
+ if (error instanceof CirclesError) {
135
+ return error;
136
+ }
137
+ if (error instanceof Error) {
138
+ return new CirclesError(error.name || 'UnknownError', error.message, {
139
+ source,
140
+ cause: error,
141
+ });
142
+ }
143
+ return new CirclesError('UnknownError', String(error), {
144
+ source,
145
+ cause: error,
146
+ });
147
+ }
148
+ /**
149
+ * Type guard to check if error is a CirclesError
150
+ */
151
+ export function isCirclesError(error) {
152
+ return error instanceof CirclesError;
153
+ }
154
+ /**
155
+ * Helper to extract error message safely
156
+ */
157
+ export function getErrorMessage(error) {
158
+ if (isCirclesError(error)) {
159
+ return error.message;
160
+ }
161
+ if (error instanceof Error) {
162
+ return error.message;
163
+ }
164
+ return String(error);
165
+ }
@@ -0,0 +1,10 @@
1
+ export { CirclesConverter } from './circlesConverter';
2
+ export { bytesToHex } from './bytes';
3
+ export { encodeFunctionData, decodeFunctionResult, decodeErrorResult, checksumAddress } from './abi';
4
+ export { cidV0ToHex, cidV0ToUint8Array } from './cid';
5
+ export { uint256ToAddress } from './address';
6
+ export { ZERO_ADDRESS } from './constants';
7
+ export { parseContractError, ContractError } from './contractErrors';
8
+ export { CirclesError, ValidationError, EncodingError, wrapError, isCirclesError, getErrorMessage, } from './errors';
9
+ export type { BaseErrorSource, UtilsErrorSource } from './errors';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AACrG,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGrE,OAAO,EACL,YAAY,EACZ,eAAe,EACf,aAAa,EACb,SAAS,EACT,cAAc,EACd,eAAe,GAChB,MAAM,UAAU,CAAC;AAClB,YAAY,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC"}