From d6234a5053ec438f25b965bebc61d8ee3a0a4b7a Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Tue, 21 Jan 2025 20:38:06 -0800 Subject: [PATCH 1/4] Send error response if parameters can't be parsed --- src/typed.jl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/typed.jl b/src/typed.jl index e080d81e..01725a68 100644 --- a/src/typed.jl +++ b/src/typed.jl @@ -62,7 +62,18 @@ function dispatch_msg(x::JSONRPCEndpoint, dispatcher::MsgDispatcher, msg::Reques 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 + send_error_response(x, msg, INVALID_PARAMS, "Failed to parse parameters.", nothing) + rethrow(err) + end if handler.message_type isa RequestType res = handler.func(x, params, msg.token) From 7ae9db4bd4b510fb0d1e3ce014a6a3657c5bd10d Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Tue, 21 Jan 2025 20:39:39 -0800 Subject: [PATCH 2/4] Only send error response for requests --- src/typed.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/typed.jl b/src/typed.jl index 01725a68..4703d6ad 100644 --- a/src/typed.jl +++ b/src/typed.jl @@ -59,6 +59,7 @@ 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) @@ -71,7 +72,9 @@ function dispatch_msg(x::JSONRPCEndpoint, dispatcher::MsgDispatcher, msg::Reques param_type(msg.params) end catch err - send_error_response(x, msg, INVALID_PARAMS, "Failed to parse parameters.", nothing) + if is_request + send_error_response(x, msg, INVALID_PARAMS, "Failed to parse parameters.", nothing) + end rethrow(err) end @@ -93,6 +96,7 @@ function dispatch_msg(x::JSONRPCEndpoint, dispatcher::MsgDispatcher, msg::Reques end end else + error("Unknown method $method_name.") end finally From 81bb01cf9a7f65bbb64669c40c4ca7556ef8c031 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Tue, 21 Jan 2025 20:45:38 -0800 Subject: [PATCH 3/4] Handle parameter parsing error in static dispatch as well --- src/typed.jl | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/typed.jl b/src/typed.jl index 4703d6ad..ecc18c33 100644 --- a/src/typed.jl +++ b/src/typed.jl @@ -110,13 +110,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 From a9403bf40f18a2a7c1d129b81cf03cbb19ed44da Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Tue, 21 Jan 2025 20:45:50 -0800 Subject: [PATCH 4/4] Send error response for unknown requests --- src/typed.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/typed.jl b/src/typed.jl index ecc18c33..22dc030b 100644 --- a/src/typed.jl +++ b/src/typed.jl @@ -96,7 +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 @@ -164,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