Skip to content

Commit

Permalink
refactor: improve error handling in bot (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
evermake authored Mar 31, 2024
1 parent 87b6ba6 commit 3519a05
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions backend/src/bot/handle-error.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import type { ErrorHandler } from 'grammy'
import { GrammyError, HttpError } from 'grammy'
import { BotError, GrammyError, HttpError } from 'grammy'
import type { Ctx } from './context'

/**
* @see https://grammy.dev/guide/errors
*/
export const handleError: ErrorHandler<Ctx> = (error) => {
export const handleError: ErrorHandler<Ctx> = (e) => {
const error = e.error
const ctx = e.ctx
let msg
if (error instanceof GrammyError) {
msg = 'Bot API request failed'
} else if (error instanceof HttpError) {
msg = 'network error'
} else {
} else if (error instanceof BotError) {
msg = 'error in middleware'
}
error.ctx.logger.error({ msg, error })
ctx.logger.error({
msg: msg,
update: ctx.update,
error: error,
})
}
2 changes: 1 addition & 1 deletion backend/src/bot/handlers/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default handler((composer) => {
]]),
})
} else {
throw err
throw err.error
}
})

Expand Down

0 comments on commit 3519a05

Please sign in to comment.