Skip to content
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

Convert all exceptions in config files into diagnostics #824

Merged
merged 2 commits into from
Sep 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ defmodule Lexical.RemoteControl.Build.Document.Compilers.Config do
alias Lexical.Document
alias Lexical.Plugin.V1.Diagnostic
alias Lexical.RemoteControl.Build
alias Lexical.RemoteControl.Build.Error.Location

@elixir_source "Elixir"

@behaviour Build.Document.Compiler
require Logger
Expand Down Expand Up @@ -49,7 +52,7 @@ defmodule Lexical.RemoteControl.Build.Document.Compilers.Config do
{:ok, []}
rescue
e ->
{:error, [to_result(document, e)]}
{:error, [to_result(document, e, __STACKTRACE__)]}
end
end

Expand All @@ -73,33 +76,41 @@ defmodule Lexical.RemoteControl.Build.Document.Compilers.Config do
end
end

defp to_result(%Document{} = document, %CompileError{} = error) do
Diagnostic.Result.new(document.uri, error.line, Exception.message(error), :error, "Elixir")
defp to_result(document, error, stack \\ [])

defp to_result(%Document{} = document, %CompileError{} = error, _stack) do
Diagnostic.Result.new(
document.uri,
error.line,
Exception.message(error),
:error,
@elixir_source
)
end

defp to_result(%Document{} = document, %error_type{} = error)
defp to_result(%Document{} = document, %error_type{} = error, _stack)
when error_type in [SyntaxError, TokenMissingError] do
Diagnostic.Result.new(
document.uri,
{error.line, error.column},
Exception.message(error),
:error,
"Elixir"
@elixir_source
)
end

defp to_result(%Document{} = document, %{
position: position,
message: message,
severity: severity
}) do
Diagnostic.Result.new(
document.path,
position,
message,
severity,
"Elixir"
)
defp to_result(
%Document{} = document,
%{position: position, message: message, severity: severity},
_stack
) do
Diagnostic.Result.new(document.path, position, message, severity, @elixir_source)
end

defp to_result(%Document{} = document, %{__exception__: true} = exception, stack) do
message = Exception.message(exception)
position = Location.stack_to_position(stack)
Diagnostic.Result.new(document.path, position, message, :error, @elixir_source)
end

defp reject_logged_messages(results) do
Expand Down
20 changes: 3 additions & 17 deletions apps/remote_control/lib/lexical/remote_control/build/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ defmodule Lexical.RemoteControl.Build.Error do
mfa = {module, function, arity}
mfa_to_position(mfa, quoted_ast)
else
stack_to_position(stack)
Location.stack_to_position(stack)
end

Result.new(source.uri, position, message, :error, @elixir_source)
Expand Down Expand Up @@ -182,7 +182,7 @@ defmodule Lexical.RemoteControl.Build.Error do
if pipe_or_struct? or expanding? do
Location.context_to_position(context)
else
stack_to_position(stack)
Location.stack_to_position(stack)
end

Result.new(source.uri, position, message, :error, @elixir_source)
Expand All @@ -195,7 +195,7 @@ defmodule Lexical.RemoteControl.Build.Error do
ExUnit.DuplicateDescribeError
] do
message = Exception.message(exception)
position = stack_to_position(stack)
position = Location.stack_to_position(stack)
Result.new(source.uri, position, message, :error, @elixir_source)
end

Expand Down Expand Up @@ -281,20 +281,6 @@ defmodule Lexical.RemoteControl.Build.Error do
end
end

defp stack_to_position([{_, target, _, _} | rest])
when target not in [:__FILE__, :__MODULE__] do
stack_to_position(rest)
end

defp stack_to_position([{_, target, _, context} | _rest])
when target in [:__FILE__, :__MODULE__] do
Location.context_to_position(context)
end

defp stack_to_position([]) do
nil
end

defp do_message_to_diagnostic(_, "") do
nil
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ defmodule Lexical.RemoteControl.Build.Error.Location do

require Logger

def stack_to_position([{_, target, _, _} | rest])
when target not in [:__FILE__, :__MODULE__] do
stack_to_position(rest)
end

def stack_to_position([{_, target, _, context} | _rest])
when target in [:__FILE__, :__MODULE__] do
context_to_position(context)
end

def stack_to_position([]) do
nil
end

def context_to_position(context) do
case {context[:line], context[:column]} do
{nil, nil} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ defmodule Lexical.RemoteControl.Build.Document.Compilers.ConfigTest do
assert result.source == "Elixir"
end

test "it produces diagnostics for arbitrary exceptions" do
assert {:error, [result]} =
~q[
import Config

System.fetch_env!("_LEXICAL_NON_EXISTING_ENV_VAR_")
]
|> document()
|> compile()

assert result.message =~ "could not fetch environment variable"
assert result.severity == :error
assert result.source == "Elixir"

if Features.with_diagnostics?() do
assert result.position == 3
end
end

test "it produces no diagnostics on success" do
assert {:ok, []} =
~q[
Expand Down
1 change: 0 additions & 1 deletion integration/boot/set_up_mise.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ chmod +x ./mise
eval "$(./mise activate bash)"

export KERL_CONFIGURE_OPTIONS="--disable-debug --without-javac --without-termcap --without-wx"
./mise plugin install -y erlang
./mise use --global "erlang@$ERLANG_VERSION"

./mise plugins install -y elixir
Expand Down
Loading