우리앱에서는 Retrofit의 Response()
을 따로 Network Handling 하는 기능이 있습니다.
suspend fun <T> Response<T>.executeNetworkHandling(): T {
val handle = if (isSuccessful) {
null
} else {
val gson = Gson()
val errorResponse = gson.fromJson(errorBody()!!.string(), ErrorResponse::class.java)
DefaultHandleServerStatus(errorResponse.error)
}
return body().executeErrorHandling(handle)
}
하지만 errorBody()에 값이 있는데도 불구하고 val errorResponse = gson.fromJson(errorBody()!!.string(), ErrorResponse::class.java)
을 지나면 값이 사라집니다.
string()
이 무엇인지 알아보겠습니다.
fun string(): String = source().use { source ->
source.readString(charset = source.readBomAsCharset(charset()))
}
use
를 사용하였는데, 다 read하고 나면 close하는 편한 코틀린 만의 함수이기 때문에
읽고나서 지혼자 값이 사라졌던 것입니다.
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
var exception: Throwable? = null
try {
return block(this)
} catch (e: Throwable) {
exception = e
throw e
} finally {
when {
apiVersionIsAtLeast(1, 1, 0) -> this.closeFinally(exception)
this == null -> {}
exception == null -> close()
else ->
try {
close()
} catch (closeException: Throwable) {
// cause.addSuppressed(closeException) // ignored here
}
}
}
결국 String 변수를 만들어 값을 저장해서 해결하셨습니다 <3