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 with try/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.

TriggerBehavior
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

SymptomLikely causeFix
Error: Treliq: call identify() before track()track() ran before identify() resolvedAwait the identify() promise
TreliqError: PROJECT_NOT_FOUNDWrong projectId or wrong environment for the keyVerify the project ID in dashboard; check key prefix matches environment
TreliqError: HTTP_401Invalid or revoked API keyGenerate a new key
WebSocket immediately closessseUrl unreachable or wrong schemeCheck the URL; the SDK auto-converts http→ws, https→wss
In-app content never appearsinApp.enabled: false, or workflow targeting issueEnable debug logging and check for in-app content received lines
Rate-limit errors during traffic spikePer-key event ceiling hitIncrease plan limits or batch more aggressively