Hi guys, I have a following `DTO` ```data class C...
# server
t
Hi guys, I have a following
DTO
Copy code
data class CompanyCreateDto(
    val name: String,
    val address: String
)
The dto is send by front end for me. Problem is when the name is null kotlin throws an exception that can not be handled. Is it any library or any approach, that provide an ability to give a custom exception with text like
name can not be null
for example
m
I think you need to mention what server framework you're using, and/or what unmarshalling lib? That's probably what's throwing the exception, not Kotlin itself.
👆 1
a
And make the field nullable, or when that's not valid in your model, the exception is valid, and prevent that it tries to create the object in the first place with invalid data
m
I think the error is fine and the field should be left non-nullable as the situation clearly shows that name has to have a value. Thus said, I agree with Matheus above: you should check how your specific framework (ktor? spring?) or deserialization library (jackson? gson?) handles the error. There’s no exception that can’t be handled, you just have to find the right extension point 😉
r
require(name != null) { "name can not be null" }