-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle errors in multisearch responses
- Loading branch information
Showing
2 changed files
with
83 additions
and
21 deletions.
There are no files selected for viewing
46 changes: 25 additions & 21 deletions
46
elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/SearchHandlers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...core/src/test/scala/com/sksamuel/elastic4s/requests/searches/MultiSearchHandlerTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.sksamuel.elastic4s.requests.searches | ||
|
||
import com.sksamuel.elastic4s.HttpEntity.StringEntity | ||
import com.sksamuel.elastic4s.requests.searches.SearchHandlers.MultiSearchHandler | ||
import com.sksamuel.elastic4s.{ElasticError, HttpResponse} | ||
import org.scalatest.EitherValues | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class MultiSearchHandlerTest extends AnyFlatSpec with Matchers with EitherValues { | ||
|
||
it should "handle error responses properly" in { | ||
val responseBody = """{"error":{"type":"some_error_type","reason":"some_error_reason","root_cause":[]},"status":400}""" | ||
val response = HttpResponse(400, Some(StringEntity(responseBody, None)), Map.empty) | ||
|
||
MultiSearchHandler.responseHandler.handle(response).left.value shouldBe ElasticError("some_error_type", "some_error_reason", None, None, None, Seq.empty, None) | ||
} | ||
|
||
it should "handle successful responses properly" in { | ||
val responseBody = | ||
"""{ | ||
| "took": 1, | ||
| "responses": [ | ||
| { | ||
| "took": 1, | ||
| "timed_out": false, | ||
| "_shards": { | ||
| "total": 1, | ||
| "successful": 1, | ||
| "skipped": 0, | ||
| "failed": 0 | ||
| }, | ||
| "hits": { | ||
| "total": { | ||
| "value": 0, | ||
| "relation": "eq" | ||
| }, | ||
| "max_score": null, | ||
| "hits": [] | ||
| }, | ||
| "status": 200 | ||
| }, | ||
| { | ||
| "error": { | ||
| "type": "some_error_type", | ||
| "reason": "some_error_reason", | ||
| "root_cause": [] | ||
| }, | ||
| "status": 400 | ||
| } | ||
| ] | ||
|}""".stripMargin | ||
val response = HttpResponse(200, Some(StringEntity(responseBody, None)), Map.empty) | ||
val mResponse = MultiSearchHandler.responseHandler.handle(response).right.value | ||
mResponse.items should have size 2 | ||
mResponse.items.map(_.status) shouldEqual Seq(200, 400) | ||
} | ||
} |