Skip to main content

Overview

Gogram provides several mechanisms to handle errors that occur during update processing or API requests.

Handler Errors

When a handler returns an error, Gogram processes it based on the type of error.

Logging Errors

By default, if a handler returns an error (and it is not telegram.EndGroup), the error is logged using the client’s logger.
client.OnMessage("", func(m *telegram.NewMessage) error {
    if err := doSomething(); err != nil {
        return err // This will be logged as an error
    }
    return nil
})

Stopping Propagation

To stop the execution of subsequent handlers in the same group, return telegram.ErrEndGroup. This is useful for “middleware-like” handlers that filter out updates.
client.OnMessage("", func(m *telegram.NewMessage) error {
    if isSpam(m) {
        return telegram.ErrEndGroup // Stop processing this update in this group
    }
    return nil
})

Client Error Handlers

You can configure global error handlers when creating the Client.

Flood Wait Handler

Telegram imposes rate limits. When you hit a limit, Telegram returns a FLOOD_WAIT_X error. You can define a FloodHandler to decide whether to wait and retry.
client, err := telegram.NewClient(telegram.ClientConfig{
    // ...
    FloodHandler: func(err error) bool {
        // Return true to wait and retry, false to abort
        log.Println("Flood wait:", err)
        return true 
    },
})

Global RPC Error Handler

For other RPC errors (e.g., internal server errors), you can use ErrorHandler.
client, err := telegram.NewClient(telegram.ClientConfig{
    // ...
    ErrorHandler: func(err error) bool {
        // Return true to retry the request
        return false
    },
})

Panic Recovery

Gogram automatically recovers from panics within handlers to prevent the entire application from crashing. Panics are logged with a stack trace.
client.OnMessage("", func(m *telegram.NewMessage) error {
    panic("something went wrong") // Application will NOT crash
})