|
| 1 | +import json |
| 2 | + |
| 3 | +import defusedxml.ElementTree as ET |
| 4 | + |
| 5 | + |
| 6 | +class ProblemDecoder(json.JSONDecoder): |
| 7 | + def decode(self, s): |
| 8 | + data = super().decode(s) |
| 9 | + if isinstance(data, dict): |
| 10 | + return self._make_problem(data) |
| 11 | + return data |
| 12 | + |
| 13 | + def _make_problem(self, data): |
| 14 | + type_ = data.pop("type", None) |
| 15 | + status = data.pop("status", None) |
| 16 | + title = data.pop("title", None) |
| 17 | + detail = data.pop("detail", None) |
| 18 | + instance = data.pop("instance", None) |
| 19 | + return Problem(type_, status, title, detail, instance, **data) |
| 20 | + |
| 21 | + |
| 22 | +class Problem(Exception): |
| 23 | + def __init__(self, type_, status, title, detail, instance, **kwargs): |
| 24 | + self._type = type_ or "about:blank" |
| 25 | + self.status = status |
| 26 | + self.title = title |
| 27 | + self.detail = detail |
| 28 | + self.instance = instance |
| 29 | + self.extras = kwargs |
| 30 | + |
| 31 | + for k in self.extras: |
| 32 | + if k in ["type", "status", "title", "detail", "instance"]: |
| 33 | + raise ValueError(f"Reserved key '{k}' used in Problem extra arguments.") |
| 34 | + |
| 35 | + def __str__(self): |
| 36 | + return self.title |
| 37 | + |
| 38 | + def __repr__(self): |
| 39 | + return f"{self.__class__.__name__}<type={self._type}; status={self.status}; title={self.title}>" |
| 40 | + |
| 41 | + def msg(self): |
| 42 | + msg = ( |
| 43 | + f"type = {self._type}, " |
| 44 | + f"status = {self.status}, " |
| 45 | + f"title = {self.title}, " |
| 46 | + f"detail = {self.detail}, " |
| 47 | + f"instance = {self.instance}, " |
| 48 | + f"extras = {self.extras}" |
| 49 | + ) |
| 50 | + return msg |
| 51 | + |
| 52 | + @property |
| 53 | + def type(self): |
| 54 | + return self._type |
| 55 | + |
| 56 | + |
| 57 | +class LegacyProblemDecoder(json.JSONDecoder): |
| 58 | + def decode(self, s): |
| 59 | + data = super().decode(s) |
| 60 | + if isinstance(data, dict): |
| 61 | + return self._make_legacy_problem(data) |
| 62 | + return data |
| 63 | + |
| 64 | + def _make_legacy_problem(self, data): |
| 65 | + request_id = None |
| 66 | + error_code = None |
| 67 | + code_type = None |
| 68 | + |
| 69 | + if "__type" in data: |
| 70 | + error_code = data.get("__type") |
| 71 | + else: |
| 72 | + request_id = (data.get("ResponseContext") or {}).get("RequestId") |
| 73 | + errors = data.get("Errors") |
| 74 | + if errors: |
| 75 | + error = errors[0] |
| 76 | + error_code = error.get("Code") |
| 77 | + reason = error.get("Type") |
| 78 | + if error.get("Details"): |
| 79 | + code_type = reason |
| 80 | + else: |
| 81 | + code_type = None |
| 82 | + return LegacyProblem(None, error_code, code_type, request_id, None) |
| 83 | + |
| 84 | + |
| 85 | +class LegacyProblem(Exception): |
| 86 | + def __init__(self, status, error_code, code_type, request_id, url): |
| 87 | + self.status = status |
| 88 | + self.error_code = error_code |
| 89 | + self.code_type = code_type |
| 90 | + self.request_id = request_id |
| 91 | + self.url = url |
| 92 | + |
| 93 | + def msg(self): |
| 94 | + msg = ( |
| 95 | + f"status = {self.status}, " |
| 96 | + f"code = {self.error_code}, " |
| 97 | + f"{'code_type = ' if self.code_type is not None else ''}" |
| 98 | + f"{self.code_type + ', ' if self.code_type is not None else ''}" |
| 99 | + f"request_id = {self.request_id}, " |
| 100 | + f"url = {self.url}" |
| 101 | + ) |
| 102 | + return msg |
| 103 | + |
| 104 | + |
| 105 | +def api_error(response): |
| 106 | + try: |
| 107 | + problem = None |
| 108 | + ct = response.headers.get("content-type") or "" |
| 109 | + if "application/json" in ct: |
| 110 | + problem = response.json(cls=LegacyProblemDecoder) |
| 111 | + problem.status = problem.status or str(response.status_code) |
| 112 | + problem.url = response.url |
| 113 | + elif "application/problem+json" in ct: |
| 114 | + problem = response.json(cls=ProblemDecoder) |
| 115 | + problem.status = problem.status or str(response.status_code) |
| 116 | + |
| 117 | + if problem: |
| 118 | + return problem |
| 119 | + except json.JSONDecodeError: |
| 120 | + pass |
| 121 | + |
| 122 | + try: |
| 123 | + error = ET.fromstring(response.text) |
| 124 | + |
| 125 | + err = dict() |
| 126 | + for key, attr in [ |
| 127 | + ("Code", "error_code"), |
| 128 | + ("Message", "status"), |
| 129 | + ("RequestId", "request_id"), |
| 130 | + ("RequestID", "request_id"), |
| 131 | + ]: |
| 132 | + value = next((x.text for x in error.iter() if x.tag.endswith(key)), None) |
| 133 | + if value: |
| 134 | + err[attr] = value |
| 135 | + |
| 136 | + return LegacyProblem(**err) |
| 137 | + except: |
| 138 | + raise Exception( |
| 139 | + f"Could not decode error response from {response.url} with status code {response.status_code}" |
| 140 | + ) |
0 commit comments