Skip to content

Add error dictionary for DAP errors #1209

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

Merged
merged 3 commits into from
Jul 1, 2025
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
23 changes: 23 additions & 0 deletions apps/debug_adapter/lib/debug_adapter/error_dictionary.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule ElixirLS.DebugAdapter.ErrorDictionary do
@moduledoc """
Provides mapping from error names to unique integer codes for DAP error messages.
"""

@codes %{
"internalServerError" => 1,
"cancelled" => 2,
"invalidRequest" => 3,
"launchError" => 4,
"attachError" => 5,
"invalidArgument" => 6,
"evaluateError" => 7,
"argumentError" => 8,
"runtimeError" => 9,
"notSupported" => 10
}

@spec code(String.t()) :: integer()
def code(name) do
Map.fetch!(@codes, name)
end
end
12 changes: 6 additions & 6 deletions apps/debug_adapter/lib/debug_adapter/output.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule ElixirLS.DebugAdapter.Output do
def send_error_response(
server \\ __MODULE__,
request_packet,
error_code,
message,
format,
variables,
Expand All @@ -32,8 +33,8 @@ defmodule ElixirLS.DebugAdapter.Output do
) do
GenServer.call(
server,
{:send_error_response, request_packet, message, format, variables, send_telemetry,
show_user},
{:send_error_response, request_packet, error_code, message, format, variables,
send_telemetry, show_user},
:infinity
)
end
Expand Down Expand Up @@ -142,8 +143,8 @@ defmodule ElixirLS.DebugAdapter.Output do
end

def handle_call(
{:send_error_response, request_packet, message, format, variables, send_telemetry,
show_user},
{:send_error_response, request_packet, error_code, message, format, variables,
send_telemetry, show_user},
_from,
seq
) do
Expand All @@ -159,8 +160,7 @@ defmodule ElixirLS.DebugAdapter.Output do
message: message,
body: %{
error: %GenDAP.Structures.Message{
# TODO unique ids
id: 1,
id: error_code,
format: format,
variables: variables,
send_telemetry: send_telemetry,
Expand Down
9 changes: 8 additions & 1 deletion apps/debug_adapter/lib/debug_adapter/protocol.basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ defmodule ElixirLS.DebugAdapter.Protocol.Basic do
send_telemetry,
show_user
) do
message_value = Macro.expand_once(message, __CALLER__)
error_id =
case message_value do
value when is_binary(value) -> ElixirLS.DebugAdapter.ErrorDictionary.code(value)
_ -> quote(do: ElixirLS.DebugAdapter.ErrorDictionary.code(unquote(message)))
end

quote do
%{
"type" => "response",
Expand All @@ -68,7 +75,7 @@ defmodule ElixirLS.DebugAdapter.Protocol.Basic do
"message" => unquote(message),
"body" => %{
"error" => %{
"id" => unquote(seq),
"id" => unquote(error_id),
"format" => unquote(format),
"variables" => unquote(variables),
"showUser" => unquote(show_user),
Expand Down
29 changes: 26 additions & 3 deletions apps/debug_adapter/lib/debug_adapter/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ defmodule ElixirLS.DebugAdapter.Server do
ModuleInfoCache,
IdManager,
VariableRegistry,
ThreadRegistry
ThreadRegistry,
ErrorDictionary
}

alias ElixirLS.DebugAdapter.Stacktrace.Frame
Expand Down Expand Up @@ -346,6 +347,7 @@ defmodule ElixirLS.DebugAdapter.Server do
{:error, e = %ServerError{}} ->
Output.send_error_response(
packet,
ErrorDictionary.code(e.message),
e.message,
e.format,
e.variables,
Expand Down Expand Up @@ -497,6 +499,7 @@ defmodule ElixirLS.DebugAdapter.Server do
e in ServerError ->
Output.send_error_response(
packet,
ErrorDictionary.code(e.message),
e.message,
e.format,
e.variables,
Expand Down Expand Up @@ -524,7 +527,17 @@ defmodule ElixirLS.DebugAdapter.Server do

message = Exception.format(kind, payload, stacktrace)
Output.debugger_console(message)
Output.send_error_response(packet, "internalServerError", message, %{}, true, false)

Output.send_error_response(
packet,
ErrorDictionary.code("internalServerError"),
"internalServerError",
message,
%{},
true,
false
)

{:noreply, state}
end
end
Expand Down Expand Up @@ -641,6 +654,7 @@ defmodule ElixirLS.DebugAdapter.Server do

Output.send_error_response(
packet,
ErrorDictionary.code("internalServerError"),
"internalServerError",
"Request handler exited with reason #{Exception.format_exit(reason)}",
%{},
Expand Down Expand Up @@ -788,7 +802,16 @@ defmodule ElixirLS.DebugAdapter.Server do
# flush as we are not interested in :DOWN message anymore
Process.demonitor(ref, [:flush])
Process.exit(pid, :cancelled)
Output.send_error_response(packet, "cancelled", "cancelled", %{}, false, false)

Output.send_error_response(
packet,
ErrorDictionary.code("cancelled"),
"cancelled",
"cancelled",
%{},
false,
false
)

# send progressEnd if cancelling a progress
updated_progresses =
Expand Down
29 changes: 29 additions & 0 deletions apps/debug_adapter/test/output_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule ElixirLS.DebugAdapter.OutputTest do
use ExUnit.Case, async: true
import ElixirLS.DebugAdapter.Protocol.Basic

alias ElixirLS.DebugAdapter.Output

setup do
{:ok, capture} = ElixirLS.Utils.PacketCapture.start_link(self())
{:ok, output} = Output.start(:output_test)
Process.group_leader(output, capture)

on_exit(fn ->
if Process.alive?(output), do: GenServer.stop(output)
end)

{:ok, %{output: output}}
end

test "error response uses provided id", %{output: output} do
req = request(1, "cmd")
Output.send_error_response(output, req, 42, "err", "fmt", %{}, false, false)

assert_receive %{
"body" => %{"error" => %{"id" => 42}},
"seq" => 1,
"request_seq" => 1
}
end
end
Loading