@dima_aryze/reforge
Version:
TypeScript/JavaScript SDK for Reforge - Cross-chain token operations
375 lines • 11 kB
JavaScript
/**
* Enhanced error classes for the Reforge SDK
*/
/**
* Base Reforge SDK error class with enhanced context
*/
export class ReforgeError extends Error {
constructor(message,
// eslint-disable-next-line no-unused-vars
cause, context = {}) {
super(message);
Object.defineProperty(this, "cause", {
enumerable: true,
configurable: true,
writable: true,
value: cause
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'ReforgeError'
});
Object.defineProperty(this, "isReforgeError", {
enumerable: true,
configurable: true,
writable: true,
value: true
});
Object.defineProperty(this, "context", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.context = {
timestamp: new Date(),
...context,
};
// Maintain proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ReforgeError);
}
}
/**
* Convert error to JSON representation
*/
toJSON() {
return {
name: this.name,
message: this.message,
context: this.context,
stack: this.stack,
};
}
/**
* Get error details for logging
*/
getDetails() {
return {
...this.toJSON(),
cause: this.cause
? {
name: this.cause.name,
message: this.cause.message,
}
: undefined,
};
}
}
/**
* API-specific error class with enhanced HTTP context
*/
export class ReforgeApiError extends ReforgeError {
constructor(message,
// eslint-disable-next-line no-unused-vars
status,
// eslint-disable-next-line no-unused-vars
code,
// eslint-disable-next-line no-unused-vars
details, cause, context = {}) {
super(message, cause, context);
Object.defineProperty(this, "status", {
enumerable: true,
configurable: true,
writable: true,
value: status
});
Object.defineProperty(this, "code", {
enumerable: true,
configurable: true,
writable: true,
value: code
});
Object.defineProperty(this, "details", {
enumerable: true,
configurable: true,
writable: true,
value: details
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'ReforgeApiError'
});
}
/**
* Create ReforgeApiError from API error response
*/
static fromApiError(apiError, status, context) {
return new ReforgeApiError(apiError.message, status || apiError.status || 0, apiError.code, apiError.details, undefined, context);
}
/**
* Check if this is a client error (4xx)
*/
isClientError() {
return this.status >= 400 && this.status < 500;
}
/**
* Check if this is a server error (5xx)
*/
isServerError() {
return this.status >= 500 && this.status < 600;
}
/**
* Check if this is a specific error code
*/
hasCode(code) {
return this.code === code;
}
/**
* Check if this error indicates a rate limit
*/
isRateLimit() {
return this.status === 429 || this.hasCode('RATE_LIMIT');
}
/**
* Check if this error indicates authentication failure
*/
isAuthenticationError() {
return this.status === 401 || this.hasCode('UNAUTHORIZED');
}
/**
* Check if this error indicates authorization failure
*/
isAuthorizationError() {
return this.status === 403 || this.hasCode('FORBIDDEN');
}
/**
* Enhanced JSON representation
*/
toJSON() {
return {
...super.toJSON(),
status: this.status,
code: this.code,
details: this.details,
};
}
}
/**
* Network-specific error class with connection details
*/
export class ReforgeNetworkError extends ReforgeError {
constructor(message, url,
// eslint-disable-next-line no-unused-vars
timeout, cause, context = {}) {
super(message, cause, { ...context, url });
Object.defineProperty(this, "url", {
enumerable: true,
configurable: true,
writable: true,
value: url
});
Object.defineProperty(this, "timeout", {
enumerable: true,
configurable: true,
writable: true,
value: timeout
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'ReforgeNetworkError'
});
}
/**
* Create network error for timeout
*/
static timeout(url, context) {
return new ReforgeNetworkError('Request timed out', url, true, undefined, context);
}
/**
* Create network error for connection issues
*/
static connection(url, cause, context) {
return new ReforgeNetworkError('Network connection failed', url, false, cause, context);
}
/**
* Create network error for DNS resolution issues
*/
static dnsResolution(url, context) {
return new ReforgeNetworkError('DNS resolution failed', url, false, undefined, context);
}
/**
* Enhanced JSON representation
*/
toJSON() {
return {
...super.toJSON(),
url: this.url,
timeout: this.timeout,
};
}
}
/**
* Validation error class with field-specific details
*/
export class ReforgeValidationError extends ReforgeError {
constructor(message,
// eslint-disable-next-line no-unused-vars
field,
// eslint-disable-next-line no-unused-vars
value,
// eslint-disable-next-line no-unused-vars
constraint, cause, context = {}) {
super(message, cause, context);
Object.defineProperty(this, "field", {
enumerable: true,
configurable: true,
writable: true,
value: field
});
Object.defineProperty(this, "value", {
enumerable: true,
configurable: true,
writable: true,
value: value
});
Object.defineProperty(this, "constraint", {
enumerable: true,
configurable: true,
writable: true,
value: constraint
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'ReforgeValidationError'
});
}
/**
* Create validation error for required field
*/
static required(field, context) {
return new ReforgeValidationError(`Field '${field}' is required`, field, undefined, 'required', undefined, context);
}
/**
* Create validation error for invalid value
*/
static invalid(field, value, expected, context) {
const expectedMsg = expected ? `, expected ${expected}` : '';
return new ReforgeValidationError(`Invalid value for field '${field}': ${value}${expectedMsg}`, field, value, 'invalid', undefined, context);
}
/**
* Create validation error for format issues
*/
static format(field, value, format, context) {
return new ReforgeValidationError(`Field '${field}' must be in ${format} format, got: ${value}`, field, value, `format:${format}`, undefined, context);
}
/**
* Enhanced JSON representation
*/
toJSON() {
return {
...super.toJSON(),
field: this.field,
value: this.value,
constraint: this.constraint,
};
}
}
/**
* Configuration error class for SDK setup issues
*/
export class ReforgeConfigurationError extends ReforgeError {
constructor(message,
// eslint-disable-next-line no-unused-vars
configKey, cause, context = {}) {
super(message, cause, context);
Object.defineProperty(this, "configKey", {
enumerable: true,
configurable: true,
writable: true,
value: configKey
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'ReforgeConfigurationError'
});
}
/**
* Create configuration error for missing config
*/
static missing(configKey, context) {
return new ReforgeConfigurationError(`Configuration key '${configKey}' is missing or invalid`, configKey, undefined, context);
}
/**
* Enhanced JSON representation
*/
toJSON() {
return {
...super.toJSON(),
configKey: this.configKey,
};
}
}
/**
* Operation-specific error for reforge operations
*/
export class ReforgeOperationError extends ReforgeError {
constructor(message,
// eslint-disable-next-line no-unused-vars
operation,
// eslint-disable-next-line no-unused-vars
operationData, cause, context = {}) {
super(message, cause, context);
Object.defineProperty(this, "operation", {
enumerable: true,
configurable: true,
writable: true,
value: operation
});
Object.defineProperty(this, "operationData", {
enumerable: true,
configurable: true,
writable: true,
value: operationData
});
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'ReforgeOperationError'
});
}
/**
* Create operation error for initiate failures
*/
static initiateFailed(reason, data, context) {
return new ReforgeOperationError(`Reforge initiation failed: ${reason}`, 'initiate', data, undefined, context);
}
/**
* Create operation error for finish failures
*/
static finishFailed(reason, data, context) {
return new ReforgeOperationError(`Reforge completion failed: ${reason}`, 'finish', data, undefined, context);
}
/**
* Enhanced JSON representation
*/
toJSON() {
return {
...super.toJSON(),
operation: this.operation,
operationData: this.operationData,
};
}
}
//# sourceMappingURL=index.js.map