एरर हैंडलिंग
मानक एरर कोड के साथ एरर को सही तरीके से हैंडल करें।
त्रुटि प्रतिक्रिया प्रारूप
सभी API त्रुटियाँ एक सुसंगत JSON प्रारूप का पालन करती हैं।
{
"error": {
"code": "error_code_here",
"message": "A human-readable description of the error."
}
}त्रुटि कोड
| स्थिति | कोड | विवरण |
|---|---|---|
401 | missing_api_key | अनुरोध में X-API-Key हेडर नहीं दिया गया। |
401 | invalid_api_key | दी गई API कुंजी अमान्य, समाप्त या रद्द है। |
402 | quota_exceeded | मासिक अनुरोध कोटा पार हो गया है। |
422 | invalid_image | image फ़ील्ड गायब है, अमान्य base64 है, या 5MB से अधिक है। |
422 | invalid_request | अनुरोध बॉडी मान्य JSON नहीं है। |
502 | inference_error | ML अनुमान सेवा अस्थायी रूप से अनुपलब्ध है। |
त्रुटि प्रतिक्रिया उदाहरण
API कुंजी गायब (401)
{
"error": {
"code": "missing_api_key",
"message": "No API key provided. Include your key in the X-API-Key header."
}
}अमान्य छवि (422)
{
"error": {
"code": "invalid_image",
"message": "Image exceeds the maximum size of 5 MB."
}
}अनुमान त्रुटि (502)
{
"error": {
"code": "inference_error",
"message": "ML inference service is temporarily unavailable. Please retry."
}
}सर्वोत्तम प्रथाएँ
- प्रतिक्रिया बॉडी पार्स करने से पहले हमेशा HTTP स्टेटस कोड जाँचें।
- 502 त्रुटियों के लिए एक्सपोनेंशियल बैकऑफ़ लागू करें।
- प्रतिक्रिया हेडर के माध्यम से कोटा उपयोग की निगरानी करें।
- 401 या 422 त्रुटियों को पुनः प्रयास न करें।
- प्रोग्रामेटिक त्रुटि हैंडलिंग के लिए error.code फ़ील्ड का उपयोग करें।
पुनः प्रयास उदाहरण
एक्सपोनेंशियल बैकऑफ़ के साथ JavaScript उदाहरण:
async function analyzePalm(imageBase64, apiKey, retries = 3) {
for (let attempt = 0; attempt < retries; attempt++) {
const res = await fetch(
"https://api.trace-line.site/v1/palm/analyze",
{
method: "POST",
headers: {
"X-API-Key": apiKey,
"Content-Type": "application/json",
},
body: JSON.stringify({ image: imageBase64 }),
}
);
if (res.ok) return res.json();
const error = await res.json();
// Only retry on transient errors
if (res.status !== 502) throw error;
// Exponential backoff: 1s, 2s, 4s
await new Promise((r) =>
setTimeout(r, 1000 * Math.pow(2, attempt))
);
}
throw new Error("Max retries exceeded");
}