Skip to content

Commit

Permalink
Merge 8fe2b10 into da4c974
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprev committed Apr 7, 2018
2 parents da4c974 + 8fe2b10 commit 830680e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/web/views/error_view.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
defmodule Accent.ErrorView do
@moduledoc """
Render common errors for JSON or HTML format
## Examples
iex> Accent.ErrorView.render("400.json", %{reason: "test"})
%{error: "Bad request", message: "test"}
iex> Accent.ErrorView.render("400.json", %{reason: %{complex: "data"}})
%{error: "Bad request", message: "-"}
iex> Accent.ErrorView.render("400.json", %{reason: %Ecto.Query.CastError{}})
%{error: "Bad request", message: "Bad argument type cast"}
iex> Accent.ErrorView.render("404.json", %{reason: %Phoenix.Router.NoRouteError{}})
%{error: "Not found", message: "Route not found"}
iex> Accent.ErrorView.render("404.json", %{reason: %Ecto.NoResultsError{}})
%{error: "Not found", message: "Resource not found"}
iex> Accent.ErrorView.render("404.json", %{reason: "test"})
%{error: "Not found", message: "test"}
iex> Accent.ErrorView.render("404.json", %{reason: %{complex: "data"}})
%{error: "Not found", message: "-"}
iex> Accent.ErrorView.render("500.json", %{})
%{error: "Internal error", message: "An error occured, someone as been notified"}
iex> Accent.ErrorView.render("404.html", %{})
"Page not found"
iex> Accent.ErrorView.render("500.html", %{})
"Server internal error"
iex> Accent.ErrorView.template_not_found("index.html", %{})
"Server internal error"
"""

use Phoenix.View, root: "lib/web/templates"

def render("400.json", %{reason: reason}) do
message =
case reason do
%Ecto.Query.CastError{} -> "Bad argument type cast"
error when is_binary(error) -> error
_ -> "-"
end

Expand All @@ -19,6 +53,7 @@ defmodule Accent.ErrorView do
case reason do
%Phoenix.Router.NoRouteError{} -> "Route not found"
%Ecto.NoResultsError{} -> "Resource not found"
error when is_binary(error) -> error
_ -> "-"
end

Expand Down
4 changes: 4 additions & 0 deletions test/web/views/error_view_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule AccentTest.ErrorView do
use ExUnit.Case
doctest Accent.ErrorView
end

0 comments on commit 830680e

Please sign in to comment.