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

View File

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

View File

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