Skip to content

Error Handling

mtgo uses generated error types from the tgerr package for structured error handling.

Basic Pattern

go
import "github.com/mtgo-labs/mtgo/tgerr"

result, err := rpc.SomeMethod(ctx, req)
if err != nil {
    var rpcErr *tgerr.Error
    if errors.As(err, &rpcErr) {
        switch {
        case tgerr.Is(err, tgerr.ErrFloodWait):
            // Rate limited — wait duration available via rpcErr
        case tgerr.Is(err, tgerr.ErrSessionPasswordNeeded):
            // 2FA required
        case tgerr.Is(err, tgerr.ErrPhoneNumberInvalid):
            // Invalid phone number
        }
    }
    return err
}

tgerr.Error Fields

FieldTypeDescription
CodeintNumeric error code (e.g. 420 for FLOOD_WAIT)
TypestringError type string (e.g. "FLOOD_WAIT")
MessagestringHuman-readable error message

Common Error Types

ErrorCodeDescription
FLOOD_WAIT420Rate limited. Wait N seconds. Auto-handled by floodwait middleware
SESSION_PASSWORD_NEEDED4012FA password required
AUTH_KEY_UNREGISTERED401Session not authorized
PEER_ID_INVALID400Invalid peer ID or access hash
USER_IS_BOT400Bots can't perform this action
CHAT_WRITE_FORBIDDEN403No write permission in chat
MESSAGE_DELETE_FORBIDDEN403Can't delete this message
USER_NOT_PARTICIPANT403User is not in the chat
BOT_METHOD_INVALID400Bots can't use this method

DC Migration

DC migration errors (code 303) are handled automatically by the client. When Telegram returns a USER_MIGRATE_X or FILE_MIGRATE_X error, the client reconnects to the correct DC and retries the request transparently.

With Middleware

The floodwait middleware automatically handles FLOOD_WAIT errors by sleeping and retrying. The ratelimit middleware prevents hitting rate limits in the first place.

Released under the Apache-2.0 License.