Skip to main content
Module

x/grammy/mod.ts>Composer#errorBoundary

The Telegram Bot Framework.
Very Popular
Go to Latest
method Composer.prototype.errorBoundary
import { Composer } from "https://deno.land/x/grammy@v1.14.0/mod.ts";

This is an advanced function of grammY.

Installs an error boundary that catches errors that happen only inside the given middleware. This allows you to install custom error handlers that protect some parts of your bot. Errors will not be able to bubble out of this part of your middleware system, unless the supplied error handler rethrows them, in which case the next surrounding error boundary will catch the error.

Example usage:

function errHandler(err: BotError) {
  console.error('Error boundary caught error!', err)
}

const safe =
  // All passed middleware will be protected by the error boundary.
  bot.errorBoundary(errHandler, middleware0, middleware1, middleware2)

// Those will also be protected!
safe.on('message', middleware3)

// No error from `middleware4` will reach the `errHandler` from above,
// as errors are suppressed.

// do nothing on error (suppress error), and run outside middleware
const suppress = (_err: BotError, next: NextFunction) => { return next() }
safe.errorBoundary(suppress).on('edited_message', middleware4)

Check out the documentation on the website to learn more about error boundaries.

Parameters

errorHandler: (error: BotError<C>, next: NextFunction) => MaybePromise<unknown>

The error handler to use

...middleware: Array<Middleware<C>>

The middleware to protect