Added function to handle request errors.