https://kotlinlang.org logo
Title
j

jean

08/26/2021, 2:11 PM
I have this data class
@Serializable
data class ApiResponse<T> (

    @Serializable(with = HttpStatusCodeSerializer::class)
    val code: HttpStatusCode,

    val errors: List<KlubberError> = emptyList(),
    val data: T,
)
Does it make sense to use a type
ApiResponse<Nothing>
in case of errors? And what should I pass for the
data
parameter?
val response = ApiResponse<Nothing> (
    code = myCode,
    errors = listOf(error1, error2),
    data = <what should I use here?>,
)
Using Unit instead of Nothing allows me to write
data = Unit
is that a better approach?
p

Paul Griffith

08/26/2021, 2:17 PM
sealed class ApiResponse {
	abstract val code: HttpStatusCode,

	data class StandardResponse<T>(
		override val code: HttpStatusCode,
		val data: T,
	) : ApiResponse()

	data class ErrorResponse(
		override val code: HttpStatusCode,
		val errors: List<KluberrError>,
	) : ApiResponse()
}
maybe something like this?
👍 4
ł

Łukasz Bednarczyk

08/26/2021, 2:17 PM
Maybe make
data
nullable?
val data: T?
j

jean

08/27/2021, 6:52 AM
@Paul Griffith yeah I guess I’ll go with that design, I guess not having the
error
property at all in case of success is a good thing