@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
90 lines • 3.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.trackErrorHandler = void 0;
const rpc_errors_1 = require("@metamask/rpc-errors");
const snaps_utils_1 = require("@metamask/snaps-utils");
const superstruct_1 = require("@metamask/superstruct");
const hookNames = {
trackError: true,
};
const TrackErrorParametersStruct = (0, superstruct_1.object)({
error: snaps_utils_1.TrackableErrorStruct,
});
/**
* Handler for the `snap_trackError` method.
*
* @internal
*/
exports.trackErrorHandler = {
implementation: getTrackErrorImplementation,
hookNames,
actionNames: ['SnapController:getSnap'],
};
/**
* The `snap_trackError` method implementation. This method allows preinstalled
* Snaps to send errors to the Sentry instance in the client for tracking.
*
* @param request - The JSON-RPC request object.
* @param response - The JSON-RPC response object.
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
* function.
* @param end - The `json-rpc-engine` "end" callback.
* @param hooks - The RPC method hooks.
* @param hooks.trackError - The hook function to track an error.
* @param messenger - The messenger used to call controller actions.
* @returns Nothing.
*/
function getTrackErrorImplementation(request, response, _next, end, { trackError }, messenger) {
const snap = messenger.call('SnapController:getSnap', request.origin);
if (!snap?.preinstalled) {
return end(rpc_errors_1.rpcErrors.methodNotFound());
}
const { params } = request;
try {
const validatedParams = getValidatedParams(params);
const error = deserializeError(validatedParams.error);
response.result = trackError(error);
}
catch (error) {
return end(error);
}
return end();
}
/**
* Validate the parameters for the `snap_trackError` method.
*
* @param params - Parameters to validate.
* @returns Validated parameters.
* @throws Throws RPC error if validation fails.
*/
function getValidatedParams(params) {
try {
return (0, superstruct_1.create)(params, TrackErrorParametersStruct);
}
catch (error) {
if (error instanceof superstruct_1.StructError) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: `Invalid params: ${error.message}.`,
});
}
/* istanbul ignore next */
throw rpc_errors_1.rpcErrors.internal();
}
}
/**
* Deserialize a {@link TrackableError} into a standard {@link Error} object.
*
* @param error - The error to deserialize.
* @returns A standard {@link Error} object with the same properties as the
* original {@link TrackableError}.
*/
function deserializeError(error) {
const deserializedError = new Error(error.message);
deserializedError.name = error.name;
deserializedError.stack = error.stack ?? undefined;
deserializedError.cause = error.cause
? deserializeError(error.cause)
: undefined;
return deserializedError;
}
//# sourceMappingURL=trackError.cjs.map