silent failures को रोकने के लिए production में त्रुटियों को सहजता से संभालें। जब getResult() जैसा कोई कॉल विफल होता है, तो SDK एक TracioError throw करता है — एक Error उपवर्ग जिसमें machine-readable code फ़ील्ड होता है।
await tracio.getResult() को try/catch में लपेटें और throw किए गए मान को isTracioError() से संकुचित करें, फिर error.code पर शाखा बनाएं:
import { Tracio, isTracioError, isRetryableError } from "@tracio/sdk"
const tracio = Tracio.init({ publicKey: "5ca175fc..." })
try { const result = await tracio.getResult() console.log(result.visitorId, result.bot.detected)} catch (error) { if (isTracioError(error)) { if (isRetryableError(error)) { // Transient failure (network, timeout, blocked script, upstream) — // a fresh attempt may succeed. console.warn(`Retryable TRACIO error: ${error.code}`) } else { // Terminal failure (bad config, misuse, destroyed instance) — // fix the caller; retrying the same call will not help. console.error(`Terminal TRACIO error: ${error.code} — ${error.message}`) } } else { console.error("Unexpected error:", error) }}agent पृष्ठभूमि में asynchronously लोड होता है। यदि getResult() को कॉल करने से पहले ही लोडिंग विफल हो जाती है, तो विफलता onError callback के माध्यम से पहुँचाई जाती है। इसे init() के तुरंत बाद पंजीकृत करें:
const tracio = Tracio.init({ publicKey: "5ca175fc..." })
tracio.onError((error) => { // Same TracioError instance you would catch from getResult(). console.warn(`TRACIO failed to initialize: ${error.code}`)})
tracio.onReady((result) => { console.log("Visitor identified:", result.visitorId)})वही त्रुटि getResult() के माध्यम से भी अवलोकनीय रहती है — आप किसी भी channel का उपयोग कर सकते हैं।
प्रत्येक TracioError में TracioErrorCode प्रकार का एक code होता है। retryable getter (और isRetryableError() helper) यह बताते हैं कि कोई नया प्रयास सफल हो सकता है या नहीं।
| Code | अर्थ | Retryable |
|---|---|---|
invalid_config | Tracio.init() को पास किया गया config अमान्य है | नहीं |
multiple_keys | एक ही पृष्ठ पर एक से अधिक public key का उपयोग किया गया | नहीं |
non_browser | getResult() को browser के बाहर await किया गया (जैसे server पर) | नहीं |
load_failed | agent script लोड होने में विफल रहा | हाँ |
blocked | script को अवरुद्ध किया गया (ad blocker या CSP) | हाँ |
script_error | लोड किए गए script ने initialize करते समय त्रुटि throw की | हाँ |
network | API तक पहुँचने में Network/transport विफलता | हाँ |
timeout | कॉल ने कॉन्फ़िगर किए गए timeoutMs बजट को पार कर लिया | हाँ |
server | edge/server ने एक upstream विफलता लौटाई | हाँ |
destroyed | कॉल से पहले instance नष्ट कर दिया गया था (destroy() के माध्यम से) | नहीं |
Terminal codes (invalid_config, multiple_keys, non_browser, destroyed) एक configuration, usage, या lifecycle समस्या का संकेत देते हैं — caller को ठीक करें या Tracio.init() को फिर से कॉल करें। Retryable codes क्षणिक होते हैं और कोई नया प्रयास सफल हो सकता है।
retryable त्रुटियों के लिए, उसी अस्वीकृत promise को फिर से await करने के बजाय एक नए प्रयास के विरुद्ध backoff के साथ पुनः प्रयास करें:
import { Tracio, isRetryableError } from "@tracio/sdk"
async function identifyWithRetry(maxAttempts = 3) { let attempt = 0 while (true) { const tracio = Tracio.init({ publicKey: "5ca175fc..." }) try { return await tracio.getResult() } catch (error) { attempt++ // Tear down before retrying: init() returns the cached instance for the // same key, and getResult() caches its (rejected) promise — so a genuine // fresh attempt requires destroy() first. tracio.destroy() if (!isRetryableError(error) || attempt >= maxAttempts) throw error await new Promise((r) => setTimeout(r, 2 ** attempt * 250)) } }}