@metamask/snaps-sdk
Version:
A library containing the core functionality for building MetaMask Snaps
80 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getErrorData = exports.getErrorCode = 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 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) {
if ((0, utils_1.isObject)(error) &&
(0, utils_1.hasProperty)(error, 'message') &&
typeof error.message === 'string') {
return error.message;
}
return String(error);
}
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) {
if ((0, utils_1.isObject)(error) &&
(0, utils_1.hasProperty)(error, 'stack') &&
typeof error.stack === 'string') {
return error.stack;
}
return undefined;
}
exports.getErrorStack = getErrorStack;
/**
* 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) {
if ((0, utils_1.isObject)(error) &&
(0, utils_1.hasProperty)(error, 'code') &&
typeof error.code === 'number' &&
Number.isInteger(error.code)) {
return error.code;
}
return -32603;
}
exports.getErrorCode = getErrorCode;
/**
* 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) {
if ((0, utils_1.isObject)(error) &&
(0, utils_1.hasProperty)(error, 'data') &&
typeof error.data === 'object' &&
error.data !== null &&
(0, utils_1.isValidJson)(error.data) &&
!Array.isArray(error.data)) {
return error.data;
}
return {};
}
exports.getErrorData = getErrorData;
//# sourceMappingURL=errors.cjs.map