Skip to content
This repository was archived by the owner on Jun 29, 2023. It is now read-only.

Commit 384a5a7

Browse files
committed
raise exception when http request returns not ok
1 parent e566c35 commit 384a5a7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

mvg_api/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,28 @@
1313
interruptions_url = "https://www.mvg.de/.rest/betriebsaenderungen/api/interruptions"
1414
id_prefix = "de:09162:"
1515

16+
17+
class ApiError(Exception):
18+
"""
19+
Some error was returned by the mvg api when handling the request.
20+
:ivar code: status code returned by the API
21+
:ivar reason: response given by the api (optional)
22+
"""
23+
def __init__(self, code, reason):
24+
self.code = code
25+
self.reason = reason
26+
27+
def __str__(self):
28+
out = f"Got status code {self.code}"
29+
if self.reason:
30+
out += f" with response {self.reason}"
31+
return out
32+
33+
1634
def _convert_id(old_id: int) -> str:
1735
return id_prefix + str(old_id)
1836

37+
1938
def _station_sanity_check(id:str):
2039
"""
2140
New ID format has these specifications:
@@ -42,6 +61,11 @@ def _perform_api_request(url):
4261
'Accept': 'application/json'
4362
}
4463
)
64+
if not resp.ok:
65+
try:
66+
raise ApiError(resp.status_code, resp.json())
67+
except ValueError:
68+
raise ApiError(resp.status_code)
4569
return resp.json()
4670

4771

0 commit comments

Comments
 (0)