Errors
One exception type with a code. Some of these are a wait; some will never work no matter how long you wait.
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
Every code
| TigerbaseErrorCode | What happened | HTTP | Retry? |
|---|---|---|---|
rateLimited | Too 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. | 429 | Yes, after retryAfter. |
tooManyConversations | The plan's ceiling on simultaneous conversations. A slot frees when one ends. | 429 | Yes, shortly, with a backoff. |
monthlyLimit | The account's allowance is spent until the term rolls over. | 429 | No — a countdown here would be a lie. |
invalidKey | Wrong or empty key, the agent is switched off, the subscription is not active — or, today, a refused origin (see below). | 401, 403 | No — a setting has to change. |
forbiddenOrigin | The request's origin is not allowed for the agent. | 403 | No — a setting has to change. |
secretKey | A tb_live_ key was passed in. Thrown by the constructor, before any request. | none | No — fix the code. |
network | The request never got a response before the stream started — offline, DNS, a refused connection. | none | Yes, a few times with a backoff. |
aborted | Declared for parity with the browser SDK. Nothing in this package throws it — cancelling a subscription is silent. | none | Never seen. |
unknown | Anything else: a 400 or 5xx, or an error the server reported mid-answer. status carries the HTTP status when there was one. | other | Once, by hand. Log it. |
WarningToday a refused origin arrives as invalidKey
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
Minimal
try {
final answer = await chat.askOnce(question);
} on TigerbaseError catch (error) {
showMessage('Something went wrong (${error.code.name}).');
}Realistic
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.
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