Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions tck/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ def wrapper(*args, **kwargs):
except JsonRpcError:
raise
except PrecheckError as e:
logger.error(f"PrecheckError (status: {ResponseCode(e.status).name}, method: {func.__name__})")
logger.error(f"PrecheckError (method: {func.__name__}, status: {ResponseCode(e.status).name})")
raise JsonRpcError.hiero_error({"status": ResponseCode(e.status).name}) from e

except ReceiptStatusError as e:
logger.error(f"ReceiptStatusError (status: {ResponseCode(e.status).name}, method: {func.__name__})")
logger.error(f"ReceiptStatusError (method: {func.__name__}, status: {ResponseCode(e.status).name})")
raise JsonRpcError.hiero_error({"status": ResponseCode(e.status).name}) from e

except MaxAttemptsError as e:
logger.error(f"MaxAttemptsError (method: {func.__name__})")
raise JsonRpcError.hiero_error(message=str(e)) from e
logger.error(f"MaxAttemptsError (method: {func.__name__}, error: {str(e)})")
raise JsonRpcError.hiero_error() from e

except Exception as e:
logger.exception("Unhandled error in RPC handler")
raise JsonRpcError.internal_error(message="Internal error") from e
logger.error(f"InternalError (method: {func.__name__}) error: {str(e)}")
raise JsonRpcError.internal_error() from e

return wrapper
20 changes: 16 additions & 4 deletions tck/handlers/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import inspect
import logging
from collections.abc import Callable
from dataclasses import asdict, fields as dc_fields
from typing import Any, get_type_hints
Expand All @@ -13,6 +14,9 @@
from tck.protocol import build_json_rpc_error_response


logger = logging.getLogger(__name__)


# A global _HANDLERS dict to store method name -> handler function mappings
_HANDLERS: dict[str, Callable] = {}

Expand Down Expand Up @@ -43,7 +47,10 @@ def dispatch(method_name: str, params: Any) -> Any:
handler = get_handler(method_name)

if handler is None:
raise JsonRpcError.method_not_found_error(message=f"Method not found: {method_name}")
logger.warning(
f"MethodNotFoundError (method: {repr(method_name)}) error: The requested RPC method is not registered."
)
raise JsonRpcError.method_not_found_error()

try:
target_func = getattr(handler, "__wrapped__", handler)
Expand All @@ -58,26 +65,31 @@ def dispatch(method_name: str, params: Any) -> Any:
param_type = hints.get(param_name, parameters[0].annotation)
params = param_type.parse_json_params(params)
except (TypeError, ValueError) as e:
logger.error(f"InvalidParamsError (method: {repr(method_name)}) error: {str(e)}")
raise JsonRpcError.invalid_params_error(data=str(e)) from e

result = handler(params)

return parse_result(result)

except JsonRpcError:
except JsonRpcError as e:
logger.error(f"JsonRpcError (method: {repr(method_name)}) error: {str(e)}")
raise
except Exception as e:
raise JsonRpcError.internal_error(data=str(e)) from e
logger.error(f"InternalError (method: {repr(method_name)}) error: {str(e)}")
Comment thread
manishdait marked this conversation as resolved.
raise JsonRpcError.internal_error() from e


def safe_dispatch(method_name: str, params: Any, request_id: str | int | None) -> Any | dict[str, Any]:
"""Safely dispatch the request and handle exceptions."""
try:
return dispatch(method_name, params)
except JsonRpcError as e:
logger.error(f"JsonRpcError (method: {repr(method_name)}) error: {str(e)}")
return build_json_rpc_error_response(e, request_id)
except Exception as e:
error = JsonRpcError.internal_error(data=str(e))
logger.error(f"InternalError (method: {repr(method_name)}) error: {str(e)}")
error = JsonRpcError.internal_error()
return build_json_rpc_error_response(error, request_id)
Comment thread
exploreriii marked this conversation as resolved.


Expand Down
Loading