Skip to content

Commit

Permalink
log errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Jan 21, 2024
1 parent 430bb2e commit 7bd06ec
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
19 changes: 16 additions & 3 deletions lib/elixir_sense/core/normalized/code/fragment.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule ElixirSense.Core.Normalized.Code.Fragment do
@moduledoc false
require Logger

def cursor_context(string, opts \\ []) do
cond do
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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], "", ""}}
Expand Down
7 changes: 6 additions & 1 deletion lib/elixir_sense/core/normalized/tokenizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
[]
Expand Down
19 changes: 16 additions & 3 deletions lib/elixir_sense/core/normalized/typespec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
[]
Expand All @@ -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
[]
Expand All @@ -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
[]
Expand Down
31 changes: 26 additions & 5 deletions lib/elixir_sense/core/type_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7bd06ec

Please sign in to comment.