polypush135
Timex parsing invalid dates and the db is blowing up
Thoughts on how I can catch this type of error? I have an invalid date ie 11/31/2910. There is no 31st day of November. But my little logic does not catch that and timex seems to parse it all the same.
pry(2)> Timex.parse(start_date_string, "%m/%d/%Y", :strftime)
{:ok, ~N[2910-11-31 00:00:00]}
Heres my changeset validation gate
def parse_date(cs, key, string) do
case Timex.parse(string, "%m/%d/%Y", :strftime) do
{:ok, date} ->
cs |> put_change(key, date)
{:error, _} ->
cs |> add_error("#{key}_string" |> String.to_atom(), "not a valid date")
end
end
but as I said since timex parses the date it passes the gate on its way to the db where it blows up
[error] #PID<0.567.0> running BeffectWeb.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /departments/early-childhood/safe-and-together/client/1/spi/1
** (exit) an exception was raised:
** (ErlangError) Erlang error: :if_clause
(stdlib) calendar.erl:117: :calendar.date_to_gregorian_days/3
(ecto) Ecto.Adapters.Postgres.TypeModule.encode_params/3
(postgrex) lib/postgrex/query.ex:45: DBConnection.Query.Postgrex.Query.encode/3
(db_connection) lib/db_connection.ex:1079: DBConnection.describe_run/5
(db_connection) lib/db_connection.ex:1150: anonymous fn/4 in DBConnection.run_meter/5
(db_connection) lib/db_connection.ex:592: DBConnection.prepare_execute/4
(ecto) lib/ecto/adapters/postgres/connection.ex:86: Ecto.Adapters.Postgres.Connection.execute/4
(ecto) lib/ecto/adapters/sql.ex:256: Ecto.Adapters.SQL.sql_call/6
(ecto) lib/ecto/adapters/sql.ex:542: Ecto.Adapters.SQL.struct/8
(ecto) lib/ecto/repo/schema.ex:547: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:292: anonymous fn/14 in Ecto.Repo.Schema.do_update/4
(ecto) lib/ecto/repo/schema.ex:774: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
(ecto) lib/ecto/adapters/sql.ex:576: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
(db_connection) lib/db_connection.ex:1283: DBConnection.transaction_run/4
(db_connection) lib/db_connection.ex:1207: DBConnection.run_begin/3
(db_connection) lib/db_connection.ex:798: DBConnection.transaction/3
(beffect) lib/beffect_web/controllers/spi_controller.ex:156: BeffectWeb.SPIController.update/2
(beffect) lib/beffect_web/controllers/spi_controller.ex:1: BeffectWeb.SPIController.action/2
(beffect) lib/beffect_web/controllers/spi_controller.ex:1: BeffectWeb.SPIController.phoenix_controller_pipeline/2
(beffect) lib/beffect_web/endpoint.ex:1: BeffectWeb.Endpoint.instrument/4
What should I do for this?
Most Liked
axelson
Scenic Core Team
I think part of the problem is that Timex.parse/3 is only returning a NaiveDateTime when you’ll probably want a DateTime.
This is slightly improved:
iex(6)> Timex.parse!("11/31/2910", "%m/%d/%Y", :strftime) |> Timex.to_datetime("America/Chicago")
** (ErlangError) Erlang error: :if_clause
(stdlib) calendar.erl:117: :calendar.date_to_gregorian_days/3
(stdlib) calendar.erl:138: :calendar.datetime_to_gregorian_seconds/1
(timex) lib/datetime/helpers.ex:32: Timex.DateTime.Helpers.construct/2
Although it seems that Timex should be returning an {:error, term()} instead of raising that Erlang error. Maybe that’s a bug in Timex?
2
michalmuskala
Yeah, this looks like a bug in timex to me. IMHO it should never allow creating invalid dates when parsing.
2
Popular in Questions
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
can someone please explain to me how Enum.reduce works with maps
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
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
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
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
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Other popular topics
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
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
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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








