Skip to content

Commit 3fac4bf

Browse files
committed
fix: show stack traces only for exceptions in the system console
1 parent b2e093d commit 3fac4bf

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

api/client.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ColorTextureRecorderJson,
1111
EnvironmentJson,
1212
EquirectCameraJson,
13-
GenericError,
13+
ProblemJson,
1414
GetCameraJson,
1515
GroundBodyJson,
1616
ObjectJson,
@@ -43,23 +43,26 @@ def from_context(context: Context) -> "APIClient":
4343
def get_api_version(self) -> Result[ApiVersionJson, str]:
4444
return self._get("api/version", assert_compat=False).bind(self._parse_json_response)
4545

46-
def is_api_supported(self) -> bool:
47-
if self.api_version is None:
48-
api_version = self.get_api_version().unwrap_or(None)
49-
if api_version:
50-
self.api_version = (api_version["major"], api_version["minor"])
46+
def assert_compatability(self):
47+
get_version_error: str | None = None
5148

5249
if self.api_version is None:
53-
return False
50+
get_version_result = self.get_api_version()
51+
if get_version_result.is_ok:
52+
api_version = get_version_result.unwrap()
53+
self.api_version = (api_version["major"], api_version["minor"])
54+
else:
55+
get_version_error = get_version_result.unwrap_error()
5456

55-
return self.api_version[0] == ACCEPTED_API_VERSION[0] and self.api_version[1] >= ACCEPTED_API_VERSION[1]
57+
is_api_supported = self.api_version is not None and (
58+
self.api_version[0] == ACCEPTED_API_VERSION[0] and self.api_version[1] >= ACCEPTED_API_VERSION[1]
59+
)
5660

57-
def assert_compatability(self):
58-
if not self.is_api_supported():
61+
if not is_api_supported:
5962
raise AssertionError(
60-
f'versions of Outer Scout mod and addon are incompatible. Please, update the addon to support version {ACCEPTED_API_VERSION[0]}.{ACCEPTED_API_VERSION[1]}.x'
63+
f"versions of Outer Scout mod and addon are incompatible. Please, update the addon to support version {ACCEPTED_API_VERSION[0]}.{ACCEPTED_API_VERSION[1]}.x"
6164
if self.api_version is not None
62-
else "failed to connect to the Outer Scout API"
65+
else get_version_error
6366
)
6467

6568
def get_environment(self) -> Result[EnvironmentJson, str]:
@@ -151,12 +154,20 @@ def _get_response(
151154
request = Request(url=self.base_url + route, method=method, data=data, query_params=query)
152155

153156
response = request.send()
157+
if response is None:
158+
Result.do_error(
159+
f"couldn't get a response from the Outer Scout API. Make sure that it's available at {self.base_url}"
160+
)
154161

155-
error_prefix = "Outer Scout API error: "
156-
157-
if not response.is_success:
158-
generic_error = response.json(GenericError).map_error(lambda _: error_prefix + response.body).then()
159-
Result.do_error(error_prefix + generic_error["error"] if "error" in generic_error else response.body)
162+
if response.is_error:
163+
Result.do_error(
164+
response.json(ProblemJson)
165+
.map(
166+
lambda p: (f"[{p['type']}]" if "title" not in p else p["title"])
167+
+ (f": {p['detail']}" if "detail" in p else "")
168+
)
169+
.unwrap_or_else(lambda _: f"Outer Scout API error: {response.body}")
170+
)
160171

161172
return response
162173

api/http/request.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Request:
2323
query_params: dict[str, str] | None = None
2424
headers: dict[str, str] = field(default_factory=dict)
2525

26-
def send(self) -> Response:
26+
def send(self) -> Response | None:
2727
http_request = self._to_urllib_request()
2828

2929
try:
@@ -41,7 +41,8 @@ def send(self) -> Response:
4141
)
4242
except OSError as os_error:
4343
print(os_error)
44-
raise
44+
45+
return None
4546

4647
def _to_urllib_request(self) -> UrllibRequest:
4748
url = url_encode(self.url, safe="/:")

api/models/api.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypedDict
1+
from typing import TypedDict, NotRequired
22

33

44
class ApiVersionJson(TypedDict):
@@ -7,6 +7,7 @@ class ApiVersionJson(TypedDict):
77
major: int
88

99

10-
class GenericError(TypedDict):
11-
error: str
12-
10+
class ProblemJson(TypedDict):
11+
type: str
12+
title: NotRequired[str]
13+
detail: NotRequired[str]

0 commit comments

Comments
 (0)