diff --git a/src/typed.jl b/src/typed.jl index e080d81e..22dc030b 100644 --- a/src/typed.jl +++ b/src/typed.jl @@ -59,10 +59,24 @@ function dispatch_msg(x::JSONRPCEndpoint, dispatcher::MsgDispatcher, msg::Reques dispatcher._currentlyHandlingMsg = true try method_name = msg.method + is_request = msg.id !== nothing handler = get(dispatcher._handlers, method_name, nothing) if handler !== nothing param_type = get_param_type(handler.message_type) - params = param_type === Nothing ? nothing : param_type <: NamedTuple ? convert(param_type,(;(Symbol(i[1])=>i[2] for i in msg.params)...)) : param_type(msg.params) + params = try + if param_type === Nothing + nothing + elseif param_type <: NamedTuple + convert(param_type,(;(Symbol(i[1])=>i[2] for i in msg.params)...)) + else + param_type(msg.params) + end + catch err + if is_request + send_error_response(x, msg, INVALID_PARAMS, "Failed to parse parameters.", nothing) + end + rethrow(err) + end if handler.message_type isa RequestType res = handler.func(x, params, msg.token) @@ -82,6 +96,9 @@ function dispatch_msg(x::JSONRPCEndpoint, dispatcher::MsgDispatcher, msg::Reques end end else + if is_request + send_error_response(x, msg, METHOD_NOT_FOUND, "Unknown method '$method_name'.", nothing) + end error("Unknown method $method_name.") end finally @@ -95,13 +112,27 @@ macro message_dispatcher(name, body) quote function $(esc(name))(x, msg::Request, context=nothing) method_name = msg.method + is_request = msg.id !== nothing $( ( :( if method_name == $(esc(i.args[2])).method param_type = get_param_type($(esc(i.args[2]))) - params = param_type === Nothing ? nothing : param_type <: NamedTuple ? convert(param_type,(;(Symbol(i[1])=>i[2] for i in msg.params)...)) : param_type(msg.params) + params = try + if param_type === Nothing + nothing + elseif param_type <: NamedTuple + convert(param_type,(;(Symbol(i[1])=>i[2] for i in msg.params)...)) + else + param_type(msg.params) + end + catch err + if is_request + send_error_response(x, msg, INVALID_PARAMS, "Failed to parse parameters.", nothing) + end + rethrow(err) + end if context===nothing if $(esc(i.args[2])) isa RequestType @@ -135,6 +166,10 @@ macro message_dispatcher(name, body) )... ) + if is_request + send_error_response(x, msg, METHOD_NOT_FOUND, "Unknown method '$method_name'.", nothing) + end + error("Unknown method $method_name.") end end