Handle JoinRoom errors, error clases overhaul

This commit is contained in:
miruka 2019-11-09 10:20:16 -04:00
parent dbcca17192
commit e95a23ef90
2 changed files with 47 additions and 7 deletions

View File

@ -35,8 +35,27 @@ CryptDict = Dict[str, Any]
@dataclass @dataclass
class RequestFailed(Exception): class MatrixError(Exception):
m_code: Optional[str] = None m_code: str = "M_UNKNOWN"
@classmethod
def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError":
for subcls in cls.__subclasses__():
if subcls.m_code == response.status_code:
return subcls()
return cls(response.status_code)
@dataclass
class MatrixNotFound(MatrixError):
m_code: str = "M_NOT_FOUND"
@dataclass
class MatrixForbidden(MatrixError):
m_code: str = "M_FORBIDDEN"
@dataclass @dataclass
class UploadError(Exception): class UploadError(Exception):
@ -448,7 +467,7 @@ class MatrixClient(nio.AsyncClient):
) )
if isinstance(response, nio.RoomCreateError): if isinstance(response, nio.RoomCreateError):
raise RequestFailed(response.status_code) raise MatrixError.from_nio(response)
return response.room_id return response.room_id
@ -457,16 +476,19 @@ class MatrixClient(nio.AsyncClient):
if re.match(r"^https?://", string): if re.match(r"^https?://", string):
for part in urlparse(string).fragment.split("/"): for part in urlparse(string).fragment.split("/"):
if re.match(r"[#!].+:.+", part): if re.match(r"^[#!].+:.+", part):
string = part string = part
break break
else: else:
raise ValueError(f"No alias or room id found in url {string}") raise ValueError(f"No alias or room id found in url {string}")
if not re.match(r"^[#!].+:.+", string):
raise ValueError("Not an alias or room id")
response = await super().join(string) response = await super().join(string)
if isinstance(response, nio.JoinError): if isinstance(response, nio.JoinError):
raise RequestFailed(response.status_code) raise MatrixError.from_nio(response)
return response.room_id return response.room_id

View File

@ -21,13 +21,31 @@ HBox {
let args = [roomField.text] let args = [roomField.text]
py.callClientCoro(userId, "room_join", args, roomId => { py.callClientCoro(userId, "room_join", args, roomId => {
button.loading = false button.loading = false
errorMessage.text = ""
pageLoader.showRoom(userId, roomId) pageLoader.showRoom(userId, roomId)
}, (type, args) => {
button.loading = false
let txt = qsTr("Unknown error - %1, %2").arg(type).arg(args)
if (type === "ValueError")
txt = qsTr("Unrecognized alias, room ID or URL")
if (type === "MatrixNotFound")
txt = qsTr("Room not found")
if (type === "MatrixForbidden")
txt = qsTr("You do not have permission to join this room")
errorMessage.text = txt
}) })
}, },
cancel: button => { cancel: button => {
roomField.text = "" roomField.text = ""
errorMessage.text = ""
pageLoader.showPrevious() pageLoader.showPrevious()
} }
}) })