From 60a9b89d01babe1d261d42a698ea906f4b293b59 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Thu, 7 Aug 2025 19:32:41 +0200 Subject: change checkedInMessage to throw exceptions instead of returning errors --- src/main/kotlin/Connection.kt | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/main/kotlin/Connection.kt') diff --git a/src/main/kotlin/Connection.kt b/src/main/kotlin/Connection.kt index 1cef2d3..48394e0 100644 --- a/src/main/kotlin/Connection.kt +++ b/src/main/kotlin/Connection.kt @@ -51,25 +51,25 @@ class Connection(transLay: TransportLayer) : ProtocolTranslator { } /** - * Handy function to create an [InMessage] instance and check for errors. It uses [tl] and [maxSize] for instancing - * the [InMessage] class. + * Handy function to create an [InMessage] instance and check for errors. After successfully using this function, it + * is guaranteed that both no error occurred while reading the incoming message and the message is not of type + * R-error. + * + * It uses [tl] and [maxSize] for instancing the [InMessage] class. * * @return A pair of: (1) a nullable string (which can be: `null` if no error occurred, empty if an error occurred * with no message, or non-empty with the error message) and (2) the optional [InMessage] instance (null if an error * occurred). + * @throws InvalidMessageException if the received message is invalid. + * @throws RErrorException if the received message is an R-error message. */ - private fun checkedInMessage(reqTag: UShort): Pair { - val imsg: InMessage - try { - imsg = InMessage(this.tl, this.maxSize, reqTag) - } catch (ime: InvalidMessageException) { - return Pair(ime.message!!, null) - } + private fun checkedInMessage(reqTag: UShort): InMessage { + val imsg = InMessage(this.tl, this.maxSize, reqTag) if (imsg.type == NinePMessageType.RERROR) { imsg.applyField(InMessage.Field("ename", InMessage.Field.Type.STRING, 0u)) - return Pair(imsg.fieldsStr["ename"]!!, null) + throw RErrorException(imsg.fieldsStr["ename"]) } - return Pair(null, imsg) + return imsg } override fun version(msize: UInt, version: String): String? { -- cgit v1.2.3