@rarible/utils
Version:
> TODO: description
37 lines (36 loc) • 992 B
JavaScript
import * as tg from "generic-type-guard";
const hasMessage = tg.isLikeObject({
message: tg.isString,
});
export function extractErrorMessage(error) {
if (typeof error === "string")
return error;
if (hasMessage(error))
return error.message;
return undefined;
}
const hasName = tg.isLikeObject({
name: tg.isString,
});
export function extractErrorName(error) {
var _a;
if (hasName(error))
return error.name;
if (tg.isObjectLike(error)) {
if (typeof ((_a = error === null || error === void 0 ? void 0 : error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "string") {
return error.constructor.name;
}
}
return undefined;
}
const hasStack = tg.isLikeObject({
stack: tg.isString,
});
export function extractErrorStack(error) {
if (hasStack(error))
return error.stack;
return undefined;
}
export function isErrorLike(obj) {
return hasName(obj) && hasMessage(obj);
}