Skip to content

Commit

Permalink
Show request body on serialization error (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulraja authored Jun 5, 2024
1 parent 3741d2b commit f7eccf1
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,32 @@ package com.xebia.functional.openai.errors

import io.ktor.client.call.*
import io.ktor.client.statement.*
import io.ktor.http.content.*

suspend inline fun <reified A> HttpResponse.serializeOrThrowWithResponseInfo(): A =
try {
this.body() ?: throw ResponseSerializerError("Response body is null")
} catch (e: Exception) {
throw ResponseSerializerError("Failed to deserialize response body:\n${bodyAsText()}", e)
val requestBody =
when (val content = this.request.content) {
is OutgoingContent.ByteArrayContent -> content.bytes().decodeToString()
is OutgoingContent.NoContent -> "NoContent"
is OutgoingContent.ProtocolUpgrade -> "ProtocolUpgrade"
is OutgoingContent.ReadChannelContent -> "ReadChannelContent"
is OutgoingContent.WriteChannelContent -> "WriteChannelContent"
else -> "UnknownContent"
}
throw ResponseSerializerError(
"""
|Failed to serialize response body to ${A::class.simpleName}
|Request URL: ${this.request.url}
|Request Method: ${this.request.method}
|Request Body: $requestBody
|Response Status: ${this.status}
|Response Headers: ${this.headers}
|Response Body: ${this.bodyAsText()}
"""
.trimMargin(),
e
)
}

0 comments on commit f7eccf1

Please sign in to comment.