Skip to content

Send error responses #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
39 changes: 37 additions & 2 deletions src/typed.jl
Original file line number Diff line number Diff line change
@@ -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