Skip to content

Commit

Permalink
Add external error handler (#650)
Browse files Browse the repository at this point in the history
* Add external error handler

* Add external error handler
  • Loading branch information
IvanMMM authored Mar 12, 2024
1 parent 38fb5ac commit 5da44f3
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 13 deletions.
4 changes: 3 additions & 1 deletion gramjs/client/TelegramClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
3 changes: 3 additions & 0 deletions gramjs/client/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
26 changes: 24 additions & 2 deletions gramjs/client/telegramBaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
};
}
}
8 changes: 6 additions & 2 deletions gramjs/client/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion gramjs/client/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 3 additions & 1 deletion gramjs/events/Album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 3 additions & 1 deletion gramjs/events/NewMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
16 changes: 12 additions & 4 deletions gramjs/network/MTProtoSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 3 additions & 1 deletion gramjs/tl/custom/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 5da44f3

Please sign in to comment.