-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve error handling in bot (#9)
- Loading branch information
Showing
2 changed files
with
11 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ export default handler((composer) => { | |
]]), | ||
}) | ||
} else { | ||
throw err | ||
throw err.error | ||
} | ||
}) | ||
|
||
|