Add listener for inbound client errors

This commit is contained in:
Andrew Ferrazzutti 2022-04-10 02:18:53 -04:00
parent 861e3ff30d
commit 2aace35962
3 changed files with 21 additions and 1 deletions

View File

@ -22,7 +22,7 @@ with any other potential backend.
from __future__ import annotations from __future__ import annotations
from typing import TYPE_CHECKING, cast, Type, Optional, Union from typing import TYPE_CHECKING, cast, Awaitable, Type, Optional, Union
import asyncio import asyncio
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
import logging import logging
@ -423,6 +423,9 @@ class Client:
self._stop_listen() self._stop_listen()
await self.user.on_disconnect(res) await self.user.on_disconnect(res)
def _on_error(self, data: dict[str, JSON]) -> Awaitable[None]:
return self.user.on_error(data)
def _start_listen(self) -> None: def _start_listen(self) -> None:
self._add_event_handler("chat", self._on_chat) self._add_event_handler("chat", self._on_chat)
@ -430,6 +433,7 @@ class Client:
# TODO many more listeners # TODO many more listeners
self._add_event_handler("disconnected", self._on_listen_disconnect) self._add_event_handler("disconnected", self._on_listen_disconnect)
self._add_event_handler("switch_server", self._on_switch_server) self._add_event_handler("switch_server", self._on_switch_server)
self._add_event_handler("error", self._on_error)
def _stop_listen(self) -> None: def _stop_listen(self) -> None:
for method in self._handler_methods: for method in self._handler_methods:

View File

@ -22,6 +22,7 @@ import time
from mautrix.bridge import BaseUser, async_getter_lock from mautrix.bridge import BaseUser, async_getter_lock
from mautrix.types import ( from mautrix.types import (
EventID, EventID,
JSON,
MessageType, MessageType,
RoomID, RoomID,
TextMessageEventContent, TextMessageEventContent,
@ -633,6 +634,14 @@ class User(DBUser, BaseUser):
await self.logout() await self.logout()
await self.send_bridge_notice(f"Disconnected from KakaoTalk: {reason_str} {reason_suffix}") await self.send_bridge_notice(f"Disconnected from KakaoTalk: {reason_str} {reason_suffix}")
def on_error(self, error: JSON) -> Awaitable[None]:
return self.send_bridge_notice(
f"Got error event from KakaoTalk:\n\n> {error}",
# TODO Which error code to use?
#error_code="kt-connection-error",
error_message=str(error),
)
async def on_client_disconnect(self) -> None: async def on_client_disconnect(self) -> None:
self.is_connected = False self.is_connected = False
self._track_metric(METRIC_CONNECTED, False) self._track_metric(METRIC_CONNECTED, False)

View File

@ -135,6 +135,13 @@ class UserClient {
is_sequential: true, is_sequential: true,
}) })
}) })
this.#talkClient.on("error", (err) => {
this.log(`Client error: ${err}`)
return this.write("error", {
error: err,
})
})
} }
/** /**