@metamask/snaps-sdk
Version:
A library containing the core functionality for building MetaMask Snaps
44 lines • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSnapError = void 0;
const errors_1 = require("../errors.cjs");
/**
* Create a `SnapError` class from an error function from
* `@metamask/rpc-errors`. This is useful for creating custom error classes
* which can be thrown by a Snap.
*
* The created class will inherit the message, code, and data properties from
* the error function.
*
* @param fn - The error function to create the class from.
* @returns The created `SnapError` class.
*/
function createSnapError(fn) {
return class SnapJsonRpcError extends errors_1.SnapError {
/**
* Create a new `SnapJsonRpcError` from a message and data.
*
* @param message - The message to create the error from.
* @param data - The data to create the error from.
*/
constructor(message, data) {
if (typeof message === 'object') {
const error = fn();
super({
code: error.code,
message: error.message,
data: message,
});
return;
}
const error = fn(message);
super({
code: error.code,
message: error.message,
data,
});
}
};
}
exports.createSnapError = createSnapError;
//# sourceMappingURL=error-wrappers.cjs.map