Skip to content

Commit

Permalink
Add requires Postgres Error Handler module
Browse files Browse the repository at this point in the history
  • Loading branch information
trenpixster committed Jan 20, 2015
1 parent e7e492a commit 4b1c94c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/addict/postgres_error_handler.ex
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

0 comments on commit 4b1c94c

Please sign in to comment.