Is there a way to programmatically check for the existence of a specific changeset error?
If I am adding an error to the changeset like this:
Ecto.Changeset.add_error(
changeset,
:age,
"Must be more than 12"
)
After I get the changeset back from Repo.insert how can I check that this specific error has been added? I want to add special handling for a specific error. I can use Ecto.Changeset.traverse_errors but that would require looking for the string "Must be more than 12" which seems brittle.
case insert(changest) do
{:ok, data} -> handle_data(data)
{:error, changeset} ->
if too_young_error?(changeset) do
special_handling(changeset)
else
{:error, changeset}
end
end
...
def too_young_error?(changeset) do
Enum.any(changeset.errors, fn {key, {_error_message, keys} ->
Keyword.get(keys, :validation) == :too_young
end)
end
This is better than checking for the error message, but it would be nice to have a more straightforward way to do this check.