From 5da44f36189d0110ea4151ff5695108e55745d56 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 12 Mar 2024 20:03:07 +0800 Subject: [PATCH] Add external error handler (#650) * Add external error handler * Add external error handler --- gramjs/client/TelegramClient.ts | 4 +++- gramjs/client/dialogs.ts | 3 +++ gramjs/client/telegramBaseClient.ts | 26 ++++++++++++++++++++++++-- gramjs/client/updates.ts | 8 ++++++-- gramjs/client/users.ts | 4 +++- gramjs/events/Album.ts | 4 +++- gramjs/events/NewMessage.ts | 4 +++- gramjs/network/MTProtoSender.ts | 16 ++++++++++++---- gramjs/tl/custom/message.ts | 4 +++- 9 files changed, 60 insertions(+), 13 deletions(-) diff --git a/gramjs/client/TelegramClient.ts b/gramjs/client/TelegramClient.ts index 9cfa68b9..6c7adfa1 100644 --- a/gramjs/client/TelegramClient.ts +++ b/gramjs/client/TelegramClient.ts @@ -1366,7 +1366,9 @@ export class TelegramClient extends TelegramBaseClient { const res = await this.getMe(); } catch (e) { this._log.error(`Error while trying to reconnect`); - if (this._log.canSend(LogLevel.ERROR)) { + if (this._errorHandler) { + await this._errorHandler(e as Error); + } if (this._log.canSend(LogLevel.ERROR)) { console.error(e); } } diff --git a/gramjs/client/dialogs.ts b/gramjs/client/dialogs.ts index b4d14fb9..3f8ccb7f 100644 --- a/gramjs/client/dialogs.ts +++ b/gramjs/client/dialogs.ts @@ -125,6 +125,9 @@ export class _DialogsIter extends RequestIter { if (this.client._log.canSend(LogLevel.ERROR)) { console.error(e); } + if (this.client._errorHandler) { + await this.client._errorHandler(e as Error); + } } messages.set( _dialogMessageKey(message.peerId!, message.id), diff --git a/gramjs/client/telegramBaseClient.ts b/gramjs/client/telegramBaseClient.ts index 081b2b95..4a251cec 100644 --- a/gramjs/client/telegramBaseClient.ts +++ b/gramjs/client/telegramBaseClient.ts @@ -196,6 +196,8 @@ export abstract class TelegramBaseClient { /** @hidden */ public useWSS: boolean; + /** @hidden */ + public _errorHandler?: (error: Error) => Promise; /** @hidden */ public _eventBuilders: [EventBuilder, CallableFunction][]; /** @hidden */ @@ -479,7 +481,9 @@ export abstract class TelegramBaseClient { sender.userDisconnected = false; return sender; } - if (this._log.canSend(LogLevel.ERROR)) { + if (this._errorHandler) { + await this._errorHandler(err as Error); + } else if (this._log.canSend(LogLevel.ERROR)) { console.error(err); } @@ -518,7 +522,9 @@ export abstract class TelegramBaseClient { } } } catch (err) { - if (this._log.canSend(LogLevel.ERROR)) { + if (this._errorHandler) { + await this._errorHandler(err as Error); + } if (this._log.canSend(LogLevel.ERROR)) { console.error(err); } return this._borrowExportedSender(dcId, true); @@ -583,4 +589,20 @@ export abstract class TelegramBaseClient { get logger() { return this._log; } + + /** + * Custom error handler for the client + */ + set onError(handler: (error: Error) => Promise) { + this._errorHandler = async (error: Error) => { + try { + await handler(error); + } catch (e: any) { + if (this._log.canSend(LogLevel.ERROR)) { + e.message = `Error ${e.message} thrown while handling top-level error: ${error.message}`; + console.error(e); + } + } + }; + } } diff --git a/gramjs/client/updates.ts b/gramjs/client/updates.ts index a241c189..e9b970e4 100644 --- a/gramjs/client/updates.ts +++ b/gramjs/client/updates.ts @@ -174,7 +174,9 @@ export async function _dispatchUpdate( if (e instanceof StopPropagation) { break; } - if (client._log.canSend(LogLevel.ERROR)) { + if (client._errorHandler) { + await client._errorHandler(e as Error); + } if (client._log.canSend(LogLevel.ERROR)) { console.error(e); } } @@ -241,7 +243,9 @@ export async function _updateLoop(client: TelegramClient) { lastPongAt = Date.now(); } catch (err) { // eslint-disable-next-line no-console - if (client._log.canSend(LogLevel.ERROR)) { + if (client._errorHandler) { + await client._errorHandler(err as Error); + } if (client._log.canSend(LogLevel.ERROR)) { console.error(err); } diff --git a/gramjs/client/users.ts b/gramjs/client/users.ts index fb883211..37dd1895 100644 --- a/gramjs/client/users.ts +++ b/gramjs/client/users.ts @@ -368,7 +368,9 @@ export async function getInputEntity( return utils.getInputPeer(channels.chats[0]); } catch (e) { - if (client._log.canSend(LogLevel.ERROR)) { + if (client._errorHandler) { + await client._errorHandler(e as Error); + } if (client._log.canSend(LogLevel.ERROR)) { console.error(e); } } diff --git a/gramjs/events/Album.ts b/gramjs/events/Album.ts index ebc51f1f..e171ed64 100644 --- a/gramjs/events/Album.ts +++ b/gramjs/events/Album.ts @@ -108,7 +108,9 @@ export class AlbumEvent extends EventCommon { "Got error while trying to finish init message with id " + this.messages[i].id ); - if (client._log.canSend(LogLevel.ERROR)) { + if (client._errorHandler) { + client._errorHandler(e as Error); + } if (client._log.canSend(LogLevel.ERROR)) { console.error(e); } } diff --git a/gramjs/events/NewMessage.ts b/gramjs/events/NewMessage.ts index 6d8f82fd..4f925e32 100644 --- a/gramjs/events/NewMessage.ts +++ b/gramjs/events/NewMessage.ts @@ -258,7 +258,9 @@ export class NewMessageEvent extends EventCommon { client._log.error( "Got error while trying to finish init message with id " + m.id ); - if (client._log.canSend(LogLevel.ERROR)) { + if (client._errorHandler) { + client._errorHandler(e as Error); + } if (client._log.canSend(LogLevel.ERROR)) { console.error(e); } } diff --git a/gramjs/network/MTProtoSender.ts b/gramjs/network/MTProtoSender.ts index e990bffc..ebd41333 100644 --- a/gramjs/network/MTProtoSender.ts +++ b/gramjs/network/MTProtoSender.ts @@ -277,7 +277,9 @@ export class MTProtoSender { this._log.error( `WebSocket connection failed attempt: ${attempt + 1}` ); - if (this._log.canSend(LogLevel.ERROR)) { + if (this._client._errorHandler) { + await this._client._errorHandler(err as Error); + } if (this._log.canSend(LogLevel.ERROR)) { console.error(err); } await sleep(this._delay); @@ -579,7 +581,9 @@ export class MTProtoSender { return; } else { this._log.error("Unhandled error while receiving data"); - if (this._log.canSend(LogLevel.ERROR)) { + if (this._client._errorHandler) { + await this._client._errorHandler(e as Error); + } if (this._log.canSend(LogLevel.ERROR)) { console.log(e); } this.reconnect(); @@ -601,7 +605,9 @@ export class MTProtoSender { } } else { this._log.error("Unhandled error while receiving data"); - if (this._log.canSend(LogLevel.ERROR)) { + if (this._client._errorHandler) { + await this._client._errorHandler(e as Error); + } if (this._log.canSend(LogLevel.ERROR)) { console.log(e); } } @@ -978,7 +984,9 @@ export class MTProtoSender { await this._disconnect(); } catch (err) { this._log.warn("Error happened while disconnecting"); - if (this._log.canSend(LogLevel.ERROR)) { + if (this._client._errorHandler) { + await this._client._errorHandler(err as Error); + } if (this._log.canSend(LogLevel.ERROR)) { console.error(err); } } diff --git a/gramjs/tl/custom/message.ts b/gramjs/tl/custom/message.ts index 6a866f2d..7b65c8a6 100644 --- a/gramjs/tl/custom/message.ts +++ b/gramjs/tl/custom/message.ts @@ -616,7 +616,9 @@ export class CustomMessage extends SenderGetter { "Got error while trying to finish init message with id " + this.id ); - if (this._client._log.canSend(LogLevel.ERROR)) { + if (this._client._errorHandler) { + await this._client._errorHandler(e as Error); + } if (this._client._log.canSend(LogLevel.ERROR)) { console.error(e); } }