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
17 changes: 4 additions & 13 deletions lib/gen_lsp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ defmodule GenLSP do
start = System.system_time(:microsecond)
:telemetry.execute([:gen_lsp, :request, :client, :start], %{})

me = self()
id = request["id"]

{:ok, task} =
attempt(
Expand All @@ -352,7 +352,7 @@ defmodule GenLSP do

packet = %{
"jsonrpc" => "2.0",
"id" => Process.get(:request_id),
"id" => id,
"error" => output
}

Expand All @@ -363,10 +363,7 @@ defmodule GenLSP do

_ ->
case GenLSP.Requests.new(request) do
{:ok, %{id: id} = req} ->
Process.put(:request_id, id)
send(me, {:request_id, id})

{:ok, req} ->
result =
:telemetry.span([:gen_lsp, :handle_request], %{method: req.method}, fn ->
{lsp.mod.handle_request(req, lsp), %{}}
Expand Down Expand Up @@ -455,7 +452,7 @@ defmodule GenLSP do

packet = %{
"jsonrpc" => "2.0",
"id" => request["id"],
"id" => id,
"error" => output
}

Expand All @@ -464,12 +461,6 @@ defmodule GenLSP do
end
)

id =
receive do
{:request_id, id} ->
id
end

tasks = Map.put(lsp.tasks, id, task)

lsp = put_in(lsp.tasks, tasks)
Expand Down
34 changes: 33 additions & 1 deletion test/gen_lsp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ defmodule GenLSPTest do
assert log =~ expected_msg
end

test "returns an invalid request when the paylaod is not parseable, but is still deemed a request",
test "returns an invalid request when the payload is not parseable, but is still deemed a request",
%{client: client} do
assert :ok == request(client, %{"id" => "bingo", "random" => "stuff"})

Expand All @@ -336,6 +336,38 @@ defmodule GenLSPTest do
500
end

test "gracefully recovers from invalid requests and continues to accept requests",
%{client: client} do
assert :ok == request(client, %{"id" => "whoops", "random" => "again"})

assert_error "whoops",
%{
"message" => "Invalid request from the client" <> _,
"code" => -32600
},
500

id = System.unique_integer([:positive])

assert :ok ==
request(client, %{
"jsonrpc" => "2.0",
"method" => "initialize",
"params" => %{"capabilities" => %{}},
"id" => id
})

assert_result ^id,
%{
"capabilities" => %{
"callHierarchyProvider" => %{"workDoneProgress" => true},
"experimental" => nil
},
"serverInfo" => %{"name" => "Test LSP"}
},
500
end

test "logs when an invalid payload is received", %{client: client} do
assert capture_log(fn ->
assert :ok == notify(client, %{"random" => "stuff"})
Expand Down
Loading