Rohde Fischer
02/20/2025, 1:20 PMsubdomain.error-group.specific-error
or similar. This kind of looks like it could be a hierarchy of interfaces and enums, but designing it feels... weird. So I'm wondering, has anyone approached a similar design? How do you use the types and their serialization to achieve this? Is the easiest actually a hierarchy with a single parent requirement? Or is there a better way to do this?arve
02/20/2025, 1:47 PMinterface ErrorCode {
val code: String
val message: String
}
// combined with
sealed interface Response<out T> {
data class Success<T>(val value: T) : Response<T>
data class Failure(val errors: List<ErrorCode>) : Response<Nothing>
}
// QOL code omitted for brevity
Service / Domain specific error defined in the relevant component
enum class DomainXError(override val message: String) : ErrorCode {
override val code = name
OUT_OF_STOCK("Human description"),
NOT_AUTHORIZED("User is not authorized to do thing"),
// etc
}
Service interface typically looks like
interface XDomainManager {
fun doThing(request: DoThingRequest): Response<XThing>
}
Calling code is then “strongly encouraged” to handle error case, can switch on the specific error type or pass human readable description if applicable.
when(val response = xManager.doThing()) {
is Success -> // do whatever
is Failure -> Act based on enum, or pass description to user
}