Skip to content

Commit

Permalink
Move Erlang error handling to a single function
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Jun 6, 2023
1 parent 421578f commit c6aaf91
Showing 1 changed file with 41 additions and 58 deletions.
99 changes: 41 additions & 58 deletions lib/livebook/runtime/evaluator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -720,67 +720,12 @@ defmodule Livebook.Runtime.Evaluator do
{{:ok, result, binding, env}, []}
else
# Tokenizer error
{:error, {begin_loc, module, description}, _end_loc} ->
formatted =
(&module.format_error/1).(description)
|> :erlang.list_to_binary()

code_marker = %{
line: :erl_anno.line(begin_loc),
severity: :error,
description: "Tokenizer error: #{formatted}"
}

error_cons =
case {module, description} do
{:erl_parse, [~c"syntax error before: ", []]} ->
&TokenMissingError.exception/1

_ ->
&SyntaxError.exception/1
end

error =
error_cons.(
file: env.file,
line: :erl_anno.line(begin_loc),
column:
case :erl_anno.column(begin_loc) do
:undefined -> 1
val -> val
end,
description: formatted,
snippet: make_snippet(code, begin_loc)
)

{{:error, :code_error, error, []}, filter_erlang_code_markers([code_marker])}
{:error, {location, module, description}, _end_loc} ->
process_erlang_error(env, code, location, module, description)

# Parser error
{:error, {location, module, description}} ->
description =
(&module.format_error/1).format_error(description)
|> :erlang.list_to_binary()

code_marker = %{
line: :erl_anno.line(location),
severity: :error,
description: "Parser error: #{description}"
}

error =
SyntaxError.exception(
file: env.file,
line: :erl_anno.line(location),
column:
case :erl_anno.column(location) do
:undefined -> 1
val -> val
end,
description: description,
snippet: make_snippet(code, location)
)

{{:error, :error, error, []}, filter_erlang_code_markers([code_marker])}
process_erlang_error(env, code, location, module, description)
end
catch
kind, error ->
Expand All @@ -789,6 +734,44 @@ defmodule Livebook.Runtime.Evaluator do
end
end

defp process_erlang_error(env, code, location, module, description) do
line = :erl_anno.line(location)

formatted =
(&module.format_error/1).(description)
|> :erlang.list_to_binary()

code_marker = %{
line: line,
severity: :error,
description: "#{module}: #{formatted}"
}

error_cons =
case {module, description} do
{:erl_parse, [~c"syntax error before: ", []]} ->
&TokenMissingError.exception/1

_ ->
&SyntaxError.exception/1
end

error =
error_cons.(
file: env.file,
line: line,
column:
case :erl_anno.column(location) do
:undefined -> 1
val -> val
end,
description: formatted,
snippet: make_snippet(code, location)
)

{{:error, :error, error, []}, filter_erlang_code_markers([code_marker])}
end

defp make_snippet(code, location) do
start_line = 1
start_column = 1
Expand Down

0 comments on commit c6aaf91

Please sign in to comment.