libeufin

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

commit 7ea6b65786704ec04f6f42d669680c0b1e30de3c
parent f0c4f514bfdd5b60dcfc248108d4577700a7fe11
Author: MS <ms@taler.net>
Date:   Thu,  9 Jul 2020 17:35:25 +0200

Renaming the error response type.

Removing the "ebics" part from name, so as to reuse
it in any error situation.

Diffstat:
Mnexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsClient.kt | 2+-
Mnexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsNexus.kt | 12++++--------
Mnexus/src/main/kotlin/tech/libeufin/nexus/server/JSON.kt | 9++++-----
Mnexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt | 47+++++++++++++++++++++++++++--------------------
Mutil/src/main/kotlin/Ebics.kt | 5++++-
5 files changed, 40 insertions(+), 35 deletions(-)

diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsClient.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsClient.kt @@ -85,7 +85,7 @@ suspend fun doEbicsDownloadTransaction( // Success, nothing to do! } else -> { - throw NexusError( + throw EbicsProtocolError( HttpStatusCode.InternalServerError, "unexpected return code ${initResponse.technicalReturnCode}" ) diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsNexus.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/ebics/EbicsNexus.kt @@ -35,16 +35,12 @@ import io.ktor.application.call import io.ktor.client.HttpClient import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode -import io.ktor.request.receive import io.ktor.request.receiveOrNull import io.ktor.response.respond import io.ktor.response.respondText import io.ktor.routing.Route -import io.ktor.routing.get import io.ktor.routing.post -import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.insert -import org.jetbrains.exposed.sql.not import org.jetbrains.exposed.sql.statements.api.ExposedBlob import org.jetbrains.exposed.sql.transactions.transaction import tech.libeufin.nexus.* @@ -523,10 +519,10 @@ fun Route.ebicsBankConnectionRoutes(client: HttpClient) { is EbicsDownloadBankErrorResult -> { call.respond( HttpStatusCode.BadGateway, - EbicsErrorJson( - EbicsErrorDetailJson( - "bankError", - response.returnCode.errorCode + NexusErrorJson( + error = NexusErrorDetailJson( + type = "bank-error", + description = response.returnCode.errorCode ) ) ) diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/server/JSON.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/server/JSON.kt @@ -104,13 +104,12 @@ class EbicsStandardOrderParamsDateJson( } } -data class EbicsErrorDetailJson( +data class NexusErrorDetailJson( val type: String, - val ebicsReturnCode: String + val description: String ) - -data class EbicsErrorJson( - val error: EbicsErrorDetailJson +data class NexusErrorJson( + val error: NexusErrorDetailJson ) data class BankConnectionInfo( diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt @@ -82,7 +82,7 @@ fun ensureLong(param: String?): Long { } fun <T> expectNonNull(param: T?): T { - return param ?: throw EbicsProtocolError( + return param ?: throw NexusError( HttpStatusCode.BadRequest, "Non-null value expected." ) @@ -153,7 +153,7 @@ fun ApplicationRequest.hasBody(): Boolean { fun ApplicationCall.expectUrlParameter(name: String): String { return this.request.queryParameters[name] - ?: throw EbicsProtocolError(HttpStatusCode.BadRequest, "Parameter '$name' not provided in URI") + ?: throw NexusError(HttpStatusCode.BadRequest, "Parameter '$name' not provided in URI") } suspend inline fun <reified T : Any> ApplicationCall.receiveJson(): T { @@ -193,12 +193,13 @@ fun requireBankConnectionInternal(connId: String): NexusBankConnectionEntity { fun requireBankConnection(call: ApplicationCall, parameterKey: String): NexusBankConnectionEntity { val name = call.parameters[parameterKey] if (name == null) { - throw NexusError(HttpStatusCode.InternalServerError, "no parameter for bank connection") + throw NexusError(HttpStatusCode.NotFound, + "Parameter '${parameterKey}' wasn't found in URI" + ) } return requireBankConnectionInternal(name) } - fun serverMain(dbName: String, host: String) { dbCreateTables(dbName) val client = HttpClient { @@ -219,35 +220,43 @@ fun serverMain(dbName: String, host: String) { registerModule(KotlinModule(nullisSameAsDefault = true)) } } - install(StatusPages) { exception<NexusError> { cause -> logger.error("Exception while handling '${call.request.uri}'", cause) - call.respondText( - cause.reason, - ContentType.Text.Plain, - cause.statusCode + call.respond( + status = cause.statusCode, + message = NexusErrorJson( + error = NexusErrorDetailJson( + description = cause.reason, + type = "nexus-error" + ) + ) ) } exception<EbicsProtocolError> { cause -> logger.error("Exception while handling '${call.request.uri}'", cause) - call.respondText( - cause.reason, - ContentType.Text.Plain, - cause.statusCode + call.respond( + NexusErrorJson( + error = NexusErrorDetailJson( + type = "ebics-protocol-error", + description = cause.reason + ) + ) ) } exception<Exception> { cause -> logger.error("Uncaught exception while handling '${call.request.uri}'", cause) logger.error(cause.toString()) - call.respondText( - "Internal server error", - ContentType.Text.Plain, - HttpStatusCode.InternalServerError + call.respond( + NexusErrorJson( + error = NexusErrorDetailJson( + type = "nexus-error", + description = "Internal server error" + ) + ) ) } } - intercept(ApplicationCallPipeline.Fallback) { if (this.call.response.status() == null) { call.respondText("Not found (no route matched).\n", ContentType.Text.Plain, HttpStatusCode.NotFound) @@ -267,9 +276,7 @@ fun serverMain(dbName: String, host: String) { proceed() return@intercept } - startOperationScheduler(client) - routing { // Shows information about the requesting user. get("/user") { diff --git a/util/src/main/kotlin/Ebics.kt b/util/src/main/kotlin/Ebics.kt @@ -39,7 +39,10 @@ import java.util.zip.DeflaterInputStream import javax.xml.datatype.DatatypeFactory import javax.xml.datatype.XMLGregorianCalendar -data class EbicsProtocolError(val statusCode: HttpStatusCode, val reason: String) : Exception(reason) +data class EbicsProtocolError( + val httpStatusCode: HttpStatusCode, + val reason: String +) : Exception(reason) data class EbicsDateRange(val start: ZonedDateTime, val end: ZonedDateTime)