commit 5fed12a8f75cf4d8234984542de591ec869def8a
parent acfac24086c6963814e500e4a429c8ed5b729c84
Author: MS <ms@taler.net>
Date: Fri, 11 Dec 2020 15:54:37 +0100
exception handling
Diffstat:
4 files changed, 66 insertions(+), 19 deletions(-)
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
@@ -34,6 +34,7 @@ import org.slf4j.LoggerFactory
import tech.libeufin.nexus.server.serverMain
import tech.libeufin.util.CryptoUtil.hashpw
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
+import execThrowableOrTerminate
import tech.libeufin.nexus.iso20022.parseCamtMessage
import tech.libeufin.util.DEFAULT_DB_CONNECTION
import tech.libeufin.util.XMLUtil
@@ -80,12 +81,9 @@ class ResetTables : CliktCommand("Drop all the tables from the database") {
}
private val dbConnString by option().default(DEFAULT_DB_CONNECTION)
override fun run() {
- try {
+ execThrowableOrTerminate {
dbDropTables(dbConnString)
dbCreateTables(dbConnString)
- } catch (e: Exception) {
- println("Database ($dbConnString) action was unsuccessful")
- return
}
}
}
@@ -95,7 +93,9 @@ class Superuser : CliktCommand("Add superuser or change pw") {
private val username by argument()
private val password by option().prompt(requireConfirmation = true, hideInput = true)
override fun run() {
- dbCreateTables(dbConnString)
+ execThrowableOrTerminate {
+ dbCreateTables(dbConnString)
+ }
transaction {
val hashedPw = hashpw(password)
val user = NexusUserEntity.findById(username)
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt
@@ -27,6 +27,7 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
+import execThrowableOrTerminate
import io.ktor.application.ApplicationCall
import io.ktor.application.ApplicationCallPipeline
import io.ktor.application.call
@@ -209,11 +210,8 @@ fun requireBankConnection(call: ApplicationCall, parameterKey: String): NexusBan
}
fun serverMain(dbName: String, host: String) {
- try {
+ execThrowableOrTerminate {
dbCreateTables(dbName)
- } catch (e: Exception) {
- tech.libeufin.util.logger.error("Could not create tables at database: $dbName")
- return
}
val client = HttpClient {
expectSuccess = false // this way, it does not throw exceptions on != 200 responses.
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
@@ -61,6 +61,7 @@ import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.output.CliktHelpFormatter
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
+import execThrowableOrTerminate
import io.ktor.request.*
import tech.libeufin.sandbox.BankAccountTransactionsTable.amount
import tech.libeufin.sandbox.BankAccountTransactionsTable.creditorBic
@@ -100,12 +101,9 @@ class ResetTables : CliktCommand("Drop all the tables from the database") {
}
private val dbConnString by option().default(DEFAULT_DB_CONNECTION)
override fun run() {
- try {
+ execThrowableOrTerminate {
dbDropTables(dbConnString)
dbCreateTables(dbConnString)
- } catch (e: Exception) {
- println("Database ($dbConnString) action was unsuccessful")
- return
}
}
}
@@ -176,12 +174,7 @@ fun main(args: Array<String>) {
}
fun serverMain(dbName: String) {
- try {
- dbCreateTables(dbName)
- } catch (e: Exception) {
- logger.error("Could not create tables at database: $dbName")
- return
- }
+ execThrowableOrTerminate { dbCreateTables(dbName) }
val server = embeddedServer(Netty, port = 5000) {
install(CallLogging) {
this.level = Level.DEBUG
diff --git a/util/src/main/kotlin/Errors.kt b/util/src/main/kotlin/Errors.kt
@@ -0,0 +1,56 @@
+import kotlin.system.exitProcess
+
+/*
+ * This file is part of LibEuFin.
+ * Copyright (C) 2019 Stanisci and Dold.
+
+ * LibEuFin is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation; either version 3, or
+ * (at your option) any later version.
+
+ * LibEuFin is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
+ * Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public
+ * License along with LibEuFin; see the file COPYING. If not, see
+ * <http://www.gnu.org/licenses/>
+ */
+
+/**
+ * Helper function that wraps throwable code and
+ * (1) prints the error message and (2) terminates
+ * the current process, should one exception occur.
+ *
+ * Note: should be called when it is REALLY required
+ * to stop the process when the exception cannot be
+ * handled. Notably, when the database cannot be reached.
+ */
+fun execThrowableOrTerminate(func: () -> Unit) {
+ try {
+ func()
+ } catch (e: Exception) {
+ println(e.message)
+ exitProcess(1)
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+