Improved error handling in http lib