Skip to content

Commit

Permalink
[#107] Improved error messages on file loading and rule applying.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Sep 15, 2014
1 parent 01a0a43 commit 9eb8061
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
45 changes: 27 additions & 18 deletions src/elvis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,13 @@ rock(Config) ->
lists:foldl(fun combine_results/2, ok, Results).

%% @private
-spec do_rock(elvis_config:config()) -> ok | {fail, [elvis_result:file()]}.
do_rock(Config0) ->
elvis_utils:info("Loading files..."),
Config = elvis_config:resolve_files(Config0),
Files = elvis_config:files(Config),
Fun = fun (File) ->
Path = elvis_file:path(File),
elvis_utils:info("Loading ~s", [Path]),
elvis_file:load_file_data(Config, File)
end,
Fun = fun (File) -> load_file_data(Config, File) end,
LoadedFiles = lists:map(Fun, Files),

elvis_utils:info("Applying rules..."),
Results = [apply_rules(Config, File) || File <- LoadedFiles],

Expand All @@ -74,6 +70,21 @@ do_rock(Config0) ->
ok -> ok
end.

%% @private
-spec load_file_data(elvis_config:config(), elvis_file:file()) ->
elvis_file:file().
load_file_data(Config, File) ->
Path = elvis_file:path(File),
elvis_utils:info("Loading ~s", [Path]),
try
elvis_file:load_file_data(Config, File)
catch
_:Reason ->
Msg = "~p when loading file ~p.",
elvis_utils:error_prn(Msg, [Reason, Path]),
File
end.

%%% Git-Hook Command

-spec git_hook(elvis_config:config()) -> ok.
Expand Down Expand Up @@ -124,18 +135,16 @@ apply_rules(Config, File) ->
elvis_result:print(Results),
Results.

apply_rule({Module, Function, Args}, Acc = {Result, Config, File}) ->
try
Results = Module:Function(Config, File, Args),
RuleResult = elvis_result:new(rule, Function, Results),

{[RuleResult | Result], Config, File}
catch
_:Reason ->
Msg = "~p while applying rule '~p'",
elvis_utils:error_prn(Msg, [Function, Reason]),
Acc
end.
apply_rule({Module, Function, Args}, {Result, Config, File}) ->
RuleResult = try
Results = Module:Function(Config, File, Args),
elvis_result:new(rule, Function, Results)
catch
_:Reason ->
Msg = "'~p' while applying rule '~p'.",
elvis_result:new(error, Msg, [Reason, Function])
end,
{[RuleResult | Result], Config, File}.

%%% Command Line Interface

Expand Down
13 changes: 9 additions & 4 deletions src/elvis_result.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ new(item, Msg, Info) ->
new(rule, Name, Results) ->
#{name => Name, items => Results};
new(file, File, Rules) ->
#{file => File, rules => Rules}.
#{file => File, rules => Rules};
new(error, Msg, Info) ->
#{error_msg => Msg, info => Info}.

-spec new(item, term(), any(), any()) -> item().
new(item, Msg, Info, LineNum) ->
Expand Down Expand Up @@ -106,15 +108,18 @@ print(#{file := File, rules := Rules}) ->

elvis_utils:notice("# ~s [~s{{white-bold}}]", [Path, Status]),
print(Rules);

%% Rule
print(#{items := []}) ->
ok;
print(#{name := Name, items := Items}) ->
elvis_utils:notice(" - ~s", [atom_to_list(Name)]),
print(Items);

%% Item
print(#{message := Msg, info := Info}) ->
elvis_utils:notice(" - " ++ Msg, Info).
elvis_utils:notice(" - " ++ Msg, Info);
%% Error
print(#{error_msg := Msg, info := Info}) ->
elvis_utils:error_prn(Msg, Info).

-spec status([rule()]) -> ok | fail.
status([]) ->
Expand Down

0 comments on commit 9eb8061

Please sign in to comment.