How to serve HTTP 400 when missing Location parameters?

Viewed 28

Consider this simple ktor server setup

@Location("/user") data class User(val id: Long)

fun Application.module(testing: Boolean = false) {
    install(Locations)
    routing {
        get<User> {
            call.respondText("User[id: ${id}]")
        }
    }
}

Getting /user?id=123 works fine resulting in User [id 123].

However getting /user yields HTTP 404 but a HTTP 400 make much more sense here.

How can I

  1. Serve 400 instead of 404 in such case?
  2. Attach a custom error handler for missing query parameters so I can provide clients with information about what they got wrong?

sidenote: I also filled an issue in the ktor github repository for this.

Thanks in advance!

0 Answers
Related