From 4d69276e79f85192cae1109273f4d2361cf44c3b Mon Sep 17 00:00:00 2001 From: Lily Espirito Santo Date: Thu, 5 Sep 2024 12:32:31 +0100 Subject: [PATCH] Add ErrorSource to ErrorDescription --- .../src/main/proto/adapter.proto | 12 ++++++++++++ .../adapter/action/ActionResponseMessage.kt | 8 ++++++-- .../adapter/action/AdapterAttachment.kt | 13 ++++++++++--- .../adapter/error/ErrorCodeDescription.kt | 6 ++++-- .../adapter/protobuf/ProtobufParser.kt | 7 ++++++- .../adapter/protobuf/ProtobufSerializer.kt | 17 ++++++++++++----- .../adapter/error/ErrorCodeDescriptionTest.kt | 12 ++++++++---- .../adapter/protobuf/ProtobufSerializerTest.kt | 6 ++++-- 8 files changed, 62 insertions(+), 19 deletions(-) diff --git a/adapter-protobuf-java/src/main/proto/adapter.proto b/adapter-protobuf-java/src/main/proto/adapter.proto index 7d3e7dd..6083ce4 100644 --- a/adapter-protobuf-java/src/main/proto/adapter.proto +++ b/adapter-protobuf-java/src/main/proto/adapter.proto @@ -262,6 +262,18 @@ message Attachment { // true if you think that a quick retry might work. bool permanent = 3; + // Source of the error + ErrorSource errorSource = 4; + + enum ErrorSource { + // Adapters can call APIs to perform certain pre-requesite actions before opening a door. + // This can be authentication, verification, etc + EXTERNAL_API = 0; + + //TODO: Find a better name for this. + // Physical device that controls the door/light/whatever + DOOR = 1; + } } diff --git a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/ActionResponseMessage.kt b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/ActionResponseMessage.kt index 3dca63b..9d539eb 100644 --- a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/ActionResponseMessage.kt +++ b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/ActionResponseMessage.kt @@ -39,7 +39,7 @@ data class ActionResponseMessage(val attachments: List = empt operator fun plus(trouble: Throwable): ActionResponseMessage { return copy( - attachments = attachments + AdapterAttachment.ErrorDescription.from(trouble) + attachments = attachments + AdapterAttachment.ErrorDescription.from(trouble, null) ) } @@ -57,7 +57,11 @@ data class ActionResponseMessage(val attachments: List = empt companion object { fun permanentError(code: String, debugMessage: String): ActionResponseMessage { - return ActionResponseMessage(AdapterAttachment.ErrorDescription(code, debugMessage)) + return ActionResponseMessage(AdapterAttachment.ErrorDescription( + code = code, + debugMessage = debugMessage, + errorSource = null + )) } } } diff --git a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/AdapterAttachment.kt b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/AdapterAttachment.kt index 3b824e6..c50e359 100644 --- a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/AdapterAttachment.kt +++ b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/AdapterAttachment.kt @@ -67,14 +67,21 @@ sealed class AdapterAttachment { data class ErrorDescription( val code: String, val debugMessage: String, - val permanent: Boolean = true + val permanent: Boolean = true, + val errorSource: ErrorSource? ) : AdapterAttachment() { + + enum class ErrorSource { + EXTERNAL_API, DOOR + } + companion object { - fun from(trouble: Throwable): ErrorDescription { + fun from(trouble: Throwable, errorSource: ErrorSource?): ErrorDescription { return ErrorDescription( code = "exception", debugMessage = "${trouble.javaClass.name}: ${trouble.message ?: "No message"}", - permanent = true + permanent = true, + errorSource = null ) } } diff --git a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/error/ErrorCodeDescription.kt b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/error/ErrorCodeDescription.kt index 253b6fa..e3999c5 100644 --- a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/error/ErrorCodeDescription.kt +++ b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/error/ErrorCodeDescription.kt @@ -32,14 +32,16 @@ data class ErrorCodeDescription( AdapterAttachment.ErrorDescription( debugMessage = debugMessage, permanent = true, - code = code.code + code = code.code, + errorSource = null ) } TEMPORARY_ERROR -> { AdapterAttachment.ErrorDescription( debugMessage = debugMessage, permanent = false, - code = code.code + code = code.code, + errorSource = null ) } } diff --git a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufParser.kt b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufParser.kt index 72963af..d66c09a 100644 --- a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufParser.kt +++ b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufParser.kt @@ -143,7 +143,12 @@ object ProtobufParser { AdapterAttachment.ErrorDescription( code = attachment.errorDescription.code, debugMessage = attachment.errorDescription.debugMessage, - permanent = attachment.errorDescription.permanent + permanent = attachment.errorDescription.permanent, + errorSource = when(attachment.errorDescription.errorSource) { + Adapter.Attachment.ErrorDescription.ErrorSource.EXTERNAL_API -> AdapterAttachment.ErrorDescription.ErrorSource.EXTERNAL_API + Adapter.Attachment.ErrorDescription.ErrorSource.DOOR -> AdapterAttachment.ErrorDescription.ErrorSource.DOOR + Adapter.Attachment.ErrorDescription.ErrorSource.UNRECOGNIZED, null -> null + } ) } diff --git a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializer.kt b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializer.kt index 1ab9724..0785e31 100644 --- a/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializer.kt +++ b/adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializer.kt @@ -93,11 +93,18 @@ object ProtobufSerializer { is AdapterAttachment.ErrorDescription -> { Adapter.Attachment.newBuilder() .setErrorDescription( - Adapter.Attachment.ErrorDescription.newBuilder() - .setDebugMessage(attachment.debugMessage) - .setPermanent(attachment.permanent) - .setCode(attachment.code) - .build() + Adapter.Attachment.ErrorDescription.newBuilder().run { + debugMessage = attachment.debugMessage + permanent = attachment.permanent + code = attachment.code + if(attachment.errorSource != null) { + errorSource = when(attachment.errorSource) { + AdapterAttachment.ErrorDescription.ErrorSource.EXTERNAL_API -> Adapter.Attachment.ErrorDescription.ErrorSource.EXTERNAL_API + AdapterAttachment.ErrorDescription.ErrorSource.DOOR -> Adapter.Attachment.ErrorDescription.ErrorSource.DOOR + } + } + build() + } ) .build() } diff --git a/adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/error/ErrorCodeDescriptionTest.kt b/adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/error/ErrorCodeDescriptionTest.kt index 03d425e..c64685a 100644 --- a/adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/error/ErrorCodeDescriptionTest.kt +++ b/adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/error/ErrorCodeDescriptionTest.kt @@ -77,7 +77,8 @@ internal class ErrorCodeDescriptionTest { AdapterAttachment.ErrorDescription( code = "bad.really.bad", debugMessage = "This is bad..", - permanent = true + permanent = true, + errorSource = null ) )) } @@ -96,7 +97,8 @@ internal class ErrorCodeDescriptionTest { AdapterAttachment.ErrorDescription( code = "semi.bad", debugMessage = "This is semi bad..", - permanent = false + permanent = false, + errorSource = null ) )) } @@ -115,7 +117,8 @@ internal class ErrorCodeDescriptionTest { AdapterAttachment.ErrorDescription( code = "semi.bad", debugMessage = "Could be worse...", - permanent = false + permanent = false, + errorSource = null ) )) } @@ -151,7 +154,8 @@ internal class ErrorCodeDescriptionTest { AdapterAttachment.ErrorDescription( code = "unexpected", debugMessage = "java.lang.IllegalStateException: You didn't see this coming!", - permanent = true + permanent = true, + errorSource = null ) )) } diff --git a/adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializerTest.kt b/adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializerTest.kt index 281d555..b8c3890 100644 --- a/adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializerTest.kt +++ b/adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializerTest.kt @@ -85,7 +85,8 @@ internal class ProtobufSerializerTest { AdapterAttachment.ErrorDescription( debugMessage = "Some error occurred", code = "some.error", - permanent = true + permanent = true, + errorSource = AdapterAttachment.ErrorDescription.ErrorSource.DOOR ) ) val serializedResponse = ProtobufSerializer.serialize(response) @@ -101,7 +102,8 @@ internal class ProtobufSerializerTest { AdapterAttachment.ErrorDescription( debugMessage = "Some error occurred", code = "some.error", - permanent = false + permanent = false, + errorSource = AdapterAttachment.ErrorDescription.ErrorSource.EXTERNAL_API ) ) val serializedResponse = ProtobufSerializer.serialize(response)