gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] branch master updated: introducing "expect*()" helpers, + cus


From: gnunet
Subject: [libeufin] branch master updated: introducing "expect*()" helpers, + custom exception classes.
Date: Thu, 07 Nov 2019 11:59:41 +0100

This is an automated email from the git hooks/post-receive script.

marcello pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new 47275b9  introducing "expect*()" helpers, + custom exception classes.
47275b9 is described below

commit 47275b97d04bd37050b68a032e9b5a926310fec6
Author: Marcello Stanisci <address@hidden>
AuthorDate: Thu Nov 7 11:58:52 2019 +0100

    introducing "expect*()" helpers, + custom exception classes.
---
 nexus/src/main/kotlin/Main.kt | 72 +++++++++++++++++--------------------------
 1 file changed, 28 insertions(+), 44 deletions(-)

diff --git a/nexus/src/main/kotlin/Main.kt b/nexus/src/main/kotlin/Main.kt
index 9411fdc..943096a 100644
--- a/nexus/src/main/kotlin/Main.kt
+++ b/nexus/src/main/kotlin/Main.kt
@@ -73,6 +73,18 @@ fun testData() {
     }
 }
 
+fun expectId(param: String?) : Int {
+
+    try {
+        return param!!.toInt()
+    } catch (e: Exception) {
+        throw NotAnIdError(HttpStatusCode.BadRequest)
+    }
+}
+
+data class NotAnIdError(val statusCode: HttpStatusCode) : Exception("String ID 
not convertible in number")
+data class SubscriberNotFoundError(val statusCode: HttpStatusCode) : 
Exception("Subscriber not found in database")
+
 
 fun main() {
     dbCreateTables()
@@ -89,11 +101,17 @@ fun main() {
                 setPrettyPrinting()
             }
         }
+
         install(StatusPages) {
             exception<Throwable> { cause ->
-                tech.libeufin.sandbox.logger.error("Exception while handling 
'${call.request.uri}'", cause)
+                logger.error("Exception while handling '${call.request.uri}'", 
cause)
                 call.respondText("Internal server error.", 
ContentType.Text.Plain, HttpStatusCode.InternalServerError)
             }
+
+            exception<NotAnIdError> { cause ->
+                logger.error("Exception while handling '${call.request.uri}'", 
cause)
+                call.respondText("Bad request", ContentType.Text.Plain, 
HttpStatusCode.BadRequest)
+            }
         }
 
         intercept(ApplicationCallPipeline.Fallback) {
@@ -148,23 +166,13 @@ fun main() {
             }
 
             post("/ebics/subscribers/{id}/sendIni") {
-                val id = try {
-                    call.parameters["id"]!!.toInt()
-
-                } catch (e: Exception) {
-                    e.printStackTrace()
-                    call.respond(
-                        HttpStatusCode.BadRequest,
-                        NexusError(e.message.toString())
-                    )
-                    return@post
-                }
 
+                val id = expectId(call.parameters["id"])
                 val iniRequest = EbicsUnsecuredRequest()
 
                 val url = transaction {
-                    val subscriber = EbicsSubscriberEntity.findById(id)
-                    val tmpKey = 
CryptoUtil.loadRsaPrivateKey(subscriber!!.signaturePrivateKey.toByteArray())
+                    val subscriber = EbicsSubscriberEntity.findById(id) ?: 
throw SubscriberNotFoundError(HttpStatusCode.NotFound)
+                    val tmpKey = 
CryptoUtil.loadRsaPrivateKey(subscriber.signaturePrivateKey.toByteArray())
 
                     iniRequest.apply {
                         version = "H004"
@@ -176,10 +184,10 @@ fun main() {
                                     orderAttribute = "DZNNN"
                                     orderType = "INI"
                                     securityMedium = "0000"
-                                    hostID = subscriber!!.hostID
-                                    userID = subscriber!!.userID
-                                    partnerID = subscriber!!.partnerID
-                                    systemID = subscriber!!.systemID
+                                    hostID = subscriber.hostID
+                                    userID = subscriber.userID
+                                    partnerID = subscriber.partnerID
+                                    systemID = subscriber.systemID
                                 }
 
                             }
@@ -208,19 +216,9 @@ fun main() {
                             }
                         }
                     }
-                    subscriber!!.ebicsURL
-                }
-
-                if (iniRequest == null) {
-                    call.respond(
-                        HttpStatusCode.NotFound,
-                        NexusError("Could not find that subscriber")
-                    )
-                    return@post
+                    subscriber.ebicsURL
                 }
 
-                logger.info("POSTing to ${url}")
-
                 val response = try {
                     client.post<String>(
                         urlString = url,
@@ -233,7 +231,7 @@ fun main() {
 
                     call.respond(
                         HttpStatusCode.OK,
-                        NexusError("Exception thrown by HTTP client (likely 
server responded != 200).")
+                        NexusError("Did not get expected response from 
bank/sandbox")
                     )
                     return@post
                 }
@@ -255,20 +253,6 @@ fun main() {
                     return@post
                 }
             }
-
-            post("/nexus") {
-
-                val content = try {
-                    client.get<ByteArray>(
-                        "https://ebicstest1.libeufin.tech/";
-                    )
-                } catch (e: ServerResponseException) {
-                    logger.info("Request ended bad (${e.response.status}).")
-                }
-
-                call.respondText("Not implemented!\n")
-                return@post
-            }
         }
     }
 

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]