Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add stacktrace to SSZ #1017

Merged
merged 12 commits into from
Apr 24, 2024
11 changes: 9 additions & 2 deletions lib/ssz_ex/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ defmodule SszEx.Error do
%Error{message: message, stacktrace: [new_trace]}
end

def add_trace(%Error{message: message, stacktrace: stacktrace}, value) when is_struct(value) do
new_trace =
value.__struct__ |> Module.split() |> List.last()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!


%Error{message: message, stacktrace: [new_trace | stacktrace]}
end

def add_trace(%Error{message: message, stacktrace: stacktrace}, new_trace) do
%Error{message: message, stacktrace: [new_trace | stacktrace]}
end

def add_trace({:error, %Error{} = error}, module),
do: {:error, Error.add_trace(error, module)}
def add_trace({:error, %Error{} = error}, new_trace),
do: {:error, Error.add_trace(error, new_trace)}

def add_trace(value, _module), do: value

Expand Down
6 changes: 3 additions & 3 deletions lib/ssz_ex/ssz_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule SszEx do

@spec encode(struct()) ::
{:ok, binary()} | {:error, Error.t()}
def encode(%name{} = value), do: encode(value, name) |> Error.add_trace("#{name}")
def encode(%name{} = value), do: encode(value, name) |> Error.add_trace(value)

@spec encode(any(), schema()) ::
{:ok, binary()} | {:error, Error.t()}
Expand All @@ -71,7 +71,7 @@ defmodule SszEx do
@spec decode(binary(), schema()) ::
{:ok, any()} | {:error, Error.t()}
def decode(value, module) when is_atom(module),
do: Decode.decode(value, module) |> Error.add_trace("#{module}")
do: Decode.decode(value, module) |> Error.add_trace(module)

@spec decode(binary(), schema()) ::
{:ok, any()} | {:error, Error.t()}
Expand All @@ -85,7 +85,7 @@ defmodule SszEx do

@spec hash_tree_root(struct()) :: {:ok, Types.root()} | {:error, Error.t()}
def hash_tree_root(%name{} = value),
do: hash_tree_root(value, name) |> Error.add_trace("#{name}")
do: hash_tree_root(value, name) |> Error.add_trace(value)

@spec hash_tree_root(any, any) :: {:ok, Types.root()} | {:error, Error.t()}
def hash_tree_root(value, schema), do: Merkleization.hash_tree_root(value, schema)
Expand Down
8 changes: 4 additions & 4 deletions test/unit/ssz_ex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ defmodule Unit.SSZExTest do
{:error, error} = SszEx.hash_tree_root(execution_payload)

assert "#{error}" ==
"Invalid binary length while merkleizing byte_vector.\nExpected size: 256.\nFound: 3\nStacktrace: Elixir.Types.ExecutionPayload.logs_bloom"
"Invalid binary length while merkleizing byte_vector.\nExpected size: 256.\nFound: 3\nStacktrace: ExecutionPayload.logs_bloom"
end

test "stacktrace in encode with invalid sync_committee_bits" do
Expand All @@ -880,7 +880,7 @@ defmodule Unit.SSZExTest do
{:error, error} = SszEx.encode(sync_aggregate)

assert "#{error}" ==
"Invalid binary length while encoding BitVector. \nExpected: 512.\nFound: 2.\nStacktrace: Elixir.Types.SyncAggregate.sync_committee_bits"
"Invalid binary length while encoding BitVector. \nExpected: 512.\nFound: 2.\nStacktrace: SyncAggregate.sync_committee_bits"
end

test "stacktrace encode nested container" do
Expand All @@ -889,14 +889,14 @@ defmodule Unit.SSZExTest do
{:error, error} = SszEx.encode(attester_slashing)

assert "#{error}" ==
"Invalid binary length while encoding list of {:int, 64}.\nExpected max_size: 2048.\nFound: 3000\nStacktrace: Elixir.Types.AttesterSlashing.attestation_2.attesting_indices"
"Invalid binary length while encoding list of {:int, 64}.\nExpected max_size: 2048.\nFound: 3000\nStacktrace: AttesterSlashing.attestation_2.attesting_indices"
end

test "stacktrace hash_tree_root nested container" do
attester_slashing = build_broken_attester_slashing()
{:error, error} = SszEx.hash_tree_root(attester_slashing)

assert "Invalid binary length while merkleizing list of {:int, 64}.\nExpected max_size: 2048.\nFound: 3000\nStacktrace: Elixir.Types.AttesterSlashing.attestation_2.attesting_indices" =
assert "Invalid binary length while merkleizing list of {:int, 64}.\nExpected max_size: 2048.\nFound: 3000\nStacktrace: AttesterSlashing.attestation_2.attesting_indices" =
"#{error}"
end
end
Loading