Skip to content

Commit

Permalink
Merge pull request #7 from weareyipyip/add-no-assoc-error
Browse files Browse the repository at this point in the history
Add association exists error
  • Loading branch information
LouisMT authored Jun 16, 2023
2 parents 9e915b2 + 182bc02 commit 19498a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/cose_della_vita_ex/error_mapper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ defmodule CoseDellaVitaEx.ErrorMapper do
TokenInvalidError,
UniqueConstraintError,
WrongPasswordError,
RequireOneOfError
RequireOneOfError,
AssociationExistsError
}

require Logger
Expand Down Expand Up @@ -77,6 +78,9 @@ defmodule CoseDellaVitaEx.ErrorMapper do
def map_default(%{constraint: :assoc}, _message), do: %NotFoundError{}
def map_default(%{constraint: :foreign}, _message), do: %NotFoundError{}

def map_default(%{constraint: :no_assoc, constraint_name: constraint_name}, _message),
do: %AssociationExistsError{constraint_name: constraint_name}

def map_default(opts, message) do
Logger.warning(
"Unknown changeset validation type, defaulting to a generic GraphQL error. Defining a specific error type will improve the API's type strength and usability. Full opts: #{inspect(opts)}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule CoseDellaVitaEx.Errors.AssociationExistsError do
@message "An association still exists, preventing this entity from being deleted."
@moduledoc """
#{@message}
Absinthe type is `:association_exists_error`.
"""
use Absinthe.Schema.Notation

defstruct [:constraint_name, :path, message: @message, error_type: :association_exists_error]

@type t :: %__MODULE__{}

@desc @message
object :association_exists_error do
@desc "Name of the constraint."
field(:constraint_name, non_null(:string))
interface(:error)
import_fields(:error)
end
end
1 change: 1 addition & 0 deletions lib/cose_della_vita_ex/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule CoseDellaVitaEx.Schema do
import_types(CoseDellaVitaEx.Errors.TokenInvalidError)
import_types(CoseDellaVitaEx.Errors.UniqueConstraintError)
import_types(CoseDellaVitaEx.Errors.WrongPasswordError)
import_types(CoseDellaVitaEx.Errors.AssociationExistsError)
end
end
end

0 comments on commit 19498a1

Please sign in to comment.