joel
10/02/2023, 6:10 PMeither
for handler different errorsAndrew O'Hara
10/02/2023, 8:01 PMsealed interface AppError {
data class ResourceNotFound(val id: String): AppError
data class ResourceForbidden(val id: String): AppError
data class UserError(val messageCodes: List<String>): AppError
}
fun AppError.toResponse(): Response = when(this) {
is ResourceNotFound -> Response(Status.NOT_FOUND)
is ResourceForbidden -> Response(Status.FORBIDDEN)
is UserError -> Response(Status.BAD_REQUEST)
}.body(marshaller.asFormatString(this))
routes += "/v1/things" / idLens bindContract GET to { id ->
{ req ->
service.getThing(authLens(req), id)
.map { Response(Status.OK).with(thingLens of it) }
.recover(AppError::toResponse)
}
}
joel
10/02/2023, 8:30 PMAndrew O'Hara
10/02/2023, 8:40 PMjoel
10/02/2023, 8:45 PMAndrew O'Hara
10/02/2023, 8:57 PMEither
, or the Result
class as it's more commonly known in Kotlin, is that you don't need exceptions for error handling.
In my opinion, Exceptions are only for exceptional circumstances; cases where the only thing to do with the exception is to log it and return a generic 500. For this reason, you wouldn't use exceptions as the error type in your Either
.Kevin Fereira Pro
10/04/2023, 5:38 AMjoel
10/04/2023, 6:44 PM