Skip to content

Commit 4a8e8a0

Browse files
mishaschwartzdavid-yz-liu
authored andcommitted
error: report errors as valid json
1 parent 497a1b2 commit 4a8e8a0

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

markusapi/response_parser.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import json
12
from functools import wraps
23
from typing import List, Union, Dict, Callable
34

45

6+
class ResponseParsingException(Exception):
7+
pass
8+
9+
510
def parse_response(expected: str) -> Callable:
611
"""
712
Decorator for a function that returns a requests.Response object.
@@ -22,7 +27,10 @@ def _parser(f):
2227
def _f(*args, **kwargs):
2328
response = f(*args, **kwargs)
2429
if not response.ok or expected == "json":
25-
return response.json()
30+
try:
31+
return response.json()
32+
except json.decoder.JSONDecodeError:
33+
raise ResponseParsingException(f"status: {response.status_code}\n\n{response.text}")
2634
if expected == "content":
2735
return response.content
2836
if expected == "text":

0 commit comments

Comments
 (0)