Created
February 25, 2016 06:29
-
-
Save sikanhe/a749e4e8ae35903c0a86 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule MyApp.RethinkDB.Helpers do | |
def run(query, opts) do | |
query | |
|> MyApp.Database.run(opts) | |
|> handle_response | |
end | |
def run(query) do | |
run(query, []) | |
end | |
defp handle_response(%{data: data}) do | |
case data do | |
# Errors | |
%{"first_error" => error} -> {:error, error} | |
%{"r" => errors} -> raise List.first errors # always db error | |
_ -> | |
{:ok, data} | |
end | |
end | |
defp handle_response(%RethinkDB.Exception.ConnectionClosed{}) do | |
raise "Cannot connect to RethinkDB" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment