properly handle query errors