Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Manejo de errores en Camarauth SDK
Error └── CamarauthError ├── PinExpiredError ├── RefreshTokenExpiredError ├── AuthenticationError ├── NetworkError └── ValidationError
class CamarauthError extends Error { message: string; code: ErrorCode; statusCode?: number; } type ErrorCode = | 'PIN_EXPIRED' | 'REFRESH_TOKEN_EXPIRED' | 'AUTHENTICATION_FAILED' | 'NETWORK_ERROR' | 'VALIDATION_ERROR';
import { CamarauthError } from '@camarauth/sdk'; try { // código } catch (error) { if (error instanceof CamarauthError) { console.log(error.code); // "PIN_EXPIRED" console.log(error.message); // "PIN has expired" console.log(error.statusCode); // 410 } }
class PinExpiredError extends CamarauthError { code = 'PIN_EXPIRED'; statusCode = 410; }
import { PinExpiredError } from '@camarauth/sdk'; try { await checkLogin(pin); } catch (error) { if (error instanceof PinExpiredError) { console.log('El PIN ha expirado'); // Generar nuevo PIN } }
class RefreshTokenExpiredError extends CamarauthError { code = 'REFRESH_TOKEN_EXPIRED'; statusCode = 401; }
import { RefreshTokenExpiredError } from '@camarauth/sdk'; try { await refreshToken(refreshToken); } catch (error) { if (error instanceof RefreshTokenExpiredError) { console.log('Sesión expirada, inicia sesión de nuevo'); // Redirigir a login } }
class AuthenticationError extends CamarauthError { code = 'AUTHENTICATION_FAILED'; statusCode = 401; }
import { AuthenticationError } from '@camarauth/sdk'; try { await login(credentials); } catch (error) { if (error instanceof AuthenticationError) { console.log('Credenciales inválidas'); } }
class NetworkError extends CamarauthError { code = 'NETWORK_ERROR'; }
import { NetworkError } from '@camarauth/sdk'; try { await apiCall(); } catch (error) { if (error instanceof NetworkError) { console.log('Error de conexión'); // Reintentar o mostrar mensaje al usuario } }
class ValidationError extends CamarauthError { code = 'VALIDATION_ERROR'; statusCode = 400; }
import { ValidationError } from '@camarauth/sdk'; try { await registerPin(pin); } catch (error) { if (error instanceof ValidationError) { console.log('Datos inválidos'); } }
import { usePinAuth, CamarauthError } from '@camarauth/sdk/react'; function LoginComponent() { const auth = usePinAuth({ apiUrl: 'http://localhost:3001', whatsappNumber: '+1234567890', onError: (error) => { if (error instanceof CamarauthError) { switch (error.code) { case 'PIN_EXPIRED': alert('El código ha expirado'); break; case 'NETWORK_ERROR': alert('Error de conexión'); break; default: alert(error.message); } } } }); return <button onClick={auth.generate}>Generar</button>; }
import { CamarauthError } from '@camarauth/sdk'; app.use((error, req, res, next) => { if (error instanceof CamarauthError) { return res.status(error.statusCode || 500).json({ error: error.code, message: error.message }); } next(error); });
PIN_EXPIRED
REFRESH_TOKEN_EXPIRED
AUTHENTICATION_FAILED
NETWORK_ERROR
VALIDATION_ERROR
import { CamarauthError } from '@camarauth/sdk'; class CustomError extends CamarauthError { constructor(message: string) { super(message, 'CUSTOM_ERROR', 418); } } throw new CustomError('Algo salió mal');