From 04ea67a7eded3cc296e27ce1a9ae766a638035df Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Wed, 22 Oct 2014 21:16:55 -0500 Subject: use providers PRV_ERROR and format_error/2 --- include/relx.hrl | 4 ++-- src/relx.erl | 26 +++++++++++++------------- src/rlx_app_discovery.erl | 38 +++++++++++++++++++------------------- src/rlx_prv_assembler.erl | 4 ++-- src/rlx_prv_overlay.erl | 24 ++++++++++++------------ src/rlx_prv_release.erl | 1 + src/rlx_prv_relup.erl | 4 ++-- src/rlx_release.erl | 14 +++++++------- src/rlx_state.erl | 1 + src/rlx_topo.erl | 6 +++--- 10 files changed, 62 insertions(+), 60 deletions(-) diff --git a/include/relx.hrl b/include/relx.hrl index 3194873..cccd7bb 100644 --- a/include/relx.hrl +++ b/include/relx.hrl @@ -16,6 +16,7 @@ %% -include_lib("erlware_commons/include/ec_cmd_log.hrl"). +-include_lib("providers/include/providers.hrl"). -define(RLX_ERROR, ?EC_ERROR). -define(RLX_WARN, ?EC_WARN). @@ -26,5 +27,4 @@ %% system. It is expected that everything that returns an error use %% this and that they all expose a format_error/1 message that returns %% an iolist. --define(RLX_ERROR(Reason), - {error, {?MODULE, Reason}}). +-define(RLX_ERROR(Reason), ?PRV_ERROR(Reason)). diff --git a/src/relx.erl b/src/relx.erl index 37918be..cf0b367 100644 --- a/src/relx.erl +++ b/src/relx.erl @@ -26,7 +26,7 @@ do/7, do/8, do/9, - format_error/1, + format_error/2, opt_spec_list/0]). -export_type([error/0]). @@ -215,18 +215,18 @@ opt_spec_list() -> {version, undefined, "version", undefined, "Print relx version"}, {root_dir, $r, "root", string, "The project root directory"}]. --spec format_error(Reason::term()) -> string(). -format_error({invalid_return_value, Provider, Value}) -> +-spec format_error(Reason::term(), rlx_state:t()) -> string(). +format_error({invalid_return_value, Provider, Value}, _) -> io_lib:format(lists:flatten([providers:format(Provider), " returned an invalid value ", - io_lib:format("~p", [Value])]), []); -format_error({opt_parse, {invalid_option, Opt}}) -> + io_lib:format("~p", [Value])]), []); +format_error({opt_parse, {invalid_option, Opt}}, _) -> io_lib:format("invalid option ~s~n", [Opt]); -format_error({opt_parse, Arg}) -> +format_error({opt_parse, Arg}, _) -> io_lib:format("~p~n", [Arg]); -format_error({error, {relx, Reason}}) -> - format_error(Reason); -format_error({error, {Module, Reason}}) -> - io_lib:format("~s~n", [Module:format_error(Reason)]). +format_error({error, {relx, Reason}}, State) -> + format_error(Reason, State); +format_error({error, {Module, Reason}}, State) -> + io_lib:format("~s~n", [Module:format_error(Reason, State)]). %%============================================================================ %% internal api @@ -303,13 +303,13 @@ usage() -> report_error(State, Error) -> case Error of {error, {relx, {opt_parse, _}}} -> - io:format(standard_error, format_error(Error), []), + io:format(standard_error, format_error(Error, State), []), usage(); {error, {rlx_cmd_args, _}} -> - io:format(standard_error, format_error(Error), []), + io:format(standard_error, format_error(Error, State), []), usage(); _ -> - io:format(standard_error, format_error(Error), []) + io:format(standard_error, format_error(Error, State), []) end, case rlx_state:caller(State) of command_line -> diff --git a/src/rlx_app_discovery.erl b/src/rlx_app_discovery.erl index 6ac8d11..2c5714b 100644 --- a/src/rlx_app_discovery.erl +++ b/src/rlx_app_discovery.erl @@ -25,7 +25,7 @@ -module(rlx_app_discovery). -export([do/2, - format_error/1]). + format_error/2]). -include("relx.hrl"). @@ -44,10 +44,10 @@ do(State, LibDirs) -> end), resolve_app_metadata(State, LibDirs). --spec format_error([ErrorDetail::term()]) -> iolist(). -format_error(ErrorDetails) +-spec format_error([ErrorDetail::term()], rlx_state:t()) -> iolist(). +format_error(ErrorDetails, State) when erlang:is_list(ErrorDetails) -> - [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails]. + [[format_detail(ErrorDetail, State), "\n"] || ErrorDetail <- ErrorDetails]. %%%=================================================================== %%% Internal Functions @@ -85,10 +85,10 @@ get_app_metadata(State, LibDirs) -> {ok, _} = AppMeta -> [AppMeta|Acc]; {warning, W} -> - ec_cmd_log:warn(rlx_state:log(State), format_detail(W)), + ec_cmd_log:warn(rlx_state:log(State), format_detail(W, State)), Acc; {error, E} -> - ec_cmd_log:error(rlx_state:log(State), format_detail(E)), + ec_cmd_log:error(rlx_state:log(State), format_detail(E, State)), Acc; _ -> Acc @@ -111,7 +111,7 @@ resolve_app_metadata(State, LibDirs) -> {error, _} -> true; {warning, W} -> - ec_cmd_log:warn(rlx_state:log(State), format_detail(W)), + ec_cmd_log:warn(rlx_state:log(State), format_detail(W, State)), false; _ -> false @@ -155,30 +155,30 @@ resolve_override(AppName, FileName0) -> {ok, rlx_app_info:link(App, true)} end. --spec format_detail(ErrorDetail::term()) -> iolist(). -format_detail({missing_beam_file, Module, BeamFile}) -> +-spec format_detail(ErrorDetail::term(), rlx_state:t()) -> iolist(). +format_detail({missing_beam_file, Module, BeamFile}, _) -> io_lib:format("Missing beam file ~p ~p", [Module, BeamFile]); -format_detail({error, {invalid_override, AppName, FileName}}) -> +format_detail({error, {invalid_override, AppName, FileName}}, _) -> io_lib:format("Override {~p, ~p} is not a valid OTP App. Perhaps you forgot to build it?", [AppName, FileName]); -format_detail({accessing, File, eaccess}) -> +format_detail({accessing, File, eaccess}, _) -> io_lib:format("permission denied accessing file ~s", [File]); -format_detail({accessing, File, Type}) -> +format_detail({accessing, File, Type}, _) -> io_lib:format("error (~p) accessing file ~s", [Type, File]); -format_detail({no_beam_files, EbinDir}) -> +format_detail({no_beam_files, EbinDir}, _) -> io_lib:format("no beam files found in directory ~s", [EbinDir]); -format_detail({not_a_directory, EbinDir}) -> +format_detail({not_a_directory, EbinDir}, _) -> io_lib:format("~s is not a directory when it should be a directory", [EbinDir]); -format_detail({unable_to_load_app, AppDir, _}) -> +format_detail({unable_to_load_app, AppDir, _}, _) -> io_lib:format("Unable to load the application metadata from ~s", [AppDir]); -format_detail({invalid_app_file, File}) -> +format_detail({invalid_app_file, File}, _) -> io_lib:format("Application metadata file exists but is malformed: ~s", [File]); -format_detail({unversioned_app, AppDir, _AppName}) -> +format_detail({unversioned_app, AppDir, _AppName}, _) -> io_lib:format("Application metadata exists but version is not available: ~s", [AppDir]); -format_detail({app_info_error, {Module, Detail}}) -> - Module:format_error(Detail). +format_detail({app_info_error, {Module, Detail}}, State) -> + Module:format_error(Detail, State). -spec discover_dir([file:name()], directory | file) -> {ok, rlx_app_info:t()} | {error, Reason::term()}. diff --git a/src/rlx_prv_assembler.erl b/src/rlx_prv_assembler.erl index 7ef655b..c535485 100644 --- a/src/rlx_prv_assembler.erl +++ b/src/rlx_prv_assembler.erl @@ -89,9 +89,9 @@ format_error({release_script_generation_warning, Module, Warnings}, _) -> format_error({unable_to_create_output_dir, OutputDir}, _) -> io_lib:format("Unable to create output directory (possible permissions issue): ~s", [OutputDir]); -format_error({release_script_generation_error, Module, Errors}, _) -> +format_error({release_script_generation_error, Module, Errors}, State) -> ["Errors generating release \n", - rlx_util:indent(2), Module:format_error(Errors)]; + rlx_util:indent(2), Module:format_error(Errors, State)]; format_error({unable_to_make_symlink, AppDir, TargetDir, Reason}, _) -> io_lib:format("Unable to symlink directory ~s to ~s because \n~s~s", [AppDir, TargetDir, rlx_util:indent(2), diff --git a/src/rlx_prv_overlay.erl b/src/rlx_prv_overlay.erl index cfdaf77..6df142b 100644 --- a/src/rlx_prv_overlay.erl +++ b/src/rlx_prv_overlay.erl @@ -96,9 +96,9 @@ format_error({unable_to_enclosing_dir, ToFile, Reason}, _) -> format_error({unable_to_render_template, FromFile, Reason}, _) -> io_lib:format("Unable to render template ~s because ~p", [FromFile, Reason]); -format_error({unable_to_compile_template, FromFile, Reason}, _) -> +format_error({unable_to_compile_template, FromFile, Reason}, State) -> io_lib:format("Unable to compile template ~s because \n~s", - [FromFile, [format_errors(F, Es) || {F, Es} <- Reason]]); + [FromFile, [format_errors(F, Es, State) || {F, Es} <- Reason]]); format_error({unable_to_make_dir, Absolute, Error}, _) -> io_lib:format("Unable to make directory ~s because ~p", [Absolute, Error]). @@ -107,22 +107,22 @@ format_error({unable_to_make_dir, Absolute, Error}, _) -> %%% Internal Functions %%%=================================================================== -format_errors(File, [{none, Mod, E}|Es]) -> +format_errors(File, [{none, Mod, E}|Es], State) -> [io_lib:format("~s~s: ~ts~n", [rlx_util:indent(2), File, - Mod:format_error(E)]) - |format_errors(File, Es)]; -format_errors(File, [{{Line, Col}, Mod, E}|Es]) -> + Mod:format_error(E, State)]) + |format_errors(File, Es, State)]; +format_errors(File, [{{Line, Col}, Mod, E}|Es], State) -> [io_lib:format("~s~s:~w:~w: ~ts~n", [rlx_util:indent(2), File, Line, Col, - Mod:format_error(E)]) - |format_errors(File, Es)]; -format_errors(File, [{Line, Mod, E}|Es]) -> + Mod:format_error(E, State)]) + |format_errors(File, Es, State)]; +format_errors(File, [{Line, Mod, E}|Es], State) -> [io_lib:format("~s~s:~w: ~ts~n", [rlx_util:indent(2), File, Line, - Mod:format_error(E)]) - |format_errors(File, Es)]; -format_errors(_, []) -> []. + Mod:format_error(E, State)]) + |format_errors(File, Es, State)]; +format_errors(_, [], _State) -> []. -spec generate_overlay_vars(rlx_state:t(), rlx_release:t()) -> diff --git a/src/rlx_prv_release.erl b/src/rlx_prv_release.erl index d51fc07..bd5acc4 100644 --- a/src/rlx_prv_release.erl +++ b/src/rlx_prv_release.erl @@ -78,6 +78,7 @@ format_error({release_not_found, {RelName, RelVsn}}, _) -> format_error({failed_solve, Error}, _) -> io_lib:format("Failed to solve release:\n ~s", [rlx_depsolver:format_error({error, Error})]). + %%%=================================================================== %%% Internal Functions %%%=================================================================== diff --git a/src/rlx_prv_relup.erl b/src/rlx_prv_relup.erl index 3564dd0..df6f831 100644 --- a/src/rlx_prv_relup.erl +++ b/src/rlx_prv_relup.erl @@ -70,9 +70,9 @@ format_error({relup_script_generation_error, {missing_sasl, _}}}, _) -> "Unfortunately, due to requirements in systools, you need to have the sasl application " "in both the current release and the release to upgrade from."; -format_error({relup_script_generation_error, Module, Errors}, _) -> +format_error({relup_script_generation_error, Module, Errors}, State) -> ["Errors generating relup \n", - rlx_util:indent(2), Module:format_error(Errors)]. + rlx_util:indent(2), Module:format_error(Errors, State)]. make_relup(State, Release) -> Vsn = rlx_state:upfrom(State), diff --git a/src/rlx_release.erl b/src/rlx_release.erl index 1d333bf..7b82119 100644 --- a/src/rlx_release.erl +++ b/src/rlx_release.erl @@ -41,7 +41,7 @@ canonical_name/1, format/1, format/2, - format_error/1]). + format_error/2]). -export_type([t/0, name/0, @@ -217,14 +217,14 @@ format_goal({Constraint, AppType, AppInc}) -> format_goal(Constraint) -> rlx_depsolver:format_constraint(Constraint). --spec format_error(Reason::term()) -> iolist(). -format_error({topo_error, E}) -> - rlx_topo:format_error(E); -format_error({failed_to_parse, Con}) -> +-spec format_error(Reason::term(), rlx_state:t()) -> iolist(). +format_error({topo_error, E}, State) -> + rlx_topo:format_error(E, State); +format_error({failed_to_parse, Con}, _) -> io_lib:format("Failed to parse constraint ~p", [Con]); -format_error({invalid_constraint, _, Con}) -> +format_error({invalid_constraint, _, Con}, _) -> io_lib:format("Invalid constraint specified ~p", [Con]); -format_error({not_realized, Name, Vsn}) -> +format_error({not_realized, Name, Vsn}, _) -> io_lib:format("Unable to produce metadata release: ~p-~s has not been realized", [Name, Vsn]). diff --git a/src/rlx_state.erl b/src/rlx_state.erl index a25c60f..550a44a 100644 --- a/src/rlx_state.erl +++ b/src/rlx_state.erl @@ -126,6 +126,7 @@ %%============================================================================ %% API %%============================================================================ + -spec new(string(), undefined | [atom()]) -> t() | relx:error(). new(Config, Targets) -> new(Config, [], Targets). diff --git a/src/rlx_topo.erl b/src/rlx_topo.erl index d24f227..1d5de7e 100644 --- a/src/rlx_topo.erl +++ b/src/rlx_topo.erl @@ -34,7 +34,7 @@ -export([sort/1, sort_apps/1, - format_error/1]). + format_error/2]). -include("relx.hrl"). @@ -72,8 +72,8 @@ sort(Pairs) -> iterate(Pairs, [], all(Pairs)). %% @doc nicely format the error from the sort. --spec format_error(Reason::term()) -> iolist(). -format_error({cycle, Pairs}) -> +-spec format_error(Reason::term(), rlx_state:t()) -> iolist(). +format_error({cycle, Pairs}, _) -> ["Cycle detected in dependency graph, this must be resolved " "before we can continue:\n", case Pairs of -- cgit v1.2.3