Print the error kind in error messages

This commit is contained in:
Ulf Norell
2019-09-06 09:44:41 +02:00
parent 56b77f55fe
commit 9dac134477
4 changed files with 76 additions and 61 deletions
+14 -16
View File
@@ -76,8 +76,7 @@ file(File, Options0) ->
{ok, Bin} -> from_string(Bin, [{src_file, File} | Options]);
{error, Error} ->
Msg = lists:flatten([File,": ",file:format_error(Error)]),
Pos = aeso_errors:pos(0, 0),
{error, [aeso_errors:new(file_error, Pos, Msg)]}
{error, [aeso_errors:new(file_error, Msg)]}
end.
add_include_path(File, Options) ->
@@ -283,14 +282,14 @@ to_sophia_value(_, _, revert, Data, Options) ->
{ok, {app, [], {id, [], "abort"}, [{string, [], Err}]}};
{error, _} ->
Msg = "Could not interpret the revert message\n",
{error, [aeso_errors:new(data_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end;
fate ->
try aeb_fate_encoding:deserialize(Data) of
Err -> {ok, {app, [], {id, [], "abort"}, [{string, [], Err}]}}
catch _:_ ->
Msg = "Could not deserialize the revert message\n",
{error, [aeso_errors:new(data_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end
end;
to_sophia_value(ContractString, FunName, ok, Data, Options0) ->
@@ -313,11 +312,11 @@ to_sophia_value(ContractString, FunName, ok, Data, Options0) ->
Type0Str = prettypr:format(aeso_pretty:type(Type0)),
Msg = io_lib:format("Cannot translate VM value ~p\n of type ~p\n to Sophia type ~s\n",
[Data, VmType, Type0Str]),
{error, [aeso_errors:new(type_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end;
{error, _Err} ->
Msg = io_lib:format("Failed to decode binary as type ~p\n", [VmType]),
{error, [aeso_errors:new(code_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end;
fate ->
try
@@ -326,11 +325,11 @@ to_sophia_value(ContractString, FunName, ok, Data, Options0) ->
Type1 = prettypr:format(aeso_pretty:type(Type)),
Msg = io_lib:format("Cannot translate FATE value ~p\n of Sophia type ~s\n",
[aeb_fate_encoding:deserialize(Data), Type1]),
{error, [aeso_errors:new(type_error, aeso_errors:pos(0, 0), Msg)]};
{error, [aeso_errors:new(data_error, Msg)]};
_:_ ->
Type1 = prettypr:format(aeso_pretty:type(Type)),
Msg = io_lib:format("Failed to decode binary as type ~s\n", [Type1]),
{error, [aeso_errors:new(code_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end
end
catch
@@ -395,11 +394,11 @@ decode_calldata(ContractString, FunName, Calldata, Options0) ->
Type0Str = prettypr:format(aeso_pretty:type(Type0)),
Msg = io_lib:format("Cannot translate VM value ~p\n of type ~p\n to Sophia type ~s\n",
[VmValue, VmType, Type0Str]),
{error, [aeso_errors:new(type_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end;
{error, _Err} ->
Msg = io_lib:format("Failed to decode calldata as type ~p\n", [VmType]),
{error, [aeso_errors:new(code_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end;
fate ->
case aeb_fate_abi:decode_calldata(FunName, Calldata) of
@@ -411,13 +410,13 @@ decode_calldata(ContractString, FunName, Calldata, Options0) ->
{ok, ArgTypes, AstArgs}
catch throw:cannot_translate_to_sophia ->
Type0Str = prettypr:format(aeso_pretty:type(Type0)),
Msg = io_lib:format("Cannot translate FATE value ~p\n of Sophia type ~s\n",
Msg = io_lib:format("Cannot translate FATE value ~p\n to Sophia type ~s\n",
[FateArgs, Type0Str]),
{error, [aeso_errors:new(type_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end;
{error, _} ->
Msg = io_lib:format("Failed to decode calldata binary\n", []),
{error, [aeso_errors:new(code_error, aeso_errors:pos(0, 0), Msg)]}
{error, [aeso_errors:new(data_error, Msg)]}
end
end
catch
@@ -433,8 +432,7 @@ get_arg_icode(Funs) ->
-dialyzer({nowarn_function, error_missing_call_function/0}).
error_missing_call_function() ->
Msg = "Internal error: missing '__call'-function",
Pos = aeso_errors:pos(0, 0),
aeso_errors:throw(aeso_errors:new(internal_error, Pos, Msg)).
aeso_errors:throw(aeso_errors:new(internal_error, Msg)).
get_call_type([{contract, _, _, Defs}]) ->
case [ {lists:last(QFunName), FunType}
@@ -462,7 +460,7 @@ get_decode_type(FunName, [{contract, Ann, _, Defs}]) ->
_ ->
Msg = io_lib:format("Function '~s' is missing in contract\n", [FunName]),
Pos = aeso_code_errors:pos(Ann),
aeso_errors:throw(aeso_errors:new(code_error, Pos, Msg))
aeso_errors:throw(aeso_errors:new(data_error, Pos, Msg))
end
end;
get_decode_type(FunName, [_ | Contracts]) ->
+16 -4
View File
@@ -30,6 +30,7 @@
-export([ err_msg/1
, msg/1
, new/2
, new/3
, new/4
, pos/2
@@ -40,6 +41,9 @@
, type/1
]).
new(Type, Msg) ->
new(Type, pos(0, 0), Msg).
new(Type, Pos, Msg) ->
#err{ type = Type, pos = Pos, message = Msg }.
@@ -72,15 +76,23 @@ str_pos(#pos{file = F, line = L, col = C}) ->
type(#err{ type = Type }) -> Type.
pp(#err{ pos = Pos } = Err) ->
lists:flatten(io_lib:format("~s~s", [pp_pos(Pos), msg(Err)])).
pp(#err{ type = Kind, pos = Pos } = Err) ->
lists:flatten(io_lib:format("~s~s:\n~s", [pp_kind(Kind), pp_pos(Pos), msg(Err)])).
pp_kind(type_error) -> "Type error";
pp_kind(parse_error) -> "Parse error";
pp_kind(code_error) -> "Code generation error";
pp_kind(file_error) -> "File error";
pp_kind(data_error) -> "Data error";
pp_kind(internal_error) -> "Internal error".
pp_pos(#pos{file = no_file, line = 0, col = 0}) ->
"";
pp_pos(#pos{file = no_file, line = L, col = C}) ->
io_lib:format("At line ~p, col ~p:\n", [L, C]);
io_lib:format(" at line ~p, col ~p", [L, C]);
pp_pos(#pos{file = F, line = L, col = C}) ->
io_lib:format("In '~s' at line ~p, col ~p:\n", [F, L, C]).
io_lib:format(" in '~s' at line ~p, col ~p", [F, L, C]).
to_json(#err{pos = Pos, type = Type, message = Msg, context = Cxt}) ->
Json = #{ pos => pos_to_json(Pos),
type => atom_to_binary(Type, utf8),