Correctly handle query errors