Skip to main content

Códigos de Error

Referencia completa de todos los códigos de error del SDK.

Errores del Backend

Errores de PIN

CódigoHTTPDescripciónSolución
PIN_NOT_FOUND404PIN no existe o fue limpiadoGenerar nuevo PIN
PIN_EXPIRED410PIN expiró después de 3 minutosGenerar nuevo PIN
PIN_ALREADY_VERIFIED409PIN ya fue utilizadoGenerar nuevo PIN
INVALID_PIN_FORMAT400Formato de PIN inválidoVerificar formato

Errores de Autenticación

CódigoHTTPDescripciónSolución
TOKEN_INVALID401Token JWT inválidoRe-autenticar
TOKEN_EXPIRED401Token JWT expiradoUsar refresh token
TOKEN_NOT_PROVIDED401No se proporcionó tokenIncluir header Authorization
REFRESH_TOKEN_INVALID401Refresh token inválidoRe-autenticar
REFRESH_TOKEN_EXPIRED401Refresh token expiradoRe-autenticar

Errores de Rate Limiting

CódigoHTTPDescripciónSolución
RATE_LIMIT_EXCEEDED429Demasiados requestsEsperar y reintentar

Errores de Webhook

CódigoHTTPDescripciónSolución
WEBHOOK_SIGNATURE_INVALID401Firma de webhook inválidaVerificar WEBHOOK_SECRET

Errores Generales

CódigoHTTPDescripciónSolución
VALIDATION_ERROR400Datos inválidosVerificar request body
INTERNAL_ERROR500Error interno del servidorContactar soporte
NETWORK_ERROR503Error de redReintentar

Errores del Cliente

Errores de Conexión

CódigoDescripciónSolución
SOCKET_ERRORError de conexión WebSocketVerificar conexión de red
PIN_GENERATION_FAILEDFallo al generar PINReintentar
CONFIG_ERRORError de configuraciónVerificar configuración
SESSION_RESTORE_FAILEDFallo al restaurar sesiónRe-autenticar
NETWORK_ERRORError de redVerificar conexión
AUTHENTICATION_FAILEDFallo de autenticaciónReintentar
VALIDATION_ERRORError de validaciónVerificar inputs
TIMEOUT_ERRORTimeout de conexiónReintentar

Estructura de Errores

Backend

{
  "success": false,
  "error": "Mensaje descriptivo",
  "code": "PIN_EXPIRED",
  "status": 410,
  "details": {
    "retryAfter": 60,
    "expiresAt": "2024-01-01T00:03:00.000Z"
  }
}

Cliente

class CamarauthError extends Error {
  code: string;           // 'PIN_EXPIRED'
  message: string;        // 'PIN has expired'
  statusCode?: number;    // 410
}

Manejo de Errores

Ejemplo: Manejo Completo

import { CamarauthError } from 'camarauth-sdk';

try {
  await backend.registerPin(pin);
} catch (error) {
  if (error instanceof CamarauthError) {
    switch (error.code) {
      case 'PIN_EXPIRED':
        showToast('Tu código ha expirado. Genera uno nuevo.');
        regeneratePin();
        break;
        
      case 'RATE_LIMIT_EXCEEDED':
        const retryAfter = error.details?.retryAfter || 60;
        showToast(`Demasiados intentos. Espera ${retryAfter} segundos.`);
        break;
        
      case 'NETWORK_ERROR':
        showToast('Sin conexión. Verifica tu red.');
        break;
        
      default:
        showToast('Ocurrió un error. Intenta de nuevo.');
    }
  }
}

Retry Automático

Algunos errores son recuperables y se pueden reintentar:
const retryableCodes = [
  'NETWORK_ERROR',
  'SOCKET_ERROR',
  'TIMEOUT_ERROR',
  'RATE_LIMIT_EXCEEDED'
];

async function withRetry(fn, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await fn();
    } catch (error) {
      if (!retryableCodes.includes(error.code) || i === maxRetries - 1) {
        throw error;
      }
      await sleep(1000 * Math.pow(2, i));
    }
  }
}