neiam
/apollos_crib
It's Apollo's Crib
public · Issues · Pulls 1 · Labels · Forks · Compare · Actions failure · Badges · Packages
⭐
Log in to mark this repository.
hermes was missing error views, soon we will know the True Error
b6d4917 · gmorell · 2023-03-07 05:59
Files changed
added
apps/room_hermes/lib/room_hermes_web/views/error_helpers.ex
+47
−0
@@ -0,0 +1,47 @@
| 1 | +defmodule RoomHermesWeb.ErrorHelpers do | |
| 2 | + @moduledoc """ | |
| 3 | + Conveniences for translating and building error messages. | |
| 4 | + """ | |
| 5 | + | |
| 6 | + use Phoenix.HTML | |
| 7 | + | |
| 8 | + @doc """ | |
| 9 | + Generates tag for inlined form input errors. | |
| 10 | + """ | |
| 11 | + def error_tag(form, field) do | |
| 12 | + Enum.map(Keyword.get_values(form.errors, field), fn error -> | |
| 13 | + content_tag(:span, translate_error(error), | |
| 14 | + class: "invalid-feedback", | |
| 15 | + phx_feedback_for: input_name(form, field) | |
| 16 | + ) | |
| 17 | + end) | |
| 18 | + end | |
| 19 | + | |
| 20 | + @doc """ | |
| 21 | + Translates an error message using gettext. | |
| 22 | + """ | |
| 23 | + def translate_error({msg, opts}) do | |
| 24 | + # When using gettext, we typically pass the strings we want | |
| 25 | + # to translate as a static argument: | |
| 26 | + # | |
| 27 | + # # Translate "is invalid" in the "errors" domain | |
| 28 | + # dgettext("errors", "is invalid") | |
| 29 | + # | |
| 30 | + # # Translate the number of files with plural rules | |
| 31 | + # dngettext("errors", "1 file", "%{count} files", count) | |
| 32 | + # | |
| 33 | + # Because the error messages we show in our forms and APIs | |
| 34 | + # are defined inside Ecto, we need to translate them dynamically. | |
| 35 | + # This requires us to call the Gettext module passing our gettext | |
| 36 | + # backend as first argument. | |
| 37 | + # | |
| 38 | + # Note we use the "errors" domain, which means translations | |
| 39 | + # should be written to the errors.po file. The :count option is | |
| 40 | + # set by Ecto and indicates we should also apply plural rules. | |
| 41 | + if count = opts[:count] do | |
| 42 | + Gettext.dngettext(RoomHermesWeb.Gettext, "errors", msg, msg, count, opts) | |
| 43 | + else | |
| 44 | + Gettext.dgettext(RoomHermesWeb.Gettext, "errors", msg, opts) | |
| 45 | + end | |
| 46 | + end | |
| 47 | +end |
added
apps/room_hermes/lib/room_hermes_web/views/error_view.ex
+16
−0
@@ -0,0 +1,16 @@
| 1 | +defmodule RoomHermesWeb.ErrorView do | |
| 2 | + use RoomHermesWeb, :view | |
| 3 | + | |
| 4 | + # If you want to customize a particular status code | |
| 5 | + # for a certain format, you may uncomment below. | |
| 6 | + # def render("500.html", _assigns) do | |
| 7 | + # "Internal Server Error" | |
| 8 | + # end | |
| 9 | + | |
| 10 | + # By default, Phoenix returns the status message from | |
| 11 | + # the template name. For example, "404.html" becomes | |
| 12 | + # "Not Found". | |
| 13 | + def template_not_found(template, _assigns) do | |
| 14 | + Phoenix.Controller.status_message_from_template(template) | |
| 15 | + end | |
| 16 | +end |
Parents: 0efe3de