refactor(backend): extract convert_ecto_errors in the Mobilizon.Storage.Ecto module
And use it to log refreshing instance errors Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -7,6 +7,7 @@ defmodule Mobilizon.Storage.Ecto do
|
||||
import Ecto.Changeset, only: [fetch_change: 2, put_change: 3, get_field: 2]
|
||||
alias Ecto.{Changeset, Query}
|
||||
alias Mobilizon.Web.Endpoint
|
||||
alias Mobilizon.Web.Gettext, as: GettextBackend
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
|
||||
@doc """
|
||||
@@ -56,4 +57,30 @@ defmodule Mobilizon.Storage.Ecto do
|
||||
changeset
|
||||
end
|
||||
end
|
||||
|
||||
def convert_ecto_errors(%Ecto.Changeset{} = changeset),
|
||||
do: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
|
||||
|
||||
# Translates an error message using gettext.
|
||||
defp translate_error({msg, opts}) do
|
||||
# Because error messages were defined within Ecto, we must
|
||||
# call the Gettext module passing our Gettext backend. We
|
||||
# also use the "errors" domain as translations are placed
|
||||
# in the errors.po file.
|
||||
# Ecto will pass the :count keyword if the error message is
|
||||
# meant to be pluralized.
|
||||
# On your own code and templates, depending on whether you
|
||||
# need the message to be pluralized or not, this could be
|
||||
# written simply as:
|
||||
#
|
||||
# dngettext "errors", "1 file", "%{count} files", count
|
||||
# dgettext "errors", "is invalid"
|
||||
#
|
||||
|
||||
if count = opts[:count] do
|
||||
Gettext.dngettext(GettextBackend, "errors", msg, msg, count, opts)
|
||||
else
|
||||
Gettext.dgettext(GettextBackend, "errors", msg, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user