-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...boilerplate_web/controllers/error_html.ex → ...ixir_boilerplate_web/errors/error_html.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...boilerplate_web/controllers/error_json.ex → ...ixir_boilerplate_web/errors/error_json.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
defmodule ElixirBoilerplateWeb.Errors do | ||
alias Ecto.Changeset | ||
|
||
import Phoenix.Template, only: [embed_templates: 1] | ||
|
||
embed_templates("templates/*") | ||
|
||
@doc """ | ||
Generates a human-readable block containing all errors in a changeset. Errors | ||
are then localized using translations in the `ecto` domain. | ||
For example, you could have an `errors.po` file in the french locale: | ||
``` | ||
msgid "" | ||
msgstr "" | ||
"Language: fr" | ||
msgid "can't be blank" | ||
msgstr "ne peut être vide" | ||
``` | ||
""" | ||
def changeset_to_error_messages(changeset) do | ||
changeset | ||
|> Changeset.traverse_errors(&translate_error/1) | ||
|> convert_errors_to_html(changeset.data.__struct__) | ||
end | ||
|
||
defp translate_error({message, options}) do | ||
if options[:count] do | ||
Gettext.dngettext(ElixirBoilerplate.Gettext, "errors", message, message, options[:count], options) | ||
else | ||
Gettext.dgettext(ElixirBoilerplate.Gettext, "errors", message, options) | ||
end | ||
end | ||
|
||
defp convert_errors_to_html(errors, schema) do | ||
errors = Enum.reduce(errors, [], &convert_error_field(&1, &2, schema)) | ||
|
||
error_messages(%{errors: errors}) | ||
end | ||
|
||
defp convert_error_field({field, errors}, memo, schema) when is_list(errors) do | ||
memo ++ Enum.flat_map(errors, &convert_error_subfield(&1, field, [], schema)) | ||
end | ||
|
||
defp convert_error_field({field, errors}, memo, schema) when is_map(errors) do | ||
memo ++ Enum.flat_map(Map.keys(errors), &convert_error_subfield(&1, field, errors[&1], schema)) | ||
end | ||
|
||
defp convert_error_subfield(message, field, _, _schema) when is_binary(message) do | ||
# NOTE `schema` is available here if we want to use something like | ||
# `schema.humanize_field(field)` to be able to display `"Email address is | ||
# invalid"` instead of `email is invalid"`. | ||
["#{field} #{message}"] | ||
end | ||
|
||
defp convert_error_subfield(message, field, memo, schema) when is_map(message) do | ||
Enum.reduce(message, memo, fn {subfield, errors}, memo -> | ||
memo ++ convert_error_field({"#{field}.#{subfield}", errors}, memo, schema) | ||
end) | ||
end | ||
|
||
defp convert_error_subfield(subfield, field, errors, schema) do | ||
field = "#{field}.#{subfield}" | ||
convert_error_field({field, errors}, [], schema) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>Not found</title> | ||
</head> | ||
<body> | ||
<p>Sorry, the page you are looking for does not exist.</p> | ||
</body> | ||
</html> |
7 changes: 7 additions & 0 deletions
7
lib/elixir_boilerplate_web/errors/templates/error_messages.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<%= if @errors != [] do %> | ||
<ul> | ||
<%= for error <- @errors do %> | ||
<li><%= error %></li> | ||
<% end %> | ||
</ul> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule ElixirBoilerplateWeb.ErrorHtmlTest do | ||
use ElixirBoilerplate.DataCase, async: true | ||
|
||
# Bring render_to_string/3 for testing custom views | ||
import Phoenix.Template | ||
|
||
alias ElixirBoilerplateWeb.Errors.ErrorHTML | ||
|
||
test "renders 404.html" do | ||
assert render_to_string(ErrorHTML, "404", "html", []) == "Not Found" | ||
end | ||
|
||
test "renders 500.html" do | ||
assert render_to_string(ErrorHTML, "500", "html", []) == "Internal Server Error" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule ElixirBoilerplateWeb.ErrorJsonTest do | ||
use ElixirBoilerplate.DataCase, async: true | ||
|
||
alias ElixirBoilerplateWeb.Errors.ErrorJSON | ||
|
||
test "renders 404" do | ||
assert ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}} | ||
end | ||
|
||
test "renders 500" do | ||
assert ErrorJSON.render("500.json", %{}) == %{errors: %{detail: "Internal Server Error"}} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters