Errors

One exception type with a code. Some of these are a wait; some will never work no matter how long you wait.

What you catch
on TigerbaseError catch (error) {
  error.code;        // TigerbaseErrorCode — branch on this
  error.retryAfter;  // Duration?, on rateLimited only
  error.status;      // int?, the HTTP status when there was one — for logs
  error.message;     // a sentence for your logs, not your customers
}

NoteNever match on the message

The sentence is for your log and will be improved without warning. The code is the contract — and it is narrower than the HTTP status on purpose, because two 403s can mean completely different things to the person holding the phone.

Every code

TigerbaseErrorCodeWhat happenedHTTPRetry?
rateLimitedToo many messages in the last minute. retryAfter is a Duration measured by the server’s own window — 5 seconds if the server did not say.429Yes, after retryAfter.
tooManyConversationsThe plan's ceiling on simultaneous conversations. A slot frees when one ends.429Yes, shortly, with a backoff.
monthlyLimitThe account's allowance is spent until the term rolls over.429No — a countdown here would be a lie.
invalidKeyWrong or empty key, the agent is switched off, the subscription is not active — or, today, a refused origin (see below).401, 403No — a setting has to change.
forbiddenOriginThe request's origin is not allowed for the agent.403No — a setting has to change.
secretKeyA tb_live_ key was passed in. Thrown by the constructor, before any request.noneNo — fix the code.
networkThe request never got a response before the stream started — offline, DNS, a refused connection.noneYes, a few times with a backoff.
abortedDeclared for parity with the browser SDK. Nothing in this package throws it — cancelling a subscription is silent.noneNever seen.
unknownAnything else: a 400 or 5xx, or an error the server reported mid-answer. status carries the HTTP status when there was one.otherOnce, by hand. Log it.

WarningToday a refused origin arrives as invalidKey

The package tells forbiddenOrigin apart from invalidKey by looking for “origin”, “domain” or “allowed” in the 403’s message. The server’s refusal now reads “This assistant is not available right now.”, so it comes through as invalidKey with status 403. Treat both the same — neither ever succeeds on a retry.

Wait, or stop

Two codes clear on their own; the rest fail the same way until somebody changes something.

Minimal

dart
try {
  final answer = await chat.askOnce(question);
} on TigerbaseError catch (error) {
  showMessage('Something went wrong (${error.code.name}).');
}

Realistic

lib/chat_errors.dart — Harbor & Pine
String sentenceFor(Object error) {
  if (error is! TigerbaseError) {
    // A connection that dropped mid-answer — see below.
    return 'The connection dropped. Try again?';
  }
  return switch (error.code) {
    TigerbaseErrorCode.rateLimited =>
      'A lot of people are asking at once — try again in '
          '${error.retryAfter?.inSeconds ?? 5} seconds.',
    TigerbaseErrorCode.tooManyConversations => 'Busy at the moment. Try again shortly.',
    TigerbaseErrorCode.network => 'You seem to be offline.',
    TigerbaseErrorCode.monthlyLimit ||
    TigerbaseErrorCode.invalidKey ||
    TigerbaseErrorCode.forbiddenOrigin ||
    TigerbaseErrorCode.secretKey => 'The assistant is not available right now.',
    TigerbaseErrorCode.aborted || TigerbaseErrorCode.unknown => 'Something went wrong.',
  };
}

Errors inside a stream

A refusal is thrown before the first event. An error the server reports mid-answer is thrown from the stream as a TigerbaseError with code unknown. Either way one try around the await for — or one onError on the subscription — covers both.

dart
try {
  await for (final event in chat.ask(question)) {
    // …
  }
} catch (error) {
  // A TigerbaseError — or a connection that dropped mid-answer (below)
  show(sentenceFor(error));
}

CarefulA dropped connection mid-answer is not a TigerbaseError

The package maps failures before the stream starts to network. Once the answer is arriving, a connection that drops surfaces as whatever package:http throws — typically a ClientException — not as a TigerbaseError.

An on TigerbaseError clause alone lets it through. Add a general catch, or check is! TigerbaseError in onError. And do not retry it automatically: the turn already ran and was counted.

Four sentences, not one

The temptation is a single “Something went wrong, try again”. Resist it: two of these clear on their own and two of them never will, and one message for all four leaves somebody retrying a limit that is not going to move until next month.

rateLimited"A lot of people are asking at once — try again in 11 seconds." A number, because a number is something to wait for.
monthlyLimit, invalidKey, forbiddenOrigin"The assistant is not available right now." No retry button. The detail is for the account owner, and it reaches them another way.
tooManyConversations"Busy at the moment." A retry button is right here.
network"You seem to be offline." Different from our side being broken, and a person on a train can tell.

WarningNever render the message to a customer

It is written for whoever reads your logs. A server message detailed enough to be useful to you is, by definition, one a stranger using your app should not be reading.