Errors & Debugging
The SDK is designed to never break the host application. Most methods route failures through an onError callback rather than throwing. The exception is identify(), which throws so you can decide whether to retry or proceed without analytics.
Error types
TreliqError
Thrown for any non-2xx response from the API.
class TreliqError extends Error {
code: string; // e.g. "VALIDATION_FAILED", "PROJECT_NOT_FOUND", "HTTP_500"
message: string;
}
Example:
import { TreliqError } from '@treliq/sdk';
try {
await treliq.identify('user_123');
} catch (err) {
if (err instanceof TreliqError) {
console.error('Treliq rejected the request:', err.code, err.message);
}
}
RateLimitError
Thrown (or surfaced via onError) when the API responds with HTTP 429.
class RateLimitError extends Error {
retryAfter: number; // seconds, from the Retry-After header
}
The SDK automatically retries identify() up to 3 times with the Retry-After delay (capped at 10 seconds). For track(), the SDK does not retry — it logs the error and continues; events are not lost from the queue if you upgrade volume limits before the next flush.
The onError callback
Configure once in init():
init({
apiKey, projectId,
onError: (err) => {
if (err.name === 'RateLimitError') return; // ignore rate limits
Sentry.captureException(err);
},
});
onError is called for:
- Failed
track()flushes - Failed
getInAppContent()requests - Failed
reportAction()calls - Failures bubbled up from the in-app content stream
It is not called for:
identify()errors (those throw — handle them withtry/catch)- WebSocket disconnects (those reconnect silently)
- Polling failures (silently retried on the next interval)
Debug logging
The SDK ships with a built-in logger that prints every action — init, identify, track, batch flushes, WebSocket lifecycle, in-app render — to the browser console.
| Trigger | Behavior |
|---|---|
apiKey starts with tk_test_ | Debug on by default |
apiKey starts with tk_live_ | Debug off by default |
debug: true in init() | Forces debug on |
debug: false in init() | Forces debug off |
init({ apiKey: 'tk_live_xxx', projectId, debug: true });
// Console: [Treliq] SDK initialized { projectId: '…', debug: true }
// [Treliq] identify user_123 { email: '…' }
// [Treliq] track checkout_started {…} (batched)
// [Treliq] flush 3 events
// [Treliq] WebSocket connected
Leave debug off in production unless you’re actively investigating an issue — the volume of logs is high.
Diagnosing common issues
| Symptom | Likely cause | Fix |
|---|---|---|
Error: Treliq: call identify() before track() | track() ran before identify() resolved | Await the identify() promise |
TreliqError: PROJECT_NOT_FOUND | Wrong projectId or wrong environment for the key | Verify the project ID in dashboard; check key prefix matches environment |
TreliqError: HTTP_401 | Invalid or revoked API key | Generate a new key |
| WebSocket immediately closes | sseUrl unreachable or wrong scheme | Check the URL; the SDK auto-converts http→ws, https→wss |
| In-app content never appears | inApp.enabled: false, or workflow targeting issue | Enable debug logging and check for in-app content received lines |
| Rate-limit errors during traffic spike | Per-key event ceiling hit | Increase plan limits or batch more aggressively |
Related
- Authentication — key types and what they authorize
- Configuration —
onErroranddebugoptions