Skip to content

Commit

Permalink
refactor: encode error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
avilagaston9 committed Apr 19, 2024
1 parent 97acc5c commit ffbab5d
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions lib/ssz_ex/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule SszEx.Encode do
@bits_per_byte 8

@spec encode(any(), SszEx.schema()) ::
{:ok, binary()} | Error.t()
{:ok, binary()} | {:error, Error.t()}
def encode(value, {:int, size}), do: encode_int(value, size)
def encode(value, :bool), do: encode_bool(value)
def encode(value, {:byte_list, _}), do: {:ok, value}
Expand Down Expand Up @@ -52,10 +52,11 @@ defmodule SszEx.Encode do
size = Enum.count(list)

if size > max_size do
%Error{
message:
"Invalid binary length while encoding list of #{inspect(inner_type)}.\nExpected max_size: #{max_size}.\nFound: #{size}\n"
}
{:error,
%Error{
message:
"Invalid binary length while encoding list of #{inspect(inner_type)}.\nExpected max_size: #{max_size}.\nFound: #{size}\n"
}}
else
list
|> Enum.map(&encode(&1, inner_type))
Expand All @@ -67,20 +68,23 @@ defmodule SszEx.Encode do
len = bit_size(bit_list)

if len > max_size do
%Error{
message:
"Invalid binary length while encoding BitList.\nExpected max_size: #{max_size}. Found: #{len}.\n"
}
{:error,
%Error{
message:
"Invalid binary length while encoding BitList.\nExpected max_size: #{max_size}. Found: #{len}.\n"
}}
else
{:ok, BitList.to_bytes(bit_list)}
end
end

defp encode_bitvector(bit_vector, size) when bit_vector_size(bit_vector) != size,
do: %Error{
message:
"Invalid binary length while encoding BitVector. \nExpected: #{size}.\nFound: #{bit_vector_size(bit_vector)}."
}
do:
{:error,
%Error{
message:
"Invalid binary length while encoding BitVector. \nExpected: #{size}.\nFound: #{bit_vector_size(bit_vector)}."
}}

defp encode_bitvector(bit_vector, _size),
do: {:ok, BitVector.to_bytes(bit_vector)}
Expand All @@ -89,10 +93,11 @@ defmodule SszEx.Encode do
size = Enum.count(list)

if size > max_size do
%Error{
message:
"Invalid binary length while encoding list of #{inspect(inner_type)}.\nExpected max_size: #{max_size}.\nFound: #{size}\n"
}
{:error,
%Error{
message:
"Invalid binary length while encoding list of #{inspect(inner_type)}.\nExpected max_size: #{max_size}.\nFound: #{size}\n"
}}
else
fixed_lengths = @bytes_per_length_offset * length(list)

Expand Down Expand Up @@ -126,7 +131,7 @@ defmodule SszEx.Encode do
{:cont, {:ok, {[encoded | res_encoded], [size | res_size], size + acc}}}

error ->
{:halt, %Error{message: error}}
{:halt, {:error, %Error{message: error}}}
end
end) do
{:ok, {Enum.reverse(encoded_list), Enum.reverse(byte_size_list), total_byte_size}}
Expand Down Expand Up @@ -198,10 +203,11 @@ defmodule SszEx.Encode do
2 ** (@bytes_per_length_offset * @bits_per_byte) do
:ok
else
%Error{
message:
"Invalid binary size after encoding. Size out of offset range. Size: #{fixed_lengths + total_byte_size}"
}
{:error,
%Error{
message:
"Invalid binary size after encoding. Size out of offset range. Size: #{fixed_lengths + total_byte_size}"
}}
end
end
end

0 comments on commit ffbab5d

Please sign in to comment.