Proper upload exceptions, handle in QML set avatar

This commit is contained in:
miruka 2019-10-28 08:06:22 -04:00
parent 5894481dc5
commit 6eb3a378bc
3 changed files with 29 additions and 27 deletions

View File

@ -58,7 +58,7 @@ class Backend:
try: try:
await client.login(password) await client.login(password)
except RuntimeError as err: except RuntimeError as err: # XXX raise
await client.close() await client.close()
return (False, err.args[0].message) return (False, err.args[0].message)
@ -140,10 +140,10 @@ class Backend:
nio.crypto.key_export.decrypt_and_read(file_path, passphrase) nio.crypto.key_export.decrypt_and_read(file_path, passphrase)
return True return True
except OSError as err: except OSError as err: # XXX raise
return (f"{file_path}: {err.strerror}", True) return (f"{file_path}: {err.strerror}", True)
except ValueError as err: except ValueError as err: # XXX raise
if str(err).startswith("HMAC check failed"): if str(err).startswith("HMAC check failed"):
return False return False

View File

@ -5,8 +5,8 @@ import json
import logging as log import logging as log
import platform import platform
from contextlib import suppress from contextlib import suppress
from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from enum import Enum
from functools import partial from functools import partial
from pathlib import Path from pathlib import Path
from types import ModuleType from types import ModuleType
@ -22,10 +22,19 @@ from .models.model_store import ModelStore
from .pyotherside_events import AlertRequested from .pyotherside_events import AlertRequested
class UploadError(Enum): @dataclass
forbidden = "M_FORBIDDEN" class UploadError(Exception):
too_large = "M_TOO_LARGE" http_code: Optional[int] = None
unknown = "UNKNOWN"
@dataclass
class UploadForbidden(UploadError):
http_code: Optional[int] = 403
@dataclass
class UploadTooLarge(UploadError):
http_code: Optional[int] = 413
class MatrixClient(nio.AsyncClient): class MatrixClient(nio.AsyncClient):
@ -295,23 +304,16 @@ class MatrixClient(nio.AsyncClient):
return resp.content_uri return resp.content_uri
if resp.status_code == 403: if resp.status_code == 403:
return UploadError.forbidden.value raise UploadForbidden()
if resp.status_code == 413: if resp.status_code == 413:
return UploadError.too_large.value raise UploadTooLarge()
return UploadError.unknown.value raise UploadError(resp.status_code)
async def set_avatar_from_file(self, path: Union[Path, str], async def set_avatar_from_file(self, path: Union[Path, str]) -> None:
) -> Union[bool, str]: await self.set_avatar(await self.upload_file(path))
resp = await self.upload_file(path)
if resp in (i.value for i in UploadError):
return resp
await self.set_avatar(resp)
return True
async def import_keys(self, infile: str, passphrase: str) -> None: async def import_keys(self, infile: str, passphrase: str) -> None:
@ -328,7 +330,7 @@ class MatrixClient(nio.AsyncClient):
try: try:
sessions = await loop.run_in_executor(None, import_keys) sessions = await loop.run_in_executor(None, import_keys)
except nio.EncryptionError as err: except nio.EncryptionError as err: # XXX raise
account.import_error = (infile, passphrase, str(err)) account.import_error = (infile, passphrase, str(err))
return return

View File

@ -28,12 +28,12 @@ HGridLayout {
saveButton.avatarChangeRunning = true saveButton.avatarChangeRunning = true
let path = Qt.resolvedUrl(avatar.imageUrl).replace(/^file:/, "") let path = Qt.resolvedUrl(avatar.imageUrl).replace(/^file:/, "")
py.callClientCoro( py.callClientCoro(userId, "set_avatar_from_file", [path], () => {
userId, "set_avatar_from_file", [path], response => {
saveButton.avatarChangeRunning = false saveButton.avatarChangeRunning = false
if (response != true) { print(response) } }, (errType, [httpCode]) => {
} console.error("Avatar upload failed:", httpCode, errType)
) saveButton.avatarChangeRunning = false
})
} }
} }