Commit eea74e0f authored by Konstantin Munichev's avatar Konstantin Munichev
Browse files

[dlt] Basic error handling

parent 5f3d57ca
Loading
Loading
Loading
Loading
+16 −5
Original line number Diff line number Diff line
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.features.*
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.serialization.ExperimentalSerializationApi
@@ -43,10 +44,20 @@ suspend fun main(args: Array<String>) {
        .build()
    val cfgBytes = cfg.toByteArray()

    val client = HttpClient(CIO)
    val config = client.post<ByteArray>("http://localhost:8080/dlt/configure") {
    val client = HttpClient(CIO) {
        HttpResponseValidator {
            validateResponse { response ->
                println(response.status)
            }
        }
    }

    try {
        client.post<ByteArray>("http://localhost:8080/dlt/configure") {
            contentType(ContentType.Any)
            body = cfgBytes
        }
    println(DltConfig.parseFrom(config))
    } catch (e: ClientRequestException) {
        println(e.response.status)
    }
}
 No newline at end of file
+14 −8
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@ package http
import fabric.FabricConnector
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
@@ -18,13 +18,19 @@ fun main() {
        routing {
            post("/dlt/configure") {
                withContext(Dispatchers.IO) {
                    try {
                        val data = call.receiveStream()
                        val config = parseFrom(data)
                        println(config)
                        val connector = FabricConnector(config)
                        connector.connect()
                    val encodedBack = config.toByteArray()
                    call.respond(encodedBack)
                        call.response.status(HttpStatusCode.Created)
                    }
                    // TODO: catch exceptions one by one
                    catch (e: Exception) {
                        call.response.status(HttpStatusCode.BadRequest)
                        e.printStackTrace()
                    }
                }
            }
        }