Skip to content

Commit

Permalink
Improve shell --eval error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
choptastic committed Jul 17, 2022
1 parent a16c8b8 commit d35ca08
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/rebar_prv_shell.erl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ do(Config) ->
-spec format_error(any()) -> iolist().
format_error({unknown_app, Unknown}) ->
io_lib:format("Applications list for shell contains an unrecognizable application definition: ~p", [Unknown]);
format_error({eval_parse, Exp, Msg}) ->
io_lib:format("Failed to parse -eval expression: \"~ts\". Error message: ~ts", [Exp, Msg]);
format_error({eval_exprs, Exp, {C, E, S}}) ->
?DEBUG("--eval failure details: ~p:~p~nStacktrace: ~p", [C, E, S]),
io_lib:format("Failed to evaluate expression: \"~ts\". Error: ~p:~p. Run with DIAGNOSTIC=1 to stacktrace or consult rebar3.crashdump", [Exp, C, E]);
format_error(Reason) ->
io_lib:format("~p", [Reason]).

Expand Down Expand Up @@ -344,7 +349,7 @@ maybe_run_eval(State) ->
Exprs = find_evals_to_run(State),
lists:map(fun(Expr) ->
?INFO("Evaluating: ~p",[Expr]),
eval(Expr)
{ok, _} = eval(Expr)
end, Exprs).

find_evals_to_run(State) ->
Expand All @@ -354,9 +359,18 @@ find_evals_to_run(State) ->

eval(Expression) ->
{ok, Tokens, _} = erl_scan:string(Expression),
{ok, Parsed} = erl_parse:parse_exprs(Tokens),
{value, Result, _} = erl_eval:exprs(Parsed, []),
Result.
case erl_parse:parse_exprs(Tokens) of
{error, {_, _, Msg}} ->
throw(?PRV_ERROR({eval_parse, Expression, Msg}));
{ok, Parsed} ->
try erl_eval:exprs(Parsed, []) of
{value, Result, _} ->
{ok, Result}
catch
C:E:S ->
throw(?PRV_ERROR({eval_exprs, Expression, {C, E, S}}))
end
end.

setup_name(State) ->
{Long, Short, Opts} = rebar_dist_utils:find_options(State),
Expand Down

0 comments on commit d35ca08

Please sign in to comment.