Comprendere le risposte di errore ti aiuta a risolvere i problemi rapidamente. Ecco i più comuni errori che potrai incontrare.
400 Bad Request
{
"error": "media_id is required"
}
Cause:
Parametro obbligatorio mancante
Formato del parametro non valido
JSON malformato
Soluzione: Verifica che il corpo della tua richiesta corrisponda alla specifica dell’API su api.cathopic.com/docs.
401 Unauthorized
{
"message": "Unauthorized"
}
or
{
"error": "Unauthorized token"
}
Cause:
Chiave API mancante o non valida
Chiave API disattivata
Nome dell’header errato
Soluzione: Verifica che l’header x-api-key sia corretto e che la chiave sia attiva in Impostazioni → API.
404 Not Found
{
"error": "No file found for media_id 1234"
}
or
{
"error": "Media item not found"
}
Cause:
La risorsa non esiste
La risorsa è stata eliminata
media_id errato
Soluzione: Verifica che il media_id esista cercando prima con /resources.
429 Too Many Requests
{
"error": "Limit exceed."
}
Cause:
Troppe richieste in un breve periodo
Limite di richieste superato
Soluzione: Implementa un backoff esponenziale. Attendi e riprova aumentando i ritardi (1s, 2s, 4s, ecc.).
500 Internal Server Error
{
"error": "Internal server error"
}
Cause:
Problema temporaneo del server
Condizione imprevista
Soluzione: Attendi un momento e riprova. Se il problema persiste, contatta support.
Esempio di gestione degli errori
async function searchWithErrorHandling(query) {
try {
const response = await fetch('https://api.cathopic.com/resources', {
method: 'POST',
headers: {
'x-api-key': apiKey,
'Content-Type': 'application/json'
},
body: JSON.stringify({ query, page: 1, page_size: 20 })
});
if (!response.ok) {
const error = await response.json();
switch (response.status) {
case 400:
console.error('Bad request:', error);
break;
case 401:
console.error('Unauthorized. Check your API key.');
break;
case 404:
console.error('Resource not found:', error);
break;
case 429:
console.error('Rate limited. Implementing backoff...');
await sleep(5000);
return searchWithErrorHandling(query); // Retry
case 500:
console.error('Server error. Retrying in 10s...');
await sleep(10000);
return searchWithErrorHandling(query);
default:
console.error('Unknown error:', error);
}
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Request failed:', error);
throw error;
}
}
Per errori persistenti, consulta la documentazione completa dell’API oppure contatta il supporto.