Commit a924c1a99888409b75adc50a7235601826a88cfb
1 parent
ba2766c5
Exists in
master
and in
21 other branches
Raise errors with full messages from Elasticsearch on bulk import failure.
Showing
2 changed files
with
23 additions
and
2 deletions
Show diff stats
lib/searchkick.rb
@@ -103,8 +103,12 @@ module Searchkick | @@ -103,8 +103,12 @@ module Searchkick | ||
103 | if items.any? | 103 | if items.any? |
104 | response = client.bulk(body: items) | 104 | response = client.bulk(body: items) |
105 | if response["errors"] | 105 | if response["errors"] |
106 | - first_item = response["items"].first | ||
107 | - raise Searchkick::ImportError, (first_item["index"] || first_item["delete"])["error"] | 106 | + messages = response["items"].map do |item| |
107 | + (item["index"] || item["delete"]) | ||
108 | + end.select { |item| item["error"] }.map do |item| | ||
109 | + "#{item["error"]} on item with id '#{item["_id"]}'" | ||
110 | + end.join "\n" | ||
111 | + raise Searchkick::ImportError, messages | ||
108 | end | 112 | end |
109 | end | 113 | end |
110 | end | 114 | end |
@@ -0,0 +1,17 @@ | @@ -0,0 +1,17 @@ | ||
1 | +require_relative "test_helper" | ||
2 | + | ||
3 | +class ErrorsTest < Minitest::Test | ||
4 | + def test_bulk_import_raises_with_full_message | ||
5 | + valid_dog = Dog.new(name: "2016-01-01") | ||
6 | + invalid_dog_1 = Dog.new(name: "Bucket") | ||
7 | + invalid_dog_2 = Dog.new(name: "Ol' One-Leg") | ||
8 | + index = Searchkick::Index.new "dogs" | ||
9 | + message = nil | ||
10 | + begin | ||
11 | + index.bulk_index [valid_dog, invalid_dog_1, invalid_dog_2] | ||
12 | + rescue Searchkick::ImportError => e | ||
13 | + message = e.message | ||
14 | + end | ||
15 | + assert_match /MapperParsingException.*Bucket.*Ol' One-Leg/m, message | ||
16 | + end | ||
17 | +end |