From 933e11acb9cca432b54e1050b616175b20cfb832 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 13 Jan 2016 13:31:04 +0000 Subject: [PATCH] Intercept and print compile errors in a nicer way. --- lib/display.ex | 5 +++++ lib/watcher.ex | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/display.ex b/lib/display.ex index 7c64c0f..7b58519 100644 --- a/lib/display.ex +++ b/lib/display.ex @@ -24,6 +24,11 @@ defmodule Display do "Assertion failed in #{source_file(module)}:#{line_number(expr)}" end + def format_compile_error(error) do + trace = System.stacktrace |> Enum.take(2) + IO.puts(format_red(Exception.format(:error, error, trace))) + end + defp line_number({_, [line: line], _}) do line end diff --git a/lib/watcher.ex b/lib/watcher.ex index ce1baef..a33cdeb 100644 --- a/lib/watcher.ex +++ b/lib/watcher.ex @@ -3,8 +3,12 @@ defmodule Watcher do def callback(file, events) do if Enum.member?(events, :modified) do - [{mod, _}] = Code.load_file(file) - Runner.run(mod) + try do + [{mod, _}] = Code.load_file(file) + Runner.run(mod) + rescue + e -> Display.format_compile_error(e) + end end end end