UNPKG

@metamask/snaps-sdk

Version:

A library containing the core functionality for building MetaMask Snaps

122 lines 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getErrorData = exports.getErrorCause = exports.getErrorCode = exports.getErrorName = exports.getErrorStack = exports.getErrorMessage = exports.SNAP_ERROR_MESSAGE = exports.SNAP_ERROR_CODE = void 0; const utils_1 = require("@metamask/utils"); exports.SNAP_ERROR_CODE = -31002; exports.SNAP_ERROR_MESSAGE = 'Snap Error'; /** * Get a property from an object, or return a fallback value if the property * does not exist. * * @param object - The object to get the property from. * @param property - The property to get from the object. * @param fallback - The fallback value to return if the property does not * exist. * @returns The value of the property if it exists, or the fallback value if * the property does not exist. */ function getObjectProperty(object, property, fallback = null) { if ((0, utils_1.isObject)(object) && (0, utils_1.hasProperty)(object, property)) { return object[property]; } return fallback; } /** * Get a string property from an object, or convert the object to a string * if the property does not exist or is not a string. * * @param object - The object to get the property from. * @param property - The property to get from the object. * @param fallback - The fallback value to return if the property does not exist * or is not a string. Defaults to the string representation of the object. * @returns The value of the property if it exists and is a string, or the * fallback value if it does not exist or is not a string. */ function getObjectStringProperty(object, property, fallback = String(object)) { const value = getObjectProperty(object, property); if (typeof value === 'string') { return value; } return fallback; } /** * Get the error message from an unknown error type. * * - If the error is an object with a `message` property, return the message. * - Otherwise, return the error converted to a string. * * @param error - The error to get the message from. * @returns The error message. * @internal */ function getErrorMessage(error) { return getObjectStringProperty(error, 'message'); } exports.getErrorMessage = getErrorMessage; /** * Get the error stack from an unknown error type. * * @param error - The error to get the stack from. * @returns The error stack, or undefined if the error does not have a valid * stack. * @internal */ function getErrorStack(error) { return getObjectStringProperty(error, 'stack', null); } exports.getErrorStack = getErrorStack; /** * Get the error name from an unknown error type. * * @param error - The error to get the name from. * @returns The error name, or `'Error'` if the error does not have a valid * name. */ function getErrorName(error) { const fallbackName = error instanceof Error ? error.name : 'Error'; return getObjectStringProperty(error, 'name', fallbackName); } exports.getErrorName = getErrorName; /** * Get the error code from an unknown error type. * * @param error - The error to get the code from. * @returns The error code, or `-32603` if the error does not have a valid code. * @internal */ function getErrorCode(error) { const value = getObjectProperty(error, 'code'); if (typeof value === 'number' && Number.isInteger(value)) { return value; } return -32603; } exports.getErrorCode = getErrorCode; /** * Get the error cause from an unknown error type. * * @param error - The error to get the cause from. * @returns The error cause, or `null` if the error does not have a valid * cause. */ function getErrorCause(error) { return getObjectProperty(error, 'cause'); } exports.getErrorCause = getErrorCause; /** * Get the error data from an unknown error type. * * @param error - The error to get the data from. * @returns The error data, or an empty object if the error does not have valid * data. * @internal */ function getErrorData(error) { const value = getObjectProperty(error, 'data'); if (value !== null && (0, utils_1.isValidJson)(value) && !Array.isArray(value)) { return value; } return {}; } exports.getErrorData = getErrorData; //# sourceMappingURL=errors.cjs.map