If I need to convert to objects a Okio 2 source us...
# squarelibraries
d
If I need to convert to objects a Okio 2 source using Moshi, but if there's an error, I'd like to be able to log it... this is what I have (but it looks like
peek
is not really made for this...:
Copy code
override suspend fun convertForReceive(context: PipelineContext<ApplicationReceiveRequest, ApplicationCall>): Any? = withContext(
		<http://Dispatchers.IO|Dispatchers.IO>) {
		val request = context.subject
		val channel = request.value as? ByteReadChannel ?: return@withContext null
		val reader = channel.toInputStream().source().buffer()
		val buffer2 = reader.peek()
		val type = request.type

		return@withContext moshi.adapter<Any>(type.java).runCatching {
			fromJson(reader)
		}.onFailure {
			logger.error(it) { "Error converting request body <${type.simpleName}>: ${buffer2.readUtf8()}" }
		}.getOrThrow()
	}