Skip to content
2 changes: 1 addition & 1 deletion src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class ApiClientHelper private constructor(
val responseBody = responseBytes.asString()
logErrorResponseIfNeeded(requestId, requestTime, httpResponse.statusCode, responseBody)
val ecwidError = if (responseBody.isNotBlank()) {
jsonTransformer.deserialize(responseBody, EcwidApiError::class.java)
jsonTransformer.deserializeOrNull(responseBody, EcwidApiError::class.java)
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package com.ecwid.apiclient.v3.jsontransformer

interface JsonTransformer {
fun serialize(src: Any?, srcExt: Any? = null): String
fun <V> deserialize(json: String, clazz: Class<V>): V?
fun <V> deserialize(json: String, clazz: Class<V>): V
fun <V> deserializeOrNull(json: String, clazz: Class<V>): V?
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ class GsonTransformer(polymorphicTypes: List<PolymorphicType<*>>) : JsonTransfor
}
}

override fun <V> deserialize(json: String, clazz: Class<V>): V? {
override fun <V> deserialize(json: String, clazz: Class<V>): V {
try {
return gson.fromJson(json, clazz)
} catch (e: JsonParseException) {
throw JsonDeserializationException(e.message, e)
}
}

override fun <V> deserializeOrNull(json: String, clazz: Class<V>): V? {
return try {
return gson.fromJson(json, clazz)
} catch (e: JsonParseException) {
null
}
}
}

private fun JsonObject.mergeJsonObject(from: JsonObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ internal class GsonTransformerTest {
)
}

@Test
fun `deserializeOrNull of broken ParsedResponseWithExt`() {
val json = "'testField': {'baseField': 'base', 'extField': 'ext'}}"
val deserializedValue = transformer.deserializeOrNull(json, TestParsedResponseWithExt::class.java)
assertEquals(null, deserializedValue)
}

}

private fun assertJsonEquals(expected: String, actual: String) {
Expand Down