improve error handling for bad queries