Hi. I have an exception handler where i use Ktor's...
# ktor
m
Hi. I have an exception handler where i use Ktor's
RequestValidationException
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?
Copy code
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.
Copy code
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
        }
    }
}
a
What do you mean by getting the 400 Bad Request?
m
As you can see I have some validation for the request object, but if its another bad request then the validation, would it throw ClientRequestException?
a
Do you mean if during the validation a Ktor HTTP client throws the ClientRequestException?
m
AH never mind. I think I confused these two exception. ClientRequestException is typically when calling some external api and will be triggered if there any error getting back from external service.
a
The ClientRequestException is the Ktor client’s exception type
I don’t quite understand how it’s related to your server.