Phoenix fails in live_reloader when response is encoded with Msgpax

with the code_reloader everything goes through:

  defp before_send_inject_reloader(conn, endpoint) do
    register_before_send conn, fn conn ->
      resp_body = to_string(conn.resp_body)

msgpack:

iex> (for id <- 1..16 do %{id: id, title: "post-#{id}", body: "body-#{id}"} end)|> Msgpax.pack!
[<<220, 0, 16>>,
 [131, [[164 | "body"], 166 | "body-1"], [[162 | "id"], 1]....

to_string breaks with

iex> (for id <- 1..16 do %{id: id, title: "post-#{id}", body: "body-#{id}"} end)|> Msgpax.pack! |> to_string
** (UnicodeConversionError) invalid encoding starting at <<220, 0, 16>>

works fine with 1…15:

iex> (for id <- 1..15 do %{id: id, title: "post-#{id}", body: "body-#{id}"} end)|> Msgpax.pack!             
[159,
 [131, [[164 | "body"], 166 | "body-1"], [[162 | "id"], 1]....

to_string

iex> (for id <- 1..15 do %{id: id, title: "post-#{id}", body: "body-#{id}"} end)|> Msgpax.pack! |> to_string
<<194, 159, 194, 131, 194, 164, 98, 111, 100, 121, 194, 166, 98, 111, 100, 121....

I’m not the one to say if the error is in msgpax or the code_reloader or iodata is not to be used - but obviously to_string is not happy about the <<220, 0, 16>> part;-)

With my limited knowledge iodata should be preferred yes.

1 Like