Henrik Johansson
04/11/2023, 6:40 AMjavax.validation
. That way the restrictions on the request type stays very close to the type and it becomes very little code.Aleksei Tirman [JB]
04/11/2023, 6:55 AMCLOVIS
04/11/2023, 7:57 AMrequire
in your constructors.
@Serializable
data class Password(
val text: String,
) {
init {
require(text.length > 8) { "The password is too short" }
}
}
post("setPassword") {
val password = body.receive<Password>()
}
Henrik Johansson
04/11/2023, 8:40 AMHenrik Johansson
04/11/2023, 8:42 AMStephen
05/09/2023, 4:23 PMrequire
in init blocks is great for enforcing invariants, but if deserialization fails one of the checks ktor emits a "BadRequestException" with the require message suppressed.
The only way I've found to pass the particulars of the failure back to the caller is by capturing the exception with a status page and using Internal api rootCause. Do we have a more stable way to support invariants?CLOVIS
05/09/2023, 4:32 PMStephen
05/09/2023, 4:48 PMcause.message
? You get something akin to
Failed to convert request body to class com.jetbrains.Password
Which, while true, doesn't give helpful feedback to the caller. In the same way as "The password is too short"
does if you use cause.rootCause
Sorry if I'm missing something obvious here—CLOVIS
05/09/2023, 5:04 PMHenrik Johansson
05/11/2023, 7:52 AM