@kitiumai/utils-ts
Version:
Comprehensive TypeScript utilities for KitiumAI projects
62 lines • 1.38 kB
JavaScript
/**
* Base error class for utils-ts package
*/
export class UtilsError extends Error {
code;
details;
constructor(init) {
super(init.message);
this.name = 'UtilsError';
this.code = init.code;
this.details = init.details;
if (init.cause) {
this.cause = init.cause;
}
}
/**
* Convert to JSON representation
*/
toJSON() {
return {
name: this.name,
code: this.code,
message: this.message,
...(this.details ? { details: this.details } : {}),
...(this.cause ? { cause: this.cause } : {}),
};
}
}
/**
* Create a utility error
*/
export const createUtilsError = (init) => {
return new UtilsError(init);
};
/**
* Check if an error is a UtilsError
*/
export function isUtilsError(error) {
return error instanceof UtilsError;
}
/**
* Extract error message from unknown error
*/
export function getErrorMessage(error) {
if (error instanceof Error) {
return error.message;
}
if (typeof error === 'string') {
return error;
}
return 'Unknown error';
}
/**
* Extract error stack from unknown error
*/
export function getErrorStack(error) {
if (error instanceof Error) {
return error.stack;
}
return undefined;
}
//# sourceMappingURL=error.js.map