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

Fix mangled Protobuf.EncodeError #386

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/protobuf/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,21 @@ defmodule Protobuf.Encoder do
end

acc =
case encode_field(class_field(prop), val, syntax, prop) do
case encode_field(prop, struct.__struct__, val, syntax) do
{:ok, iodata} -> [acc | iodata]
:skip -> acc
end

encode_fields(rest, syntax, struct, oneofs, acc)
end

defp encode_field(prop, struct_mod, val, syntax) do
do_encode_field(class_field(prop), val, syntax, prop)
rescue
error ->
raise Protobuf.EncodeError,
message:
"Got error when encoding #{inspect(struct.__struct__)}##{prop.name_atom}: #{Exception.format(:error, error)}"
"Got error when encoding #{inspect(struct_mod)}##{prop.name_atom}: #{Exception.format(:error, error)}"
end

defp skip_field?(_syntax, [], _prop), do: true
Expand All @@ -88,7 +92,7 @@ defmodule Protobuf.Encoder do
defp skip_field?(:proto3, false, %FieldProps{oneof: nil}), do: true
defp skip_field?(_syntax, _val, _prop), do: false

defp encode_field(
defp do_encode_field(
:normal,
val,
syntax,
Expand All @@ -102,11 +106,11 @@ defmodule Protobuf.Encoder do
end
end

defp encode_field(:embedded, _val = nil, _syntax, _prop) do
defp do_encode_field(:embedded, _val = nil, _syntax, _prop) do
:skip
end

defp encode_field(
defp do_encode_field(
:embedded,
val,
syntax,
Expand All @@ -131,7 +135,7 @@ defmodule Protobuf.Encoder do
{:ok, result}
end

defp encode_field(:packed, val, syntax, %FieldProps{type: type, encoded_fnum: fnum} = prop) do
defp do_encode_field(:packed, val, syntax, %FieldProps{type: type, encoded_fnum: fnum} = prop) do
if skip_field?(syntax, val, prop) or skip_enum?(syntax, val, prop) do
:skip
else
Expand Down Expand Up @@ -251,7 +255,7 @@ defmodule Protobuf.Encoder do
Enum.reduce(pb_exts, [], fn {{ext_mod, key}, val}, acc ->
case Protobuf.Extension.get_extension_props(mod, ext_mod, key) do
%{field_props: prop} ->
case encode_field(class_field(prop), val, :proto2, prop) do
case do_encode_field(class_field(prop), val, :proto2, prop) do
{:ok, iodata} -> [acc | iodata]
:skip -> acc
end
Expand Down
11 changes: 11 additions & 0 deletions test/protobuf/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ defmodule Protobuf.EncoderTest do
end
end

test "raises correct error for nested protobufs" do
message = """
Got error when encoding TestMsg.Foo#e: ** (Protobuf.EncodeError) Got error when \
encoding TestMsg.Foo.Bar#a: ** (Protobuf.EncodeError) "not_a_number" is invalid for type :int32\
"""

assert_raise Protobuf.EncodeError, message, fn ->
Encoder.encode(%TestMsg.Foo{a: 90, e: %TestMsg.Foo.Bar{a: "not_a_number"}})
end
end

test "encodes with transformer module" do
msg = %TestMsg.ContainsTransformModule{field: 0}
assert Encoder.encode(msg) == <<10, 0>>
Expand Down
Loading