Skip to content

Commit 8b271d7

Browse files
committed
fix #28
1 parent 0c21d54 commit 8b271d7

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

annofabapi/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def _request_wrapper(self, http_method: str, url_path: str, query_params: Option
177177
annofabapi.utils.log_error_response(logger, response)
178178

179179
response.encoding = 'utf-8'
180-
response.raise_for_status()
180+
annofabapi.utils.raise_for_status(response)
181181

182182
content = self._response_to_content(response)
183183
return content, response
@@ -200,7 +200,7 @@ def login(self) -> Tuple[Dict[str, Any], requests.Response]:
200200

201201
url = f"{self.URL_PREFIX}/login"
202202
response = self.session.post(url, json=login_info)
203-
response.raise_for_status()
203+
annofabapi.utils.raise_for_status(response)
204204

205205
json_obj = response.json()
206206
self.token_dict = json_obj["token"]

annofabapi/api2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def _request_wrapper(self, http_method: str, url_path: str, query_params: Option
8181
annofabapi.utils.log_error_response(logger, response)
8282

8383
response.encoding = 'utf-8'
84-
response.raise_for_status()
84+
annofabapi.utils.raise_for_status(response)
8585

8686
content = self.api._response_to_content(response)
8787
return content, response

annofabapi/utils.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212
import requests
1313

1414

15+
def raise_for_status(response: requests.Response):
16+
"""
17+
HTTP Status CodeがErrorの場合、`requests.exceptions.HTTPError`を発生させる。
18+
そのとき`response.text`もHTTPErrorに加えて、HTTPError発生時にエラーの原因が分かるようにする。
19+
20+
Args:
21+
response: Response
22+
23+
Raises:
24+
requests.exceptions.HTTPError:
25+
26+
"""
27+
try:
28+
response.raise_for_status()
29+
except requests.exceptions.HTTPError as e:
30+
http_error_msg = f"{e.args[0]} , {response.text}"
31+
e.args = (http_error_msg,)
32+
raise e
33+
34+
1535
def log_error_response(arg_logger: logging.Logger, response: requests.Response):
1636
"""
1737
HTTP Statusが400以上ならば、loggerにresponse/request情報を出力する
@@ -39,7 +59,7 @@ def download(url: str, dest_path: str):
3959
4060
"""
4161
response = requests.get(url)
42-
response.raise_for_status()
62+
raise_for_status(response)
4363

4464
p = Path(dest_path)
4565
p.parent.mkdir(parents=True, exist_ok=True)

annofabapi/wrapper.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def upload_file_to_s3(self, project_id: str, file_path: str, content_type: Optio
199199
headers={'content-type': new_content_type})
200200

201201
annofabapi.utils.log_error_response(logger, res_put)
202-
res_put.raise_for_status()
202+
annofabapi.utils.raise_for_status(res_put)
203203
return content["path"]
204204

205205
def put_input_data_from_file(self, project_id: str, input_data_id: str, file_path: str,
@@ -648,7 +648,7 @@ def upload_instruction_image(self, project_id: str, image_id: str, file_path: st
648648
res_put = self.api.session.put(s3_url, params=query_dict, data=f,
649649
headers={'content-type': new_content_type})
650650
annofabapi.utils.log_error_response(logger, res_put)
651-
res_put.raise_for_status()
651+
annofabapi.utils.raise_for_status(res_put)
652652
return content["path"]
653653

654654
#########################################

0 commit comments

Comments
 (0)