Error case return for context

Why is changeset returned in the error case.

@doc """
 Deletes a lead.

 ## Examples

     iex> delete_lead(lead)
     {:ok, %Lead{}}

     iex> delete_lead(lead)
     {:error, %Ecto.Changeset{}}

 """
 def delete_lead(%Lead{} = lead) do
   Repo.delete(lead)
 end

Should it be repo error?

The error tuple return is for errors within a “working system” like a stale record (already deleted) or some constraint causing an issue (still used as a foreign key). All of those apply to updates as well and are generally modelled to be communicated as errors on changesets. You can also supply a changeset to Repo.delete and the record will only be deleted if the changeset is valid (and stays valid e.g. when running prepared callbacks).

If your db cannot be reached or fails to execute expectedly that’s afaik an error case where ecto just raises.

1 Like