I created a custom function in Postgresql that checks data before insert or update and raises error if something goes wrong.
CREATE FUNCTION custom_check() RETURNS TRIGGER AS $$
BEGIN
IF <SOME CONDITION> THEN
RAISE EXCEPTION 'CUSTOM ERROR';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql
""")
When I'm using constraints in Postgresql, I can handle errors raised with Ecto.Changeset.check_constraint
.
But I didn't find a way to handle this error I'm raising, to reflect it in changeset instead of getting an exception and probably catching it inside my code.
Should I raise error differently for Ecto.Changeset.check_constraint
to handle it, or do differently something else?
RAISE EXCEPTION 'CUSTOM ERROR', USING ERRCODE = 'check_violation';,
but Ecto still doesn't like catching it. I'm catching it in tests usingassert_raise(Postgrex.Error, ~R/CUSTOM ERROR/
. And yes, I will probably end up catching it in my Elixir code. – Adriaadriaens