Linuus

Linuus

Poison can't encode date in staging environment

I’ve just upgraded a Phoenix app to 1.3 and Elixir 1.5.2. Now I get an exception on staging but not locally. I’ve traced it down (after getting some help from @michalmuskala on Slack) to when Poison encodes dates.

I use Poison 3.1, and this is the error I get:

** (Poison.EncodeError) unable to encode value: {0, 6}

    (poison) lib/poison/encoder.ex:383: Poison.Encoder.Any.encode/2

    (poison) lib/poison/encoder.ex:227: anonymous fn/4 in Poison.Encoder.Map.encode/3

    (poison) lib/poison/encoder.ex:228: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3

    (poison) lib/poison/encoder.ex:228: Poison.Encoder.Map.encode/3

    (poison) lib/poison/encoder.ex:227: anonymous fn/4 in Poison.Encoder.Map.encode/3

    (poison) lib/poison/encoder.ex:228: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3

    (poison) lib/poison/encoder.ex:228: Poison.Encoder.Map.encode/3

I only return Maps from my render functions (not using @derive in model/schema).

The Map that fails to encode looks like this:

%{id: 216, images: [%{aspect_ratio: 1.33333, id: 194, urls: %{original: "https://example.com/foo.jpg?v=63674938035", small: "https://example.com/foo.jpg?v=63674938035", thumb: "https://example.com/foo.jpg?v=63674938035"}}], inserted_at: ~N[2017-10-11 10:46:53.000000], tag_list: ["foo", "bar", "baz", "qux"], text: "Some text", thread: %{address: "Address 1", area: 100, district: nil, id: 127, interest_count: 4, internal_id: "23852", location: %{lat: 57.7456204, long: 11.9868868}, lot_size: nil, price: 2500000, property_type: %{id: 3, name: "FooBar"}, realtor: %{avatar_url: "https://example.com/avatar.jpg?v=63674969635", email: "foobar@example.com", first_name: "Foo", id: 78, last_name: "Bar", realtor: true}, rooms: #Decimal<4.0>, status: 2, title: "Some title", user_interest_level: 0, web_url: "https://example.com/threads/127"}, thread_id: 127, updated_at: ~N[2017-10-11 10:46:53.000000]}

And from what I can find it’s the NaiveDateTime that breaks.

Any ideas on how to fix this?

Most Liked

hubertlepicki

hubertlepicki

I suspect it might be a precision issue. If you are using Postgres it can have different settings for precision of timestamps, i.e. how many digits are after dot separating seconds from miliseconds etc. Check out the settings in your Postgres and see documentation here: https://www.postgresql.org/docs/9.1/static/datatype-datetime.html

I supect that maybe on your staging environment, you have high precision configured for timestamps. The default can be changed in Postgres’s config file. And that somehow Poison can’t understand such high precision. It’s a guess, I’ll leave it up to you to investigate.

Linuus

Linuus

For now I convert the NaiveDateTime myself with NaiveDateTime.to_iso8601(post.updated_at) etc.

Linuus

Linuus

Thank you, that sounds plausible! Will check when I’m back at work.

hubertlepicki

hubertlepicki

Don’t forget to update us here. If this is the case, it’s most likely a bug somewhere either in Elixir or Ecto or Poison that needs to be fixed. This should not happen.

Linuus

Linuus

Definitely :+1:

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement