@rarible/utils
Version:
> TODO: description
45 lines (44 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isErrorLike = exports.extractErrorStack = exports.extractErrorName = exports.extractErrorMessage = void 0;
const tslib_1 = require("tslib");
const tg = tslib_1.__importStar(require("generic-type-guard"));
const hasMessage = tg.isLikeObject({
message: tg.isString,
});
function extractErrorMessage(error) {
if (typeof error === "string")
return error;
if (hasMessage(error))
return error.message;
return undefined;
}
exports.extractErrorMessage = extractErrorMessage;
const hasName = tg.isLikeObject({
name: tg.isString,
});
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;
}
exports.extractErrorName = extractErrorName;
const hasStack = tg.isLikeObject({
stack: tg.isString,
});
function extractErrorStack(error) {
if (hasStack(error))
return error.stack;
return undefined;
}
exports.extractErrorStack = extractErrorStack;
function isErrorLike(obj) {
return hasName(obj) && hasMessage(obj);
}
exports.isErrorLike = isErrorLike;