From a09dae4d2695e8551c3b47d7c83f953403fab949 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Wed, 28 Jun 2023 11:29:54 -0400 Subject: [PATCH] fix: raise better error on invalid filter values closes #154 We still want to build something better directly into `Ash.Filter` that determines field types and casts them up front --- lib/data_layer.ex | 9 +++++++++ test/filter_test.exs | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/data_layer.ex b/lib/data_layer.ex index 37e3c79..dcf4e9c 100644 --- a/lib/data_layer.ex +++ b/lib/data_layer.ex @@ -1313,6 +1313,15 @@ defmodule AshPostgres.DataLayer do end end + defp handle_raised_error(%Ecto.Query.CastError{} = e, stacktrace, context, resource) do + handle_raised_error( + Ash.Error.Query.InvalidFilterValue.exception(value: e.value, context: context), + stacktrace, + context, + resource + ) + end + defp handle_raised_error(error, stacktrace, _context, _resource) do {:error, Ash.Error.to_ash_error(error, stacktrace)} end diff --git a/test/filter_test.exs b/test/filter_test.exs index dbe5ee9..64f6289 100644 --- a/test/filter_test.exs +++ b/test/filter_test.exs @@ -18,6 +18,16 @@ defmodule AshPostgres.FilterTest do end end + describe "invalid uuid" do + test "with an invalid uuid, an invalid error is raised" do + assert_raise Ash.Error.Invalid, fn -> + Post + |> Ash.Query.filter(id == "foo") + |> Api.read!() + end + end + end + describe "citext validation" do setup do on_exit(fn ->