使用requests.raise_for_status()时,如何捕获404并输出自定义错误信息?
问题
之前使用requests库时,404错误未被正确捕获,而是被传递给其他异常。引入raise_for_status()方法后,虽然能触发HTTPError终止代码,但无法打印自定义错误信息,仅输出栈跟踪。
原代码
try: response = requests.request( request_type, url, headers=headers, data=payload ) response.raise_for_status() except requests.ConnectionError as ce: logger.info( f"""It appears you may need to check your internet connection - {ce}""" ) except requests.exceptions.HTTPError as he: if response.raise_for_status() == 404: logger.info( f"""We seem to be having an issue with your request - {he.response}""" ) logger.info(f"request - {he.request}") return response
404错误触发的栈跟踪
Traceback (most recent call last): File "/home/user/projects/project_code/url_help/url_help.py", line 51, in connect_to_url response.raise_for_status() File "/home/user/projects/project_code/env/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api-url.com/item/v3/companies/8951507/persons?page=1&pageSize=10 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/user/projects/project_code/main.py", line 99, in <module> main(args) File "/home/user/projects/project_code/main.py", line 50, in main ).get_employees_from_company_id(PAGE_SIZE, COMPANY_ID) File "/home/user/projects/project_code/employee_dict/employee_dict.py", line 109, in get_employees_from_company_id response = Url.connect_to_url( File "/home/user/projects/project_code/url_help/url_help.py", line 60, in connect_to_url if response.raise_for_status() == 404: File "/home/user/projects/project_code/env/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api-url.com/item/v3/companies/8951507/persons?page=1&pageSize=10
需要修改代码以正常打印自定义错误信息,并保留错误代码记录。
解决方案
核心问题是except块中再次调用response.raise_for_status()——该方法仅会抛出异常,不会返回状态码,因此这行代码会再次触发HTTPError,导致栈跟踪覆盖自定义日志逻辑。
修正后的代码
try: response = requests.request( request_type, url, headers=headers, data=payload ) response.raise_for_status() except requests.ConnectionError as ce: logger.info( f"It appears you may need to check your internet connection - {ce}" ) except requests.exceptions.HTTPError as he: # 通过捕获的异常对象获取响应状态码 if he.response.status_code == 404: logger.info( f"We seem to be having an issue with your request - {he.response}" ) logger.info(f"request - {he.request}") # 可选:处理其他HTTP错误场景 else: logger.info(f"HTTP Error occurred: {he}") return response
关键说明
raise_for_status()无返回值,仅在状态码非2xx时抛出HTTPError,不能用于判断状态码。- 捕获HTTPError后,可通过
he.response.status_code直接获取响应状态码,或直接使用response.status_code(response对象在except块中仍可访问)。 - 修正后,404错误会触发自定义日志输出,不会再次抛出未处理的异常。
内容的提问来源于stack exchange,提问作者Ryan
相关产品推荐
相关产品推荐

