UNPKG

@metamask/snaps-rpc-methods

Version:
87 lines 2.87 kB
import { rpcErrors } from "@metamask/rpc-errors"; import { TrackableErrorStruct } from "@metamask/snaps-utils"; import { create, object, StructError } from "@metamask/superstruct"; const hookNames = { trackError: true, }; const TrackErrorParametersStruct = object({ error: TrackableErrorStruct, }); /** * Handler for the `snap_trackError` method. * * @internal */ export const 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(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 create(params, TrackErrorParametersStruct); } catch (error) { if (error instanceof StructError) { throw rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw 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.mjs.map