diff --git a/lib/elasticsearch/exception.ex b/lib/elasticsearch/exception.ex index eda8b65..4928168 100644 --- a/lib/elasticsearch/exception.ex +++ b/lib/elasticsearch/exception.ex @@ -70,6 +70,18 @@ defmodule Elasticsearch.Exception do ] end + defp build(error, query) when is_map(error) do + [ + status: nil, + line: nil, + col: nil, + message: error["message"], + type: nil, + query: query, + raw: error + ] + end + defp build(error, query) when is_binary(error) do binary_error(error, query) end diff --git a/test/elasticsearch/exception_test.exs b/test/elasticsearch/exception_test.exs index 138f75b..f5edbd8 100644 --- a/test/elasticsearch/exception_test.exs +++ b/test/elasticsearch/exception_test.exs @@ -59,5 +59,26 @@ defmodule Elasticsearch.ExceptionTest do query: nil ) end + + # See issue: https://github.com/infinitered/elasticsearch-elixir/issues/39 + @tag :regression + test "handles arbitrary error maps" do + assert %Exception{type: nil, message: nil, query: nil, raw: %{"message" => nil}} = + Exception.exception(%{ + response: %{"message" => nil}, + query: nil + }) + + assert %Exception{ + type: nil, + message: "Unable to connect to the server.", + query: nil, + raw: %{"message" => "Unable to connect to the server.", "ok" => false} + } = + Exception.exception(%{ + response: %{"message" => "Unable to connect to the server.", "ok" => false}, + query: nil + }) + end end end