Format runtime errors consistently with assertion errors
This commit is contained in:
@@ -2,18 +2,31 @@ defmodule Display do
|
||||
alias IO.ANSI
|
||||
@current_dir File.cwd!
|
||||
|
||||
def show_failure(%{expr: expr}, module, name) do
|
||||
def show_failure(failure, module, name) do
|
||||
clear_screen()
|
||||
|
||||
IO.puts("Now meditate upon #{display_module(module)}")
|
||||
IO.puts("Now meditate upon #{format_module(module)}")
|
||||
IO.puts("---------------------------------------")
|
||||
IO.puts(format_cyan(last_failure_location))
|
||||
IO.puts(name)
|
||||
IO.puts(format_red(Macro.to_string(expr)))
|
||||
IO.puts(format_failure(failure))
|
||||
end
|
||||
|
||||
def format_failure(%ExUnit.AssertionError{expr: expr}) do
|
||||
"""
|
||||
#{format_cyan("Assertion failed in #{last_failure_location}")}
|
||||
#{format_red(Macro.to_string(expr))}
|
||||
"""
|
||||
end
|
||||
|
||||
def format_failure(error) do
|
||||
"""
|
||||
#{format_cyan("Error in #{last_failure_location}")}
|
||||
#{format_error(error)}
|
||||
"""
|
||||
end
|
||||
|
||||
def considering(module) do
|
||||
IO.puts("Considering #{display_module(module)}...")
|
||||
IO.puts("Considering #{format_module(module)}...")
|
||||
end
|
||||
|
||||
def clear_screen() do
|
||||
@@ -29,7 +42,7 @@ defmodule Display do
|
||||
|> List.first
|
||||
|> extract_file_and_line
|
||||
|
||||
"Assertion failed in #{file}:#{line}"
|
||||
"#{file}:#{line}"
|
||||
end
|
||||
|
||||
defp in_ex_unit?({ExUnit.Assertions, _, _, _}), do: true
|
||||
@@ -40,8 +53,12 @@ defmodule Display do
|
||||
end
|
||||
|
||||
def format_compile_error(error) do
|
||||
format_error(error) |> IO.puts
|
||||
end
|
||||
|
||||
def format_error(error) do
|
||||
trace = System.stacktrace |> Enum.take(2)
|
||||
IO.puts(format_red(Exception.format(:error, error, trace)))
|
||||
format_red(Exception.format(:error, error, trace))
|
||||
end
|
||||
|
||||
defp format_red(str) do
|
||||
@@ -52,7 +69,7 @@ defmodule Display do
|
||||
Enum.join([ANSI.cyan, str, ANSI.reset], "")
|
||||
end
|
||||
|
||||
defp display_module(module) do
|
||||
defp format_module(module) do
|
||||
Module.split(module) |> List.last
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,7 +8,7 @@ defmodule Koans do
|
||||
unquote(body)
|
||||
:ok
|
||||
rescue
|
||||
e in ExUnit.AssertionError -> e
|
||||
e in _ -> e
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user