Manejo de Errores
Cómo interpretar y manejar errores de API.
Estructura de Error
{
"error": {
"code": "INVALID_REQUEST",
"message": "Descripción legible del error",
"details": {}
}
}
Códigos de Error Comunes
400 Bad Request
{
"error": {
"code": "INVALID_REQUEST",
"message": "The request was invalid or malformed."
}
}
Causas:
- Parámetros faltantes
- Tipos de datos incorrectos
- JSON inválido
401 Unauthorized
{
"error": {
"code": "UNAUTHORIZED",
"message": "Invalid or missing authentication token."
}
}
Solución: Verifica tu token de API.
403 Forbidden
{
"error": {
"code": "FORBIDDEN",
"message": "You do not have permission to access this resource."
}
}
Causas:
- Permisos insuficientes
- Recurso en otra organización
404 Not Found
{
"error": {
"code": "NOT_FOUND",
"message": "The requested resource was not found."
}
}
Solución: Verifica que el ID del recurso sea correcto.
429 Too Many Requests
{
"error": {
"code": "RATE_LIMITED",
"message": "You have exceeded the rate limit."
}
}
Solución: Espera antes de reintentar. Limit: 1000/hora.
500 Internal Server Error
{
"error": {
"code": "INTERNAL_ERROR",
"message": "An unexpected error occurred."
}
}
Solución: Intenta de nuevo más tarde. Contacta soporte si persiste.
Manejo de Errores
Python
import requests
try:
response = requests.get(
'https://us.teamday.ai/api/v1/agents',
headers={'Authorization': f'Bearer {token}'}
)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print(f"Error: {e.response.json()['error']['message']}")
JavaScript
try {
const response = await fetch(
'https://us.teamday.ai/api/v1/agents',
{ headers: { 'Authorization': `Bearer ${token}` } }
);
if (!response.ok) {
const error = await response.json();
throw new Error(error.error.message);
}
} catch (error) {
console.error('API Error:', error.message);
}
Reintento Inteligente
Reintentos automáticos para errores transitorios:
import time
def make_request_with_retry(url, token, max_retries=3):
for attempt in range(max_retries):
try:
return requests.get(url, headers={'Authorization': f'Bearer {token}'})
except Exception as e:
if attempt < max_retries - 1:
time.sleep(2 ** attempt) # Exponential backoff
else:
raise
Próximos Pasos
- Lee Autenticación
- Ve Ejemplos