mudasar187
01/09/2023, 10:04 AMRequestValidationException
which is a 400 Bad Request. But I also have ClientRequestException. SO my question is. If i get 400 Bad Request which one will be triggered?
fun StatusPagesConfig.exceptionHandler() {
exception<Throwable> { call, cause ->
val (responseStatus, apiError) = when (cause) {
is RequestValidationException -> {
Pair(
HttpStatusCode.BadRequest, ApiError(
ZonedDateTime.now(),
HttpStatusCode.BadRequest.value,
HttpStatusCode.BadRequest.description,
cause.reasons.joinToString(),
call.request.path()
)
)
}
is ClientRequestException -> {
val httpStatusCode = cause.response.status
val httpStatusCodeDescription = cause.response.status.description
Pair(
httpStatusCode, ApiError(
ZonedDateTime.now(),
httpStatusCode.value,
httpStatusCodeDescription,
cause.message,
call.request.path()
)
)
}
else -> Pair(
HttpStatusCode.InternalServerError, ApiError(
ZonedDateTime.now(),
HttpStatusCode.InternalServerError.value,
HttpStatusCode.InternalServerError.description,
cause.message ?: "Uknown Exception",
call.request.path()
)
)
}
call.application.log.error(
"Error occured ${call.request.httpMethod} - ${call.request.path()} status=$responseStatus",
cause
)
call.respond(responseStatus, apiError)
}
}
I also have a RequestValidationConfig
where I do all my logic for bad handling of the form posted to the endpoint.
fun RequestValidationConfig.validationHandler() {
validate<RequestObject> { requestObject ->
fun isNumeric(toCheck: String): Boolean {
return toCheck.all { char -> char.isDigit() }
}
when {
!isNumeric(requestObject.incomeYear) -> ValidationResult.Invalid("Income year is not a number")
requestObject.incomeYear.toInt() < 2022 -> ValidationResult.Invalid("Income year can not be less than 2022")
requestObject.incomeYear.toInt() > 2023 -> ValidationResult.Invalid("Income year can not be greater than 2023")
!isNumeric(requestObject.personidentificatior) -> ValidationResult.Invalid("Personidentificatior is not a number")
requestObject.personidentificatior.length < 11 -> ValidationResult.Invalid("Personidentificatior can not be less than 11")
requestObject.personidentificatior.length > 11 -> ValidationResult.Invalid("Personidentificatior can not be greater than 11")
else -> ValidationResult.Valid
}
}
}
Aleksei Tirman [JB]
01/09/2023, 11:30 AMmudasar187
01/09/2023, 11:30 AMAleksei Tirman [JB]
01/09/2023, 11:32 AMmudasar187
01/09/2023, 11:33 AMAleksei Tirman [JB]
01/09/2023, 11:34 AMAleksei Tirman [JB]
01/09/2023, 11:34 AM