diff --git a/lib/thrift/generator/struct_generator.ex b/lib/thrift/generator/struct_generator.ex index 2af6c78..7d2f44f 100644 --- a/lib/thrift/generator/struct_generator.ex +++ b/lib/thrift/generator/struct_generator.ex @@ -24,12 +24,17 @@ defmodule Thrift.Generator.StructGenerator do |> Utils.sort_defs define_block = case label do - :struct -> - quote do: defstruct unquote(struct_parts) - :union -> - quote do: defstruct unquote(struct_parts) :exception -> quote do: defexception unquote(struct_parts) + _ -> + quote do: defstruct unquote(struct_parts) + end + + extra_defs = if label == :exception and not Keyword.has_key?(struct_parts, :message) do + quote do + @spec message(Exception.t) :: String.t + def message(exception), do: inspect(exception) + end end quote do @@ -43,6 +48,7 @@ defmodule Thrift.Generator.StructGenerator do unquote(define_block) @type t :: %__MODULE__{} def new, do: %__MODULE__{} + unquote_splicing(List.wrap(extra_defs)) defmodule BinaryProtocol do unquote_splicing(binary_protocol_defs) end diff --git a/test/thrift/generator/binary_protocol_test.exs b/test/thrift/generator/binary_protocol_test.exs index 15f1cd4..7195faf 100644 --- a/test/thrift/generator/binary_protocol_test.exs +++ b/test/thrift/generator/binary_protocol_test.exs @@ -306,6 +306,9 @@ defmodule Thrift.Generator.BinaryProtocolTest do 1: optional string message, 99: optional byte num; } + exception Ex2 { + 1: optional i16 error_code; + } struct Exception { 1: optional Ex val; 2: optional map val_map; @@ -327,6 +330,12 @@ defmodule Thrift.Generator.BinaryProtocolTest do assert_serializes %Exception{val_list: [%Ex{num: 91}]}, <<15, 0, 4, 12, 0, 0, 0, 1, 3, 0, 99, 91, 0, 0>> end + thrift_test "exceptions always provide message/1" do + assert Ex.message(%Ex{message: "text", num: 1}) == "text" + assert Ex2.message(%Ex2{error_code: 1}) == + ~s(%Thrift.Generator.BinaryProtocolTest.Ex2{error_code: 1}) + end + @thrift_file name: "composite.thrift", contents: """ struct Composite { 1: optional map, map> map_of_maps;