summaryrefslogtreecommitdiff
path: root/kvmd/apps
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2021-07-30 07:30:22 +0300
committerMaxim Devaev <[email protected]>2021-07-30 07:30:22 +0300
commit4a1f51bc3017de604d2512e00c25e14a139e771e (patch)
tree75c3f8c4dea7918d1a907253d83463e40924e3a4 /kvmd/apps
parent7d7f4965deb5a7f57b64c2132ab600b206d67595 (diff)
send error in streaming response
Diffstat (limited to 'kvmd/apps')
-rw-r--r--kvmd/apps/kvmd/api/msd.py6
-rw-r--r--kvmd/apps/kvmd/http.py8
2 files changed, 9 insertions, 5 deletions
diff --git a/kvmd/apps/kvmd/api/msd.py b/kvmd/apps/kvmd/api/msd.py
index a8d44e5e..c2ec746f 100644
--- a/kvmd/apps/kvmd/api/msd.py
+++ b/kvmd/apps/kvmd/api/msd.py
@@ -106,9 +106,9 @@ class MsdApi:
size = written = 0
response: Optional[StreamResponse] = None
- async def stream_write_info() -> None:
+ async def stream_write_info(err: Optional[Exception]=None) -> None:
assert response is not None
- await stream_json(response, self.__make_write_info(name, size, written))
+ await stream_json(response, self.__make_write_info(name, size, written), err)
try:
async with htclient.download(
@@ -141,7 +141,7 @@ class MsdApi:
except Exception as err:
if response is not None:
- await stream_write_info()
+ await stream_write_info(err)
elif isinstance(err, aiohttp.ClientError):
return make_json_exception(err, 400)
raise
diff --git a/kvmd/apps/kvmd/http.py b/kvmd/apps/kvmd/http.py
index 9ffed1d4..9649a995 100644
--- a/kvmd/apps/kvmd/http.py
+++ b/kvmd/apps/kvmd/http.py
@@ -178,8 +178,12 @@ async def start_streaming(request: Request, content_type: str) -> StreamResponse
return response
-async def stream_json(response: StreamResponse, result: Dict) -> None:
- await response.write(json.dumps(result).encode("utf-8") + b"\r\n")
+async def stream_json(response: StreamResponse, result: Dict, err: Optional[Exception]=None) -> None:
+ await response.write(json.dumps({
+ "result": result,
+ "error": ("" if err is None else type(err).__name__),
+ "error_msg": ("" if err is None else str(err)),
+ }).encode("utf-8") + b"\r\n")
# =====