Skip to content

Commit

Permalink
fix: send error log on exception during loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Jun 23, 2023
1 parent 0fff936 commit e8d9122
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/gen_lsp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ defmodule GenLSP do
deb = :sys.handle_debug(deb, &write_debug/3, __MODULE__, {:in, :request, from})

attempt(
lsp,
"Last message received: handle_request #{inspect(request)}",
fn ->
{:ok, %{id: id} = req} = GenLSP.Requests.new(request)

Expand Down Expand Up @@ -250,14 +252,15 @@ defmodule GenLSP do
{:noreply, lsp} ->
loop(lsp, parent, deb)
end
end,
"Last message received: handle_request #{inspect(request)}"
end
)

{:notification, from, notification} ->
deb = :sys.handle_debug(deb, &write_debug/3, __MODULE__, {:in, :notification, from})

attempt(
lsp,
"Last message received: handle_notification #{inspect(notification)}",
fn ->
{:ok, note} = GenLSP.Notifications.new(notification)

Expand All @@ -267,36 +270,39 @@ defmodule GenLSP do
{:noreply, %LSP{} = lsp} ->
loop(lsp, parent, deb)
end
end,
"Last message received: handle_notification #{inspect(notification)}"
end
)

message ->
attempt(
lsp,
"Last message received: handle_info #{inspect(message)}",
fn ->
case lsp.mod.handle_info(message, lsp) do
{:noreply, %LSP{} = lsp} ->
loop(lsp, parent, deb)
end
end,
"Last message received: handle_info #{inspect(message)}"
end
)
end
end

@spec attempt((() -> any()), String.t()) :: no_return()
defp attempt(callback, message) do
@spec attempt(LSP.t(), String.t(), (() -> any())) :: no_return()
defp attempt(lsp, message, callback) do
callback.()
rescue
e ->
Logger.error("""
message = """
LSP Exited.
#{message}
#{Exception.format(:error, e, __STACKTRACE__)}
""")
"""

error(lsp, message)
Logger.error(message)

reraise e, __STACKTRACE__
end
Expand Down
12 changes: 12 additions & 0 deletions test/gen_lsp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ defmodule GenLSPTest do
500
end

test "sends an error log on exception", %{server: server} do
send(server.lsp, :boom)

assert_notification "window/logMessage", %{
"message" => message,
"type" => 1
}

assert message =~
"LSP Exited.\n\nLast message received: handle_info :boom\n\n** (RuntimeError) boom"
end

test "can receive a normal message with handle_info/2", %{server: server} do
send(server.lsp, "hi")

Expand Down
5 changes: 5 additions & 0 deletions test/support/example_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ defmodule GenLSPTest.ExampleServer do
end

@impl true
def handle_info(:boom, lsp) do
raise "boom"
{:noreply, lsp}
end

def handle_info(_message, lsp) do
send(lsp.assigns.test_pid, {:info, :ack})
{:noreply, lsp}
Expand Down

0 comments on commit e8d9122

Please sign in to comment.