Skip to content

Commit 5419ec7

Browse files
committed
add report account logic for openai and grammy errors
1 parent 1d2804d commit 5419ec7

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export default {
4444
minimumBalance: 0,
4545
isEnabled: Boolean(parseInt(process.env.LLMS_ENABLED ?? '1')),
4646
pdfUrl: process.env.PDF_URL ?? '',
47-
processingTime: 300000
47+
processingTime: 300000,
48+
reportAcount: process.env.REPORT_ACCOUNT
4849
},
4950
openAi: {
5051
dalle: {

src/modules/errorhandler.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { GrammyError } from 'grammy'
77
import { sendMessage } from './llms/utils/helpers'
88
import { now } from '../utils/perf'
99
import OpenAI from 'openai'
10+
import config from '../config'
1011

1112
// const MAX_TRIES = 3 // Define the maximum number of retries
1213

@@ -31,12 +32,16 @@ class ErrorHandler {
3132
Sentry.setContext('llms', { retryCount, msg })
3233
Sentry.captureException(e)
3334
ctx.chatAction = null
35+
const reportAccount = config.llms.reportAcount
3436
if (retryCount === 0) {
3537
// Retry limit reached, log an error or take alternative action
3638
logger.error(`Retry limit reached for error: ${e}`)
3739
return
3840
}
3941
if (e instanceof GrammyError) {
42+
if (reportAccount && ctx.chat?.id) {
43+
await ctx.api.sendMessage(reportAccount, `On method "${e.method}" in chat ${ctx.chat.id} | ${e.error_code} - ${e.description}`)
44+
}
4045
if (e.error_code === 400 && e.description.includes('not enough rights')) {
4146
const errorMsg = 'Error: The bot does not have permission to send photos in chat'
4247
this.writeLog(ctx, errorMsg, logger)
@@ -70,6 +75,9 @@ class ErrorHandler {
7075
await sendMessage(ctx, 'Error handling your request').catch(async (e) => { await this.onError(ctx, e, retryCount - 1, logger) })
7176
}
7277
} else if (e instanceof OpenAI.APIError) {
78+
if (reportAccount && ctx.chat?.id) {
79+
await ctx.api.sendMessage(reportAccount, `${ctx.session.chatGpt.model} : Error reported in chat ${ctx.chat?.id} => (${e.code}) ${e.message}`)
80+
}
7381
// 429 RateLimitError
7482
// e.status = 400 || e.code = BadRequestError
7583
const errorMsg = `OPENAI Error ${e.status}(${e.code}) - ${e.message}`
@@ -92,6 +100,8 @@ class ErrorHandler {
92100
} else if (e instanceof AxiosError) {
93101
const errorMsg = `${e.message}`
94102
this.writeLog(ctx, errorMsg, logger)
103+
console.log(e.message, e.cause)
104+
console.log(e.stack)
95105
await sendMessage(ctx, 'Error handling your request').catch(async (e) => {
96106
await this.onError(ctx, e, retryCount - 1, logger)
97107
})

0 commit comments

Comments
 (0)