Skip to content

Error Handling

The login() method throws TelegramLoginError on failure. The error carries:

PropertyTypeDescription
codeTelegramLoginErrorCodeThe error code enum
messageString?Optional error message with details
statusCodeint?For server errors, the HTTP status code
Error CodeWhen It HappensRecommended Action
notConfiguredconfigure() was not called before login().Call configure() with valid credentials before attempting login.
noAuthorizationCodeCallback URL did not contain a code query parameter.Check that the redirect URI is correctly configured in BotFather.
serverErrorToken endpoint returned a non-200 status (statusCode).Check statusCode for details. May be a temporary Telegram server issue.
requestFailedGeneric SDK-level request failure.See message for details. Check network connectivity.
cancelledUser dismissed the auth sheet or cancelLogin() was called.No UI error needed; user intentionally cancelled.
networkErrorNetwork failure while contacting Telegram.Offer a retry. Check device connectivity.
platformErrorUnexpected native or channel error.See message for details. May require debugging.
try {
final result = await telegramLogin.login();
// Handle success
} on TelegramLoginError catch (e) {
switch (e.code) {
case TelegramLoginErrorCode.cancelled:
// User cancelled — no UI error needed.
break;
case TelegramLoginErrorCode.networkError:
// Offer a retry.
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text('Network Error'),
content: const Text('Please check your connection and try again.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
break;
case TelegramLoginErrorCode.notConfigured:
// Call configure() first.
debugPrint('Error: configure() was not called');
break;
case TelegramLoginErrorCode.serverError:
debugPrint('Server error: ${e.statusCode}');
break;
default:
// noAuthorizationCode, requestFailed, platformError
debugPrint('Login error: ${e.code.name}${e.message}');
break;
}
}

Occurs when you call login() before configure():

// ❌ Wrong
final result = await telegramLogin.login();
// ✅ Correct
await telegramLogin.configure(configuration);
final result = await telegramLogin.login();

Usually indicates a mismatch between the redirectUri configured in your app and what’s registered with BotFather:

  1. Verify your Bundle ID (iOS) or package name (Android) in BotFather
  2. Check that your redirect URI matches exactly
  3. Ensure Associated Domains / intent filters are correctly configured

Transient connectivity issues. Best practice:

  • Show a user-friendly message
  • Offer a retry button
  • Consider exponential backoff for automatic retries

Telegram’s token endpoint returned an error. The statusCode may provide additional context:

  • 400 - Bad request (check your configuration)
  • 401 - Unauthorized (invalid client ID)
  • 500 - Telegram server error (retry later)

During development, enable verbose logging to diagnose issues:

try {
final result = await telegramLogin.login();
} on TelegramLoginError catch (e, stackTrace) {
debugPrint('TelegramLoginError: ${e.code.name}');
debugPrint('Message: ${e.message}');
debugPrint('Status code: ${e.statusCode}');
debugPrint('Stack trace: $stackTrace');
}