radar / elastic

A thin veneer over HTTPotion that talks to Elastic Search
https://hex.pm/packages/elastic
MIT License
65 stars 22 forks source link

Handle error case for search in Document.API? #29

Open Anderssorby opened 4 years ago

Anderssorby commented 4 years ago

Regarding this line: https://github.com/radar/elastic/blob/1e2c7a3fe7b28bfa6cc574b10fd7bc6dca3dadfa/lib/elastic/document/api.ex#L190

Could we handle the error case for the Document.API.search as well? If for example the index doesn't exist it creates a nasty exception.

Anderssorby commented 4 years ago

Something like:

case result do
          {:error, code, error} ->
            {:error, code, error}
          {:ok, 200, %{"hits" => %{"hits" => hits}}} ->
            Enum.map(hits, fn %{"_source" => source, "_id" => id} ->
              into_struct(id, source)
            end)
end

I can make a PR. Then you can have the option to deal with the errors without breaking existing code.

radar commented 4 years ago

Yes please do submit a PR for this.