Did you try using Error handling? See https://world.mendix.com/display/public/howto50/Error+Handling for more details.