Improved error handling in do_request