@wasserstoff/tribes-sdk
Version:
SDK for integrating with Tribes by Astrix platform on any EVM compatible chain
57 lines (56 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AstrixSDKError = void 0;
exports.fromError = fromError;
exports.handleError = handleError;
const core_1 = require("./core");
/**
* Custom error class for the SDK
*/
class AstrixSDKError extends Error {
constructor(type, message, code, data, originalError) {
super(message);
this.name = 'AstrixSDKError';
this.type = type;
this.data = data;
this.originalError = originalError;
// This is necessary for proper instanceof checking in TypeScript
Object.setPrototypeOf(this, AstrixSDKError.prototype);
}
/**
* Convert to string representation
*/
toString() {
return `[${this.type}] ${this.message}${this.data ? ` - ${JSON.stringify(this.data)}` : ''}`;
}
/**
* Convert to plain object
*/
toObject() {
return {
type: this.type,
message: this.message,
data: this.data,
stack: this.stack
};
}
}
exports.AstrixSDKError = AstrixSDKError;
function fromError(error, defaultMessage = 'An unknown error occurred') {
if (error instanceof AstrixSDKError) {
return error;
}
// Basic error handling for common types
if (error instanceof Error) {
// Extract code if it exists (might be custom or from ethers)
const code = error.code || core_1.ErrorType.SDK_ERROR;
return new AstrixSDKError(core_1.ErrorType.SDK_ERROR, `${defaultMessage}: ${error.message}`, code, undefined, error);
}
return new AstrixSDKError(core_1.ErrorType.SDK_ERROR, defaultMessage, undefined, error, error);
}
function handleError(error, message, _type) {
if (error instanceof AstrixSDKError) {
throw error;
}
throw fromError(error, message);
}