Skip to content

Commit

Permalink
Add ErrorSource to ErrorDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
lily-es committed Sep 5, 2024
1 parent 9e9635e commit 4d69276
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
12 changes: 12 additions & 0 deletions adapter-protobuf-java/src/main/proto/adapter.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ data class ActionResponseMessage(val attachments: List<AdapterAttachment> = empt

operator fun plus(trouble: Throwable): ActionResponseMessage {
return copy(
attachments = attachments + AdapterAttachment.ErrorDescription.from(trouble)
attachments = attachments + AdapterAttachment.ErrorDescription.from(trouble, null)
)
}

Expand All @@ -57,7 +57,11 @@ data class ActionResponseMessage(val attachments: List<AdapterAttachment> = 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
))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ internal class ErrorCodeDescriptionTest {
AdapterAttachment.ErrorDescription(
code = "bad.really.bad",
debugMessage = "This is bad..",
permanent = true
permanent = true,
errorSource = null
)
))
}
Expand All @@ -96,7 +97,8 @@ internal class ErrorCodeDescriptionTest {
AdapterAttachment.ErrorDescription(
code = "semi.bad",
debugMessage = "This is semi bad..",
permanent = false
permanent = false,
errorSource = null
)
))
}
Expand All @@ -115,7 +117,8 @@ internal class ErrorCodeDescriptionTest {
AdapterAttachment.ErrorDescription(
code = "semi.bad",
debugMessage = "Could be worse...",
permanent = false
permanent = false,
errorSource = null
)
))
}
Expand Down Expand Up @@ -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
)
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 4d69276

Please sign in to comment.