From 7bd06ece42c313bad470c16b45508b0d200b05a5 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Sun, 21 Jan 2024 17:00:30 +0100 Subject: [PATCH] log errors --- .../core/normalized/code/fragment.ex | 19 ++++++++++-- lib/elixir_sense/core/normalized/tokenizer.ex | 7 ++++- lib/elixir_sense/core/normalized/typespec.ex | 19 ++++++++++-- lib/elixir_sense/core/type_info.ex | 31 ++++++++++++++++--- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/lib/elixir_sense/core/normalized/code/fragment.ex b/lib/elixir_sense/core/normalized/code/fragment.ex index d20d50db..23b5655e 100644 --- a/lib/elixir_sense/core/normalized/code/fragment.ex +++ b/lib/elixir_sense/core/normalized/code/fragment.ex @@ -1,5 +1,6 @@ defmodule ElixirSense.Core.Normalized.Code.Fragment do @moduledoc false + require Logger def cursor_context(string, opts \\ []) do cond do @@ -15,8 +16,12 @@ defmodule ElixirSense.Core.Normalized.Code.Fragment do ]) end rescue - e in CaseClauseError -> + e -> if Version.match?(System.version(), ">= 1.16.0-dev") do + Logger.error( + "Code.Fragment.cursor_context raised #{Exception.blame(:error, e, __STACKTRACE__)}. Please report that to elixir project." + ) + reraise e, __STACKTRACE__ else :none @@ -38,8 +43,12 @@ defmodule ElixirSense.Core.Normalized.Code.Fragment do ]) end rescue - e in CaseClauseError -> + e -> if Version.match?(System.version(), ">= 1.16.0-dev") do + Logger.error( + "Code.Fragment.surround_context raised #{Exception.blame(:error, e, __STACKTRACE__)}. Please report that to elixir project." + ) + reraise e, __STACKTRACE__ else :none @@ -61,8 +70,12 @@ defmodule ElixirSense.Core.Normalized.Code.Fragment do ) end rescue - e in MatchError -> + e -> if Version.match?(System.version(), ">= 1.16.0-dev") do + Logger.error( + "Code.Fragment.container_cursor_to_quoted raised #{Exception.blame(:error, e, __STACKTRACE__)}. Please report that to elixir project." + ) + reraise e, __STACKTRACE__ else {:error, {[line: 1, column: 1], "", ""}} diff --git a/lib/elixir_sense/core/normalized/tokenizer.ex b/lib/elixir_sense/core/normalized/tokenizer.ex index acc2b359..2c1f8a2b 100644 --- a/lib/elixir_sense/core/normalized/tokenizer.ex +++ b/lib/elixir_sense/core/normalized/tokenizer.ex @@ -4,6 +4,7 @@ defmodule ElixirSense.Core.Normalized.Tokenizer do Uses private api :elixir_tokenizer """ + require Logger @spec tokenize(String.t()) :: [tuple] def tokenize(prefix) do @@ -30,8 +31,12 @@ defmodule ElixirSense.Core.Normalized.Tokenizer do sofar end rescue - e in CaseClauseError -> + e -> if Version.match?(System.version(), ">= 1.16.0-dev") do + Logger.error( + ":elixir_tokenizer.tokenize raised #{Exception.blame(:error, e, __STACKTRACE__)}. Please report that to elixir project." + ) + reraise e, __STACKTRACE__ else [] diff --git a/lib/elixir_sense/core/normalized/typespec.ex b/lib/elixir_sense/core/normalized/typespec.ex index 3ab94353..bd5266f3 100644 --- a/lib/elixir_sense/core/normalized/typespec.ex +++ b/lib/elixir_sense/core/normalized/typespec.ex @@ -2,17 +2,22 @@ defmodule ElixirSense.Core.Normalized.Typespec do @moduledoc """ A module wrapping internal Elixir Code.Typespec APIs """ + require Logger @spec get_specs(module) :: [tuple] def get_specs(module) do get_module().fetch_specs(module) |> extract_specs rescue - e in FunctionClauseError -> + e -> # workaround for crash # Keyword.fetch({:error, :beam_lib, {:not_a_beam_file, ""}}, :module) # fixed in elixir 1.16.0 if Version.match?(System.version(), ">= 1.16.0-dev") do + Logger.error( + "Code.Typespec.fetch_specs raised #{Exception.blame(:error, e, __STACKTRACE__)}. Please report that to elixir project." + ) + reraise e, __STACKTRACE__ else [] @@ -24,11 +29,15 @@ defmodule ElixirSense.Core.Normalized.Typespec do get_module().fetch_types(module) |> extract_specs rescue - e in FunctionClauseError -> + e -> # workaround for crash # Keyword.fetch({:error, :beam_lib, {:not_a_beam_file, ""}}, :module) # fixed in elixir 1.16.0 if Version.match?(System.version(), ">= 1.16.0-dev") do + Logger.error( + "Code.Typespec.fetch_types raised #{Exception.blame(:error, e, __STACKTRACE__)}. Please report that to elixir project." + ) + reraise e, __STACKTRACE__ else [] @@ -40,11 +49,15 @@ defmodule ElixirSense.Core.Normalized.Typespec do get_module().fetch_callbacks(module) |> extract_specs rescue - e in FunctionClauseError -> + e -> # workaround for crash # Keyword.fetch({:error, :beam_lib, {:not_a_beam_file, ""}}, :module) # fixed in elixir 1.16.0 if Version.match?(System.version(), ">= 1.16.0-dev") do + Logger.error( + "Code.Typespec.fetch_callbacks raised #{Exception.blame(:error, e, __STACKTRACE__)}. Please report that to elixir project." + ) + reraise e, __STACKTRACE__ else [] diff --git a/lib/elixir_sense/core/type_info.ex b/lib/elixir_sense/core/type_info.ex index 2d51f1fd..f13db6e3 100644 --- a/lib/elixir_sense/core/type_info.ex +++ b/lib/elixir_sense/core/type_info.ex @@ -9,6 +9,8 @@ defmodule ElixirSense.Core.TypeInfo do alias ElixirSense.Core.Source alias ElixirSense.Core.TypeAst + require Logger + @doc_spec_line_length 75 @param_option_spec_line_length 35 @@ -214,11 +216,30 @@ defmodule ElixirSense.Core.TypeInfo do {sanitized, original} = sanitize_type_name(spec_ast) - sanitized - |> Macro.to_string() - |> (&"@#{kind} #{&1}").() - |> Code.format_string!(line_length: line_length) - |> to_string() + string = + sanitized + |> Macro.to_string() + |> (&"@#{kind} #{&1}").() + + string = + try do + string + |> Code.format_string!(line_length: line_length) + |> to_string() + rescue + e -> + if Version.match?(System.version(), ">= 1.16.0-dev") do + Logger.error( + "Macro.to_string(#{inspect(sanitized)}) returned invalid code. Please report that to elixir project." + ) + + reraise e, __STACKTRACE__ + else + string + end + end + + string |> String.replace("__replace_me__", "#{original}") |> Source.split_lines() |> Enum.with_index()