From ffbab5d8a0dc8b656fe9b75b957ac70fb9d42793 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 19 Apr 2024 20:43:11 -0300 Subject: [PATCH] refactor: encode error messages --- lib/ssz_ex/encode.ex | 50 +++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/ssz_ex/encode.ex b/lib/ssz_ex/encode.ex index 9c2dc403f..e140b26c8 100644 --- a/lib/ssz_ex/encode.ex +++ b/lib/ssz_ex/encode.ex @@ -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} @@ -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)) @@ -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)} @@ -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) @@ -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}} @@ -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