-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add requires Postgres Error Handler module
- Loading branch information
1 parent
e7e492a
commit 4b1c94c
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
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,30 @@ | ||
defmodule PostgresErrorHandler do | ||
@moduledoc """ | ||
Handles Postgres errors and provides friendly messages on known failures. | ||
""" | ||
require Logger | ||
|
||
@doc """ | ||
Handles errors for Addict Repository interactions. | ||
""" | ||
def handle_error(Addict.Repository, postgres_error) do | ||
case postgres_error.postgres.code do | ||
"23505" -> {:error, "User already exists"} | ||
_ -> handle_unknown_error(postgres_error.postgres) | ||
end | ||
end | ||
|
||
@doc """ | ||
Handles generic errors. | ||
""" | ||
def handle_error(_, postgres_error) do | ||
handle_unknown_error(postgres_error.postgres) | ||
end | ||
|
||
defp handle_unknown_error(postgres_error) do | ||
Logger.debug "unknown error caught: #{postgres_error.code}" | ||
IO.inspect postgres_error | ||
{:error, "Unknow error: #{postgres_error.code}"} | ||
end | ||
|
||
end |