delegate-framework
Version:
A TypeScript framework for building robust, production-ready blockchain workflows with comprehensive error handling, logging, and testing. Maintained by delegate.fun
86 lines • 2.7 kB
JavaScript
;
/**
* Utility functions for error handling
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.throwError = throwError;
exports.getErrorMessage = getErrorMessage;
exports.isErrorLike = isErrorLike;
exports.createStandardError = createStandardError;
/**
* Throws an error with proper type checking and handling
* @param error - The error to throw (can be Error, string, or object with message)
* @param context - Optional context information for debugging
*/
function throwError(error, context) {
if (error instanceof Error) {
// Preserve original error with stack trace
throw error;
}
else if (typeof error === 'string') {
// Create Error from string
const message = context ? `${context}: ${error}` : error;
throw new Error(message);
}
else if (error && typeof error === 'object' && 'message' in error) {
// Extract message from object
const message = context ? `${context}: ${error.message}` : error.message;
throw new Error(message);
}
else {
// Fallback for any other type
const message = context ? `${context}: ${String(error)}` : String(error);
throw new Error(message);
}
}
/**
* Safely extracts error message from various error types
* @param error - The error to extract message from
* @returns The error message as a string
*/
function getErrorMessage(error) {
if (error instanceof Error) {
return error.message;
}
else if (typeof error === 'string') {
return error;
}
else if (error && typeof error === 'object' && 'message' in error) {
return String(error.message);
}
else {
return String(error);
}
}
/**
* Checks if a value is an error-like object
* @param value - The value to check
* @returns True if the value is an error-like object
*/
function isErrorLike(value) {
return value !== null &&
typeof value === 'object' &&
'message' in value &&
typeof value.message === 'string';
}
/**
* Creates a standardized error from various input types
* @param error - The error input
* @param defaultMessage - Default message if error is empty/null/undefined
* @returns A standardized Error object
*/
function createStandardError(error, defaultMessage = 'An unknown error occurred') {
if (error instanceof Error) {
return error;
}
else if (typeof error === 'string' && error.trim()) {
return new Error(error);
}
else if (isErrorLike(error)) {
return new Error(error.message);
}
else {
return new Error(defaultMessage);
}
}
//# sourceMappingURL=error-handling.js.map