1
0
Эх сурвалжийг харах

Add external error handler (#650)

* Add external error handler

* Add external error handler
Ivan 1 жил өмнө
parent
commit
5da44f3618

+ 3 - 1
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);
             }
         }

+ 3 - 0
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),

+ 24 - 2
gramjs/client/telegramBaseClient.ts

@@ -196,6 +196,8 @@ export abstract class TelegramBaseClient {
     /** @hidden */
     public useWSS: boolean;
 
+    /** @hidden */
+    public _errorHandler?: (error: Error) => Promise<void>;
     /** @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<void>) {
+        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);
+                }
+            }
+        };
+    }
 }

+ 6 - 2
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);
             }
 

+ 3 - 1
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);
             }
         }

+ 3 - 1
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);
                 }
             }

+ 3 - 1
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);
             }
         }

+ 12 - 4
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);
             }
         }

+ 3 - 1
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);
             }
         }