Commit 19a03896efcc7ba1c6cf233f1ab9b83ce7801a6a

Authored by Andrew Kane
Committed by GitHub
2 parents 7814670d 1b5f07cd

Merge pull request #631 from audreyschwarz/report_full_bulk_index_error_message

Raise with message on bulk import error
Showing 2 changed files with 24 additions and 2 deletions   Show diff stats
lib/searchkick.rb
... ... @@ -107,8 +107,10 @@ module Searchkick
107 107 if items.any?
108 108 response = client.bulk(body: items)
109 109 if response["errors"]
110   - first_item = response["items"].first
111   - raise Searchkick::ImportError, (first_item["index"] || first_item["delete"])["error"]
  110 + first_with_error = response["items"].map do |item|
  111 + (item["index"] || item["delete"])
  112 + end.find { |item| item["error"] }
  113 + raise Searchkick::ImportError, "#{first_with_error["error"]} on item with id '#{first_with_error["_id"]}'"
112 114 end
113 115 end
114 116 end
... ...
test/errors_test.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  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-02")
  6 + invalid_dog = Dog.new(name: "Ol' One-Leg")
  7 + index = Searchkick::Index.new "dogs", mappings: {
  8 + dog: {
  9 + properties: {
  10 + name: { type: "date" }
  11 + }
  12 + }
  13 + }
  14 + index.store valid_dog
  15 + error = assert_raises(Searchkick::ImportError) do
  16 + index.bulk_index [valid_dog, invalid_dog]
  17 + end
  18 + assert_match /MapperParsingException.*Ol' One-Leg/, error.message
  19 + end
  20 +end
... ...