Skip to content

Commit

Permalink
Add ErrorSource attachment (#297)
Browse files Browse the repository at this point in the history
* Add ErrorSource attachment

* Convert ErrorSource to data class with enum

* Rename Door to Thing

* Rename and add network error
  • Loading branch information
lily-es authored Sep 9, 2024
1 parent b2e9a8c commit 8180900
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
16 changes: 16 additions & 0 deletions adapter-protobuf-java/src/main/proto/adapter.proto
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ message Attachment {

// Says something about why an action request was denied.
DeniedReason denied_reason = 6;

// Says something about the type of an error
ErrorCategory error_category = 7;
}

message Code {
Expand Down Expand Up @@ -297,6 +300,19 @@ message Attachment {
string number = 1;
}

enum ErrorCategory {
// Adapters might call APIs to perform certain pre-requisite actions before opening a door.
// This can be for authentication, verification, etc.
// This category should be used when an API is offline or returns an error
API_ERROR = 0;

// Physical device that controls the door/light/etc is offline or returns an error
THING_ERROR = 1;

// General network issue
NETWORK_ERROR = 2;
}

}

message TextContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sealed class AdapterAttachment {

operator fun plus(other: AdapterAttachment): ActionResponseMessage {
if (this.javaClass.isAssignableFrom(other.javaClass)) {
throw IllegalStateException("Can't add more then one ${other.javaClass.simpleName}")
throw IllegalStateException("Can't add more than one ${other.javaClass.simpleName}")
}

return ActionResponseMessage(
Expand Down Expand Up @@ -90,4 +90,17 @@ sealed class AdapterAttachment {
}
}
}

data class ErrorCategory(val value: Value) : AdapterAttachment() {

enum class Value {
API_ERROR, THING_ERROR, NETWORK_ERROR;
}

companion object {
val ApiError = ErrorCategory(Value.API_ERROR)
val ThingError = ErrorCategory(Value.THING_ERROR)
val NetworkError = ErrorCategory(Value.NETWORK_ERROR)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ object ProtobufParser {
)
}

Adapter.Attachment.ValueCase.ERROR_CATEGORY -> {
when(attachment.errorCategory) {
Adapter.Attachment.ErrorCategory.API_ERROR -> AdapterAttachment.ErrorCategory.ApiError
Adapter.Attachment.ErrorCategory.THING_ERROR -> AdapterAttachment.ErrorCategory.ThingError
Adapter.Attachment.ErrorCategory.NETWORK_ERROR -> AdapterAttachment.ErrorCategory.NetworkError
Adapter.Attachment.ErrorCategory.UNRECOGNIZED, null -> null
}
}

Adapter.Attachment.ValueCase.VALUE_NOT_SET, null -> {
log.warn("Unsupported or missing attachment: {}", attachment.valueCase)
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ object ProtobufSerializer {
)
.build()
}

is AdapterAttachment.ErrorCategory -> {
when(attachment.value) {
AdapterAttachment.ErrorCategory.Value.THING_ERROR -> Adapter.Attachment.newBuilder().setErrorCategory(Adapter.Attachment.ErrorCategory.THING_ERROR).build()
AdapterAttachment.ErrorCategory.Value.API_ERROR -> Adapter.Attachment.newBuilder().setErrorCategory(Adapter.Attachment.ErrorCategory.API_ERROR).build()
AdapterAttachment.ErrorCategory.Value.NETWORK_ERROR -> Adapter.Attachment.newBuilder().setErrorCategory(Adapter.Attachment.ErrorCategory.NETWORK_ERROR).build()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class BackendTokenGenerator(
is AdapterAttachment.Code,
is AdapterAttachment.DeniedReason,
is AdapterAttachment.EndUserMessage,
is AdapterAttachment.ErrorDescription -> null
is AdapterAttachment.ErrorDescription,
is AdapterAttachment.ErrorCategory -> null
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,28 @@ internal class AdapterAttachmentTest {
}
}

@Test
fun `Can add error source to error description` () {
val errorDescription = AdapterAttachment.ErrorDescription("Test", "Test")
val response = errorDescription + AdapterAttachment.ErrorCategory.ThingError

val expected = ActionResponseMessage(
persistentListOf(
AdapterAttachment.ErrorDescription("Test", "Test"),
AdapterAttachment.ErrorCategory.ThingError
)
)

assertThat(response).isEqualTo(expected)
}

@Test
fun `Cant add multiple error sources`() {
val source = AdapterAttachment.ErrorCategory.ApiError

assertThrows<IllegalStateException> {
source + AdapterAttachment.ErrorCategory.ThingError
}
}

}

0 comments on commit 8180900

Please sign in to comment.