Better error handling for failed queries