Skip to content

Commit

Permalink
Only parse inputStream when we know the response is an OK
Browse files Browse the repository at this point in the history
  • Loading branch information
creativedrewy authored and creativedrewy committed Aug 2, 2023
1 parent 0f914ea commit e9f66b5
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions solana/src/main/java/com/solana/networking/NetworkingRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import java.lang.Exception
import java.net.HttpURLConnection
import kotlin.coroutines.resumeWithException

Expand Down Expand Up @@ -41,26 +42,24 @@ class HttpNetworkingRouter(
)
outputStream.close()

// read response
val responseString = inputStream.bufferedReader().use { it.readText() }
when (responseCode) {
HttpURLConnection.HTTP_OK -> {
try {
val responseString = inputStream.bufferedReader().use { it.readText() }

// TODO: should check response code and/or errorStream for errors
println("URL : $url")
println("Response Code : $responseCode")
println("input stream : $responseString")

try {
val decoded = json.decodeFromString(
RpcResponse.serializer(resultSerializer),
responseString
)
continuation.resumeWith(
Result.success(
decoded
)
)
} catch (e: SerializationException){
continuation.resumeWithException(e)
val decoded = json.decodeFromString(
RpcResponse.serializer(resultSerializer), responseString)
continuation.resumeWith(
Result.success(decoded)
)
} catch (e: SerializationException){
continuation.resumeWithException(e)
}
}
else -> {
val errorString = errorStream.bufferedReader().use { it.readText() }
continuation.resumeWithException(Exception(errorString))
}
}
}
}
Expand Down

0 comments on commit e9f66b5

Please sign in to comment.