From 20cdecdad11d98e5d2e11d66b7b3b5ef427cac13 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Wed, 18 Oct 2023 01:24:23 +0200 Subject: [PATCH] fix: remove non-serializable properties from exceptions --- src/rpc-handler.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/rpc-handler.ts b/src/rpc-handler.ts index 47b89f231..af29940ae 100644 --- a/src/rpc-handler.ts +++ b/src/rpc-handler.ts @@ -30,13 +30,14 @@ export class MethodNotSupportedError extends Error { } /** - * Handles a keyring JSON-RPC request. + * Inner function that dispatches JSON-RPC request to the associated Keyring + * methods. * * @param keyring - Keyring instance. * @param request - Keyring JSON-RPC request. * @returns A promise that resolves to the keyring response. */ -export async function handleKeyringRequest( +async function dispatchRequest( keyring: Keyring, request: JsonRpcRequest, ): Promise { @@ -128,3 +129,40 @@ export async function handleKeyringRequest( } } } + +/** + * Handles a keyring JSON-RPC request. + * + * This function is meant to be used as a handler for Keyring JSON-RPC requests + * in an Accounts Snap. + * + * Example: + * + * ```typescript + * export const onKeyringRequest: OnKeyringRequestHandler = async ({ + * origin, + * request, + * }) => { + * return await handleKeyringRequest(keyring, request); + * }; + * ``` + * + * @param keyring - Keyring instance. + * @param request - Keyring JSON-RPC request. + * @returns A promise that resolves to the keyring response. + */ +export async function handleKeyringRequest( + keyring: Keyring, + request: JsonRpcRequest, +): Promise { + try { + return await dispatchRequest(keyring, request); + } catch (error) { + const message = + error instanceof Error && typeof error.message === 'string' + ? error.message + : 'An unknown error occurred while handling the keyring request'; + + throw new Error(message); + } +}