Skip to content

Commit

Permalink
Output more context on error (#60)
Browse files Browse the repository at this point in the history
* Fix our spec.s as per elvis_core and output more context on error

The test is a dumb one, not unlike the prior version of it,
but it makes sure we don't crash on function call (mimics
rebar3) and on io:format'ing it

* Bump elvis_core from 3.2.0 to 3.2.1
  • Loading branch information
paulo-ferraz-oliveira authored Jan 18, 2024
1 parent bd5fbd4 commit b040918
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

%% == Dependencies and plugins ==

{deps, [{elvis_core, "3.2.0"}]}.
{deps, [{elvis_core, "3.2.1"}]}.

{project_plugins,
[{rebar3_hank, "~> 1.4.0"},
Expand Down
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{"1.2.0",
[{<<"elvis_core">>,{pkg,<<"elvis_core">>,<<"3.2.0">>},0},
[{<<"elvis_core">>,{pkg,<<"elvis_core">>,<<"3.2.1">>},0},
{<<"katana_code">>,{pkg,<<"katana_code">>,<<"2.1.0">>},1},
{<<"zipper">>,{pkg,<<"zipper">>,<<"1.0.1">>},1}]}.
[
{pkg_hash,[
{<<"elvis_core">>, <<"1B8DD3190903ED7520D03DD53E7535574CFC7354C89FA580BE8FD3F65A70C248">>},
{<<"elvis_core">>, <<"D5EFDBC55D14521BAB9337FA6822EB41085377BDB1059B2E6794333F40AE4DC1">>},
{<<"katana_code">>, <<"0C42BDCD7E59995876AED9F678CF62E3D12EF42E0FBB2190556E64BFEBDD15C6">>},
{<<"zipper">>, <<"3CCB4F14B97C06B2749B93D8B6C204A1ECB6FAFC6050CACC3B93B9870C05952A">>}]},
{pkg_hash_ext,[
{<<"elvis_core">>, <<"05EBF8D4840F4A81721907E1DDCE2F3455BE8EA0F0FB280D50C8DE85C40060FA">>},
{<<"elvis_core">>, <<"4F1996776BB53EF35D0CDA0A6799FC14CDB9190BA89F07883AFCA734D464AE3C">>},
{<<"katana_code">>, <<"AE3BBACA187511588F69695A9FF22251CB2CC672FDCCC180289779BDD25175EF">>},
{<<"zipper">>, <<"6A1FD3E1F0CC1D1DF5642C9A0CE2178036411B0A5C9642851D1DA276BD737C2D">>}]}
].
14 changes: 8 additions & 6 deletions src/rebar3_lint_prv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ init(State) ->
{desc, "A rebar linter plugin based on elvis"}]),
{ok, rebar_state:add_provider(State, Provider)}.

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, {string(), any()}}.
do(State) ->
Elvis = get_elvis_config(State),
_ = rebar_log:log(info, "elvis analysis starting, this may take a while...", []),
try elvis_core:rock(Elvis) of
ok ->
{ok, State};
{fail, _} ->
{error, "Linting failed"}
{fail, [{throw, Error} | _]} ->
rebar_api:abort("elvis_core threw an exception: ~p", [Error]);
{fail, Reason} ->
{error, {"Linting failed with ~p", Reason}}
catch
Error ->
rebar_api:abort("elvis_core threw an exception: ~p", [Error])
end.

-spec format_error(any()) -> iolist().
format_error(Reason) ->
io_lib:format("~p", [Reason]).
-spec format_error({string(), any()}) -> iolist().
format_error({Message, Reason}) ->
io_lib:format(Message, [Reason]).

-spec get_elvis_config(rebar_state:t()) -> elvis_config:configs().
get_elvis_config(State) ->
Expand Down
4 changes: 3 additions & 1 deletion test/test_app_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ test_app(_Config) ->
[#{dirs => ["src"],
filter => "*.erl",
rules => [{elvis_style, max_function_length, #{max_length => 1}}]}]),
{error, "Linting failed"} = rebar3_lint_prv:do(BadState).
{error, {Message = "Linting failed with ~p", Reason}} = rebar3_lint_prv:do(BadState),
% Check that this doesn't throw
io:format("~p", [rebar3_lint_prv:format_error({Message, Reason})]).

0 comments on commit b040918

Please sign in to comment.