From 7eac076581370db64b7b13598d348e119f83e566 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sat, 21 Oct 2023 23:30:33 -0400 Subject: [PATCH] chore: update dialyzer & cleanup dead code --- lib/ash/error/error.ex | 15 +-------------- lib/ash/error/forbidden.ex | 2 ++ lib/ash/error/framework.ex | 2 ++ lib/ash/error/invalid.ex | 2 ++ lib/ash/error/unknown.ex | 2 ++ lib/ash/test.ex | 12 +++++++++--- mix.exs | 2 +- test/code_interface_test.exs | 11 ----------- 8 files changed, 19 insertions(+), 29 deletions(-) diff --git a/lib/ash/error/error.ex b/lib/ash/error/error.ex index 446aa7a3..b0bd213d 100644 --- a/lib/ash/error/error.ex +++ b/lib/ash/error/error.ex @@ -54,19 +54,6 @@ defmodule Ash.Error do @error_class_indices @error_classes |> Enum.with_index() |> Enum.into(%{}) - @spec extract_errors( - t() - | list(t()) - | Ash.Changeset.t() - | Ash.Query.t() - | Ash.ActionInput.t() - | term() - ) :: [class_error()] - def extract_errors(%struct{errors: errors}) - when struct in [Ash.Changeset, Ash.Query, Ash.ActionInput] do - Ash.Error.to_error_class(errors) - end - @doc false def error_modules, do: Keyword.values(@error_modules) @@ -207,7 +194,7 @@ defmodule Ash.Error do - `query`: a query related to the error - `error_context`: a sting message providing extra context around the error """ - @spec to_error_class(term()) :: class_error() + @spec to_error_class(any()) :: class_error() def to_error_class(values, opts \\ []) def to_error_class(%struct{errors: errors} = thing, opts) diff --git a/lib/ash/error/forbidden.ex b/lib/ash/error/forbidden.ex index 6397c173..94e9e619 100644 --- a/lib/ash/error/forbidden.ex +++ b/lib/ash/error/forbidden.ex @@ -5,6 +5,8 @@ defmodule Ash.Error.Forbidden do def_ash_error([:errors, stacktraces?: true], class: :forbidden) + @type t :: %__MODULE__{} + defimpl Ash.ErrorKind do def id(_), do: Ash.UUID.generate() diff --git a/lib/ash/error/framework.ex b/lib/ash/error/framework.ex index ed7167d1..2930bee6 100644 --- a/lib/ash/error/framework.ex +++ b/lib/ash/error/framework.ex @@ -4,6 +4,8 @@ defmodule Ash.Error.Framework do def_ash_error([:errors, stacktraces?: true], class: :framework) + @type t :: %__MODULE__{} + defimpl Ash.ErrorKind do def id(_), do: Ash.UUID.generate() diff --git a/lib/ash/error/invalid.ex b/lib/ash/error/invalid.ex index af4496b0..931322ab 100644 --- a/lib/ash/error/invalid.ex +++ b/lib/ash/error/invalid.ex @@ -4,6 +4,8 @@ defmodule Ash.Error.Invalid do def_ash_error([:errors, stacktraces?: true], class: :invalid) + @type t :: %__MODULE__{} + defimpl Ash.ErrorKind do def id(_), do: Ash.UUID.generate() diff --git a/lib/ash/error/unknown.ex b/lib/ash/error/unknown.ex index 6478e2a2..7354df52 100644 --- a/lib/ash/error/unknown.ex +++ b/lib/ash/error/unknown.ex @@ -4,6 +4,8 @@ defmodule Ash.Error.Unknown do def_ash_error([:errors, stacktraces?: true], class: :unknown) + @type t :: %__MODULE__{} + def exception(opts) do if opts[:error] do super(Keyword.update(opts, :errors, [opts[:error]], &[opts[:error] | &1])) diff --git a/lib/ash/test.ex b/lib/ash/test.ex index 206d635a..8e4178cd 100644 --- a/lib/ash/test.ex +++ b/lib/ash/test.ex @@ -14,7 +14,7 @@ defmodule Ash.Test do Ash.Changeset.t() | Ash.Query.t() | Ash.ActionInput.t(), error_class :: Ash.Error.class_module(), (Ash.Error.t() -> boolean) - ) :: :ok | no_return + ) :: Ash.Error.t() | no_return def assert_has_error(changeset_query_or_input, error_class \\ nil, callback, opts \\ []) do type = case changeset_query_or_input do @@ -32,7 +32,9 @@ defmodule Ash.Test do ) end - ExUnit.Assertions.assert(Enum.any?(error.errors, callback), + match = Enum.find(error.errors, callback) + + ExUnit.Assertions.assert(match, message: opts[:message] || """ @@ -43,6 +45,8 @@ defmodule Ash.Test do #{inspect(error.errors, pretty: true)} """ ) + + match end @doc """ @@ -54,7 +58,7 @@ defmodule Ash.Test do Ash.Changeset.t() | Ash.Query.t() | Ash.ActionInput.t(), error_class :: Ash.Error.class_module(), (Ash.Error.t() -> boolean) - ) :: :ok | no_return + ) :: Ash.Error.t() | no_return def refute_has_error(changeset_query_or_input, error_class \\ nil, callback, opts \\ []) do type = case changeset_query_or_input do @@ -89,6 +93,8 @@ defmodule Ash.Test do #{inspect(error.errors, pretty: true)} """ ) + + match end @doc """ diff --git a/mix.exs b/mix.exs index 0eed5a2f..839cf666 100644 --- a/mix.exs +++ b/mix.exs @@ -19,7 +19,7 @@ defmodule Ash.MixProject do elixirc_paths: elixirc_paths(Mix.env()), package: package(), deps: deps(), - dialyzer: [plt_add_apps: [:mix, :mnesia, :earmark, :plug]], + dialyzer: [plt_add_apps: [:mix, :mnesia, :earmark, :plug, :ex_unit]], docs: docs(), aliases: aliases(), description: @description, diff --git a/test/code_interface_test.exs b/test/code_interface_test.exs index 8ba62064..7e1bc53f 100644 --- a/test/code_interface_test.exs +++ b/test/code_interface_test.exs @@ -1,6 +1,5 @@ defmodule Ash.Test.CodeInterfaceTest do @moduledoc false - alias Ash.Error.Exception use ExUnit.Case, async: true defmodule User do @@ -122,16 +121,6 @@ defmodule Ash.Test.CodeInterfaceTest do end test "code interface-generated functions should check the type of their first argument and return an expressive error" do - # create a few users - users = [ - User - |> Ash.Changeset.for_create(:create, %{first_name: "Zach", last_name: "Daniel"}) - |> Api.create!(), - User - |> Ash.Changeset.for_create(:create, %{first_name: "Zach2", last_name: "Daniel2"}) - |> Api.create!() - ] - assert_raise ArgumentError, ~r/^Initial must be a changeset with the action type of.+/i, fn ->