Skip to content

Instantly share code, notes, and snippets.

@sikanhe
Created February 25, 2016 06:29
Show Gist options
  • Save sikanhe/a749e4e8ae35903c0a86 to your computer and use it in GitHub Desktop.
Save sikanhe/a749e4e8ae35903c0a86 to your computer and use it in GitHub Desktop.
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