erlware / relx

Sane, simple release creation for Erlang
http://erlware.github.io/relx
Apache License 2.0
697 stars 232 forks source link

Improve error reporting with wrong arguments given to relx #743

Open saleyn opened 5 years ago

saleyn commented 5 years ago

I recently ran into an issue that a rebar3's profile was erroneously defined like this:

{profiles, [
  {slave,  [{relx, [{dev_mode, false}, {erl_opts, [debug_info]}]}]}
]}.

As the result, the erl_opts was passed to bbmustach:render/3 by relx, which crashed with an error of this type, when trying to render the extended_bin template:

11> bbmustache:render(Bin, Args, [{key_type, atom}]). 
** exception error: bad argument
     in function  iolist_to_binary/1
        called as iolist_to_binary([debug_info])
     in call from bbmustache:compile_impl/4 (d:/lib/erl-libs/rebar3/_build/default/lib/bbmustache/src/bbmustache.erl, line 233)
     in call from bbmustache:compile/3 (d:/lib/erl-libs/rebar3/_build/default/lib/bbmustache/src/bbmustache.erl, line 218)

This error translates in rlx_prv_assermbler simply as {error, render_failed}. It took me a while to figure out the cause of rebar3 failing to build a release.

I'd like to suggest to add a simple argument check to rlx_util:render/2:

render(Template, Data) when is_binary(Template) ->
    case [O || O = {K,V} <- Data, not is_atom(K) orelse not io_lib:printable_list(V)] of
        [] ->
            case catch bbmustache:render(Template, Data,
                                         [{key_type, atom}]) of
                Bin when is_binary(Bin) -> {ok, Bin};
                _ -> {error, render_failed}
            end;
        BadArgs ->
            {error, {render_bad_args, BadArgs}}
    end.