Skip to content

Commit

Permalink
Implement delete and update by query APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed Mar 23, 2023
1 parent 815fa37 commit e27c38d
Show file tree
Hide file tree
Showing 36 changed files with 1,328 additions and 314 deletions.
11 changes: 7 additions & 4 deletions elasticmagic-transport/api/elasticmagic-transport.api
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,19 @@ public abstract class dev/evo/elasticmagic/transport/TransportError {
}

public final class dev/evo/elasticmagic/transport/TransportError$Companion {
public final fun parse (Ldev/evo/elasticmagic/serde/Deserializer$ObjectCtx;)Ldev/evo/elasticmagic/transport/TransportError;
public final fun parse (Ljava/lang/String;Ldev/evo/elasticmagic/serde/Deserializer;)Ldev/evo/elasticmagic/transport/TransportError;
}

public final class dev/evo/elasticmagic/transport/TransportError$Simple : dev/evo/elasticmagic/transport/TransportError {
public fun <init> (Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ldev/evo/elasticmagic/serde/Deserializer$ObjectCtx;)V
public synthetic fun <init> (Ljava/lang/String;Ldev/evo/elasticmagic/serde/Deserializer$ObjectCtx;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;)Ldev/evo/elasticmagic/transport/TransportError$Simple;
public static synthetic fun copy$default (Ldev/evo/elasticmagic/transport/TransportError$Simple;Ljava/lang/String;ILjava/lang/Object;)Ldev/evo/elasticmagic/transport/TransportError$Simple;
public final fun component2 ()Ldev/evo/elasticmagic/serde/Deserializer$ObjectCtx;
public final fun copy (Ljava/lang/String;Ldev/evo/elasticmagic/serde/Deserializer$ObjectCtx;)Ldev/evo/elasticmagic/transport/TransportError$Simple;
public static synthetic fun copy$default (Ldev/evo/elasticmagic/transport/TransportError$Simple;Ljava/lang/String;Ldev/evo/elasticmagic/serde/Deserializer$ObjectCtx;ILjava/lang/Object;)Ldev/evo/elasticmagic/transport/TransportError$Simple;
public fun equals (Ljava/lang/Object;)Z
public final fun getError ()Ljava/lang/String;
public final fun getObj ()Ldev/evo/elasticmagic/serde/Deserializer$ObjectCtx;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open class ElasticsearchException(msg: String) : Exception(msg) {
open val isRetriable = false

companion object {
private const val MAX_TEXT_ERROR_LENGTH = 256
private const val MAX_TEXT_ERROR_LENGTH = 1024

private fun trimText(text: String) =
text.slice(0 until text.length.coerceAtMost(MAX_TEXT_ERROR_LENGTH))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.evo.elasticmagic.transport

import dev.evo.elasticmagic.serde.DeserializationException
import dev.evo.elasticmagic.serde.Deserializer
import dev.evo.elasticmagic.serde.Serde
import dev.evo.elasticmagic.serde.Serializer
Expand Down Expand Up @@ -105,6 +104,7 @@ abstract class Request<out BodyT, ResponseT, out ResultT> {
abstract val body: BodyT?
abstract val contentType: String
abstract val errorSerde: Serde
// TODO: try to rid of from processResponse here
abstract val processResponse: (ResponseT) -> ResultT

open val acceptContentType: String? = null
Expand Down Expand Up @@ -423,16 +423,9 @@ abstract class ElasticsearchTransport(
)
}
else -> {
val jsonError = try {
request.errorSerde.deserializer.objFromStringOrNull(content)
} catch (e: DeserializationException) {
null
}
val transportError = if (jsonError != null) {
TransportError.parse(jsonError)
} else {
TransportError.Simple(content)
}
val transportError = TransportError.parse(
content, request.errorSerde.deserializer
)
ResponseResult.Error(
statusCode, response.headers, response.contentType, transportError
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.evo.elasticmagic.transport

import dev.evo.elasticmagic.serde.Deserializer
import dev.evo.elasticmagic.serde.DeserializationException
import dev.evo.elasticmagic.serde.toMap

data class ErrorReason(
Expand Down Expand Up @@ -129,23 +130,33 @@ sealed class TransportError {
}
}

data class Simple(val error: String) : TransportError()
data class Simple(
val error: String,
val obj: Deserializer.ObjectCtx? = null,
) : TransportError()

companion object {
fun parse(data: Deserializer.ObjectCtx): TransportError {
val errorData = data.objOrNull("error")
if (errorData != null) {
val error = Structured.parse(errorData)
if (error != null) {
return error
fun parse(error: String, deserializer: Deserializer): TransportError {
// TODO: Implement parsing failures for delete and update by query APIs
try {
val jsonError = deserializer.objFromString(error)
val errorObj = jsonError.objOrNull("error")
if (errorObj != null) {
val structuredError = Structured.parse(errorObj)
if (structuredError != null) {
return structuredError
}
}
}

return when (val error = data.anyOrNull("error")) {
is Deserializer.ObjectCtx -> Simple(error.toMap().toString())
null -> Simple(data.toMap().toString())
else -> Simple(error.toString())
return when (val errorData = jsonError.anyOrNull("error")) {
null -> Simple(error)
is Deserializer.ObjectCtx -> Simple(errorData.toMap().toString(), errorData)
else -> Simple(errorData.toString())
}
} catch (e: DeserializationException) {
return Simple(error)
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package dev.evo.elasticmagic.transport
import dev.evo.elasticmagic.serde.serialization.JsonDeserializer

import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf

import kotlin.test.Test

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.addJsonObject
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.put
import kotlinx.serialization.json.putJsonArray
import kotlinx.serialization.json.putJsonObject
Expand All @@ -23,13 +26,13 @@ class TransportErrorTests {
}

val ex = TransportError.parse(
JsonDeserializer.ObjectCtx(rawError)
Json.Default.encodeToString(rawError), JsonDeserializer
)
ex shouldBe TransportError.Structured("error", "error reason")
}

@Test
fun parseInvalidJson() {
fun parseNonStructuredJson() {
val rawError = buildJsonObject {
putJsonObject("error") {
putJsonArray("root_cause") {
Expand All @@ -42,9 +45,10 @@ class TransportErrorTests {
}

val ex = TransportError.parse(
JsonDeserializer.ObjectCtx(rawError)
Json.Default.encodeToString(rawError), JsonDeserializer
)
ex shouldBe TransportError.Simple("{root_cause=[{type=error, reason=error reason}]}")
ex.shouldBeInstanceOf<TransportError.Simple>()
ex.error shouldBe "{root_cause=[{type=error, reason=error reason}]}"
}

@Test
Expand All @@ -53,8 +57,9 @@ class TransportErrorTests {
put("error", "Just error message")
}
val ex = TransportError.parse(
JsonDeserializer.ObjectCtx(rawError)
Json.Default.encodeToString(rawError), JsonDeserializer
)
ex shouldBe TransportError.Simple("Just error message")
ex.shouldBeInstanceOf<TransportError.Simple>()
ex.error shouldBe "Just error message"
}
}
Loading

0 comments on commit e27c38d

Please sign in to comment.