suyash
09/07/2025, 7:48 PMpost {
val newTask = getTask()
val id = helper.createTask(newTask)
call.respond(HttpStatusCode.Created, mapOf("id" to id))
}
private suspend fun RoutingContext.getTask(): CreateTaskType =
runCatching { call.receive<CreateTaskType>() }.getOrElse {
throw BadRequestException("Required fields missing ")
}
--------------------------------------------------------------------------
validate<CreateTaskType> { task ->
if (task.name.length > 10)
ValidationResult.Invalid("XYz")
else ValidationResult.Valid
}
--------------------------------------------------------------------------
exception<RequestValidationException> { call, cause ->
call.respond(HttpStatusCode.BadRequest, cause.reasons.joinToString())
}
exception<BadRequestException> { call, cause ->
call.respond(HttpStatusCode.Conflict, cause.message)
}
My issue is everytime I make a Faulty API Call I get a BadRequestException => 409 with error Message "Required fields missing"
I never get a RequestValidationException => 400 with error Message
how can I prevent that and is there a better way to do such things?Bruce Hamilton
09/08/2025, 7:33 AMgetTask()
to see why the body cannot be deserialized.suyash
09/08/2025, 5:24 PMprivate suspend fun RoutingContext.getTask(): CreateTaskType =
runCatching { call.receive<CreateTaskType>() }.getOrElse { e ->
println(e.message)
throw BadRequestException("Required fields missing ")
}
I can see these errors
1. Validation error
Validation failed for CreateTaskType(name=Prepare Monthly Report ....
2. deserialization error
Failed to convert request body to class com.example.data.model.request.CreateTaskType
I am throwing the same error in both cases how can I seperate them
should I use a try catch on call.receive
and then handle RequestValidation exception and CannotTransformContentToTypeException seperately ?Bruce Hamilton
09/09/2025, 6:52 AMsuyash
09/09/2025, 7:07 PM