Skip to content

Commit

Permalink
prettify contract error
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Mar 15, 2023
1 parent 736c775 commit 48bcf6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
23 changes: 20 additions & 3 deletions lib/archethic_web/controllers/api/transaction_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,10 @@ defmodule ArchethicWeb.API.TransactionController do
"recipient_address" => Base.encode16(recipient)
}

{{:exit, _reason}, recipient} ->
# TODO: find a way to prettify the error and remove the stacktrace
{{:exit, reason}, recipient} ->
%{
"valid" => false,
"reason" => "A contract exited while simulating",
"reason" => format_exit_reason(reason),
"recipient_address" => Base.encode16(recipient)
}
end)
Expand Down Expand Up @@ -228,4 +227,22 @@ defmodule ArchethicWeb.API.TransactionController do
{:error, reason}
end
end

defp format_exit_reason({error_atom, stacktrace}) do
Enum.reduce_while(
stacktrace,
"A contract exited with error: #{error_atom}",
fn
{:elixir_eval, _, _, [file: 'nofile', line: line]}, acc ->
{:halt, acc <> " (line: #{line})"}

_, acc ->
{:cont, acc}
end
)
end

defp format_exit_reason(_) do
"A contract exited with an unknown error"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ defmodule ArchethicWeb.API.TransactionControllerTest do
[
%{
"valid" => true,
"address" => "00009E059E8171643B959284FE542909F3B32198B8FC25B3E50447589B84341C1D66"
"recipient_address" =>
"00009E059E8171643B959284FE542909F3B32198B8FC25B3E50447589B84341C1D66"
},
%{
"valid" => true,
"address" => "00009E059E8171643B959284FE542909F3B32198B8FC25B3E50447589B84341C1D67"
"recipient_address" =>
"00009E059E8171643B959284FE542909F3B32198B8FC25B3E50447589B84341C1D67"
}
],
json_response(conn, 200)
Expand Down

0 comments on commit 48bcf6c

Please sign in to comment.