瀏覽代碼

Merge pull request #420 from tsloughter/case_catch

remove uses of 'case catch'
pull/423/head
Fred Hebert 10 年之前
父節點
當前提交
5d817c4371
共有 3 個文件被更改,包括 49 次插入48 次删除
  1. +30
    -24
      src/rebar3.erl
  2. +14
    -19
      src/rebar_base_compiler.erl
  3. +5
    -5
      src/rebar_prv_xref.erl

+ 30
- 24
src/rebar3.erl 查看文件

@ -44,33 +44,14 @@
%% escript Entry point %% escript Entry point
main(Args) -> main(Args) ->
case catch(run(Args)) of
try run(Args) of
{ok, _State} -> {ok, _State} ->
erlang:halt(0); erlang:halt(0);
rebar_abort ->
erlang:halt(1);
{error, rebar_abort} ->
erlang:halt(1);
{error, {Module, Reason}} ->
case code:which(Module) of
non_existing ->
?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
?DEBUG("Uncaught error: ~p ~p", [Module, Reason]),
?INFO("When submitting a bug report, please include the output of `rebar3 report \"your command\"`", []);
_ ->
?ERROR(Module:format_error(Reason), [])
end,
erlang:halt(1);
{error, Error} when is_list(Error) ->
?ERROR(Error, []),
erlang:halt(1);
Error -> Error ->
%% Nothing should percolate up from rebar_core;
%% Dump this error to console
?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
?DEBUG("Uncaught error: ~p", [Error]),
?INFO("When submitting a bug report, please include the output of `rebar3 report \"your command\"`", []),
erlang:halt(1)
handle_error(Error)
catch
_:Error ->
handle_error(Error)
end. end.
%% Erlang-API entry point %% Erlang-API entry point
@ -251,3 +232,28 @@ global_option_spec_list() ->
%{config, $C, "config", string, "Rebar config file to use."}, %{config, $C, "config", string, "Rebar config file to use."},
{task, undefined, undefined, string, "Task to run."} {task, undefined, undefined, string, "Task to run."}
]. ].
handle_error(rebar_abort) ->
erlang:halt(1);
handle_error({error, rebar_abort}) ->
erlang:halt(1);
handle_error({error, {Module, Reason}}) ->
case code:which(Module) of
non_existing ->
?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
?DEBUG("Uncaught error: ~p ~p", [Module, Reason]),
?INFO("When submitting a bug report, please include the output of `rebar3 report \"your command\"`", []);
_ ->
?ERROR(Module:format_error(Reason), [])
end,
erlang:halt(1);
handle_error({error, Error}) when is_list(Error) ->
?ERROR(Error, []),
erlang:halt(1);
handle_error(Error) ->
%% Nothing should percolate up from rebar_core;
%% Dump this error to console
?ERROR("Uncaught error in rebar_core. Run with DEBUG=1 to see stacktrace", []),
?DEBUG("Uncaught error: ~p", [Error]),
?INFO("When submitting a bug report, please include the output of `rebar3 report \"your command\"`", []),
erlang:halt(1).

+ 14
- 19
src/rebar_base_compiler.erl 查看文件

@ -69,12 +69,12 @@ run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
simple_compile_wrapper(S, Target, Compile3Fn, C, CheckLastMod) simple_compile_wrapper(S, Target, Compile3Fn, C, CheckLastMod)
end). end).
ok_tuple(Config, Source, Ws) ->
{ok, format_warnings(Config, Source, Ws)}.
ok_tuple(_Config, Source, Ws) ->
{ok, format_warnings(Source, Ws)}.
error_tuple(Config, Source, Es, Ws, Opts) ->
{error, format_errors(Config, Source, Es),
format_warnings(Config, Source, Ws, Opts)}.
error_tuple(_Config, Source, Es, Ws, Opts) ->
{error, format_errors(Source, Es),
format_warnings(Source, Ws, Opts)}.
%% =================================================================== %% ===================================================================
%% Internal functions %% Internal functions
@ -114,26 +114,25 @@ compile_each([Source | Rest], Config, CompileFn) ->
skipped -> skipped ->
?DEBUG("~sSkipped ~s", [rebar_utils:indent(1), filename:basename(Source)]); ?DEBUG("~sSkipped ~s", [rebar_utils:indent(1), filename:basename(Source)]);
Error -> Error ->
?ERROR("Compiling ~s failed",
[maybe_absname(Config, Source)]),
?ERROR("Compiling ~s failed", [Source]),
maybe_report(Error), maybe_report(Error),
?DEBUG("Compilation failed: ~p", [Error]), ?DEBUG("Compilation failed: ~p", [Error]),
?FAIL ?FAIL
end, end,
compile_each(Rest, Config, CompileFn). compile_each(Rest, Config, CompileFn).
format_errors(Config, Source, Errors) ->
format_errors(Config, Source, "", Errors).
format_errors(Source, Errors) ->
format_errors(Source, "", Errors).
format_warnings(Config, Source, Warnings) ->
format_warnings(Config, Source, Warnings, []).
format_warnings(Source, Warnings) ->
format_warnings(Source, Warnings, []).
format_warnings(Config, Source, Warnings, Opts) ->
format_warnings(Source, Warnings, Opts) ->
Prefix = case lists:member(warnings_as_errors, Opts) of Prefix = case lists:member(warnings_as_errors, Opts) of
true -> ""; true -> "";
false -> "Warning: " false -> "Warning: "
end, end,
format_errors(Config, Source, Prefix, Warnings).
format_errors(Source, Prefix, Warnings).
maybe_report({{error, {error, _Es, _Ws}=ErrorsAndWarnings}, {source, _}}) -> maybe_report({{error, {error, _Es, _Ws}=ErrorsAndWarnings}, {source, _}}) ->
maybe_report(ErrorsAndWarnings); maybe_report(ErrorsAndWarnings);
@ -148,10 +147,9 @@ maybe_report(_) ->
report(Messages) -> report(Messages) ->
lists:foreach(fun(Msg) -> io:format("~s~n", [Msg]) end, Messages). lists:foreach(fun(Msg) -> io:format("~s~n", [Msg]) end, Messages).
format_errors(Config, _MainSource, Extra, Errors) ->
format_errors(_MainSource, Extra, Errors) ->
[begin [begin
AbsSource = maybe_absname(Config, Source),
[format_error(AbsSource, Extra, Desc) || Desc <- Descs]
[format_error(Source, Extra, Desc) || Desc <- Descs]
end end
|| {Source, Descs} <- Errors]. || {Source, Descs} <- Errors].
@ -164,6 +162,3 @@ format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
format_error(AbsSource, Extra, {Mod, Desc}) -> format_error(AbsSource, Extra, {Mod, Desc}) ->
ErrorDesc = Mod:format_error(Desc), ErrorDesc = Mod:format_error(Desc),
?FMT("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]). ?FMT("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).
maybe_absname(_Config, Filename) ->
Filename.

+ 5
- 5
src/rebar_prv_xref.erl 查看文件

@ -248,11 +248,11 @@ format_mfa_source(MFA) ->
%% Extract an element from a tuple, or undefined if N > tuple size %% Extract an element from a tuple, or undefined if N > tuple size
%% %%
safe_element(N, Tuple) -> safe_element(N, Tuple) ->
case catch(element(N, Tuple)) of
{'EXIT', {badarg, _}} ->
undefined;
Value ->
Value
try
element(N, Tuple)
catch
error:badarg ->
undefined
end. end.
%% %%

Loading…
取消
儲存