better error handling for prepared queries