-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathplug.ex
More file actions
72 lines (60 loc) · 1.97 KB
/
plug.ex
File metadata and controls
72 lines (60 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
errors = [
{Ecto.CastError, 400},
{Ecto.Query.CastError, 400},
{Ecto.NoResultsError, 404},
{Ecto.StaleEntryError, 409}
]
excluded_exceptions = Application.get_env(:phoenix_ecto, :exclude_ecto_exceptions_from_plug, [])
for {exception, status_code} <- errors do
unless exception in excluded_exceptions do
defimpl Plug.Exception, for: exception do
def status(_), do: unquote(status_code)
def actions(_), do: []
end
end
end
unless Ecto.SubQueryError in excluded_exceptions do
defimpl Plug.Exception, for: Ecto.SubQueryError do
def status(sub_query_error) do
Plug.Exception.status(sub_query_error.exception)
end
def actions(_), do: []
end
end
unless Phoenix.Ecto.PendingMigrationError in excluded_exceptions do
defimpl Plug.Exception, for: Phoenix.Ecto.PendingMigrationError do
def status(_error), do: 503
def actions(%{repo: repo, directories: directories, migration_opts: migration_opts}),
do: [
%{
label: "Run migrations for repo",
handler: {__MODULE__, :migrate, [repo, directories, migration_opts]}
}
]
def migrate(repo, directories, migration_opts) do
Ecto.Migrator.run(repo, directories, :up, Keyword.merge(migration_opts || [], all: true))
end
end
end
unless Phoenix.Ecto.StorageNotCreatedError in excluded_exceptions do
defimpl Plug.Exception, for: Phoenix.Ecto.StorageNotCreatedError do
def status(_error), do: 503
def actions(%{repo: repo}),
do: [
%{
label: "Create database for repo",
handler: {__MODULE__, :storage_up, [repo]}
}
]
def storage_up(repo), do: repo.__adapter__().storage_up(repo.config())
end
end
if Code.ensure_loaded?(Postgrex.Error) do
unless Postgrex.Error in excluded_exceptions do
defimpl Plug.Exception, for: Postgrex.Error do
def status(%{postgres: %{code: :character_not_in_repertoire}}), do: 400
def status(_), do: 500
def actions(_), do: []
end
end
end