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: PostgreSQL: Documentation: 9.1: Date/Time Types

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.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement