libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 47275b97d04bd37050b68a032e9b5a926310fec6
parent 09f031df5c4bc39f76793c456260e756cdb76931
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Thu,  7 Nov 2019 11:58:52 +0100

introducing "expect*()" helpers, + custom exception classes.

Diffstat:
Mnexus/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 @@ -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 - } } }