ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
12.72k stars 1.04k forks source link

What is the recommended way to install global route error handling? #1502

Closed Aditya94A closed 4 years ago

Aditya94A commented 4 years ago

I'm trying to understand if there's a way to add global error handling to all my API endpoints.

For eg: Whenever any route throws a MysqlDataTruncation, I know that somebody tried to input invalid data into the database. So, as an alternative to adding redundant checks for each parameter received with the call, I'd like to just use the database internal checks and have a generic "Invalid data input" sent to client.

However, doing the following via StatusPages does not seem to work

image

When MysqlDataTruncation is thrown, it is caught by neither the first exception declaration inside the StatusPages block, nor by the 'if' check in the second block. Am I doing something wrong or is there another way to do this?

cy6erGn0m commented 4 years ago

This should just work. Are you sure that the exception is actually thrown and not caught anywhere?

Aditya94A commented 4 years ago

My bad, had a typo 😓