Manejo de errores
Maneja errores con reintentos automáticos y clases de error tipadas
Clases de error
import { CommetAPIError, CommetValidationError } from '@commet/node'
try {
await commet.customers.create({ email: 'invalid' })
} catch (error) {
if (error instanceof CommetValidationError) {
console.log(error.validationErrors)
// { email: ['Invalid email format'] }
}
if (error instanceof CommetAPIError) {
console.log(error.statusCode, error.message)
}
}from commet import CommetAPIError, CommetValidationError
try:
commet.customers.create(email='invalid')
except CommetValidationError as e:
print(e.validation_errors)
# { 'email': ['Invalid email format'] }
except CommetAPIError as e:
print(e.status_code, e)import (
"errors"
"github.com/commet/commet-go"
)
_, err := client.Customers.Create(ctx, &commet.CreateCustomerParams{Email: "invalid"})
if err != nil {
var validationErr *commet.ValidationError
if errors.As(err, &validationErr) {
fmt.Println(validationErr.ValidationErrors)
// map[email:[Invalid email format]]
}
var apiErr *commet.CommetError
if errors.As(err, &apiErr) {
fmt.Println(apiErr.StatusCode, apiErr.Message)
}
}import co.commet.CommetApiException;
import co.commet.CommetValidationException;
try {
commet.customers().create("invalid");
} catch (CommetValidationException e) {
System.out.println(e.getValidationErrors());
// { email: [Invalid email format] }
} catch (CommetApiException e) {
System.out.println(e.getStatusCode() + " " + e.getMessage());
}use Commet\Exceptions\ApiException;
use Commet\Exceptions\ValidationException;
try {
$commet->customers->create(email: 'invalid');
} catch (ValidationException $e) {
print_r($e->validationErrors);
// [ 'email' => ['Invalid email format'] ]
} catch (ApiException $e) {
echo $e->getStatusCode() . ' ' . $e->getMessage();
}| Clase | Caso de uso |
|---|---|
CommetAPIError / CommetApiException / ApiException | Errores HTTP (4xx, 5xx) |
CommetValidationError / CommetValidationException / ValidationException | Input inválido con detalles por campo |
CommetError / CommetException | Clase base de todos los errores |
Go expone *commet.CommetError y *commet.ValidationError como tipos concretos — verifícalos con errors.As.
Reintentos automáticos
Los requests fallidos se reintentan con backoff exponencial (1s → 2s → 4s, máx 8s).
Reintentables: 408, 429, 500, 502, 503, 504
const commet = new Commet({
apiKey: process.env.COMMET_API_KEY!,
retries: 3, // default
})commet = Commet(
api_key=os.environ['COMMET_API_KEY'],
retries=3, # default
)client, err := commet.New(
os.Getenv("COMMET_API_KEY"),
commet.WithRetries(3), // default
)Commet commet = Commet.builder()
.apiKey(System.getenv("COMMET_API_KEY"))
.retries(3) // default
.build();$commet = new Commet(
apiKey: getenv('COMMET_API_KEY'),
retries: 3, // default
);Uso no bloqueante
No dejes que los errores de tracking rompan tu app:
commet.usage.track({
customerId: 'user_123',
feature: 'api_calls',
}).catch(console.error)
// Continúa sin esperartry:
commet.usage.track(customer_id='user_123', feature='api_calls')
except Exception as e:
logger.error(e)
# Continúa sin esperargo func() {
_, err := client.Usage.Track(context.Background(), &commet.TrackUsageParams{
CustomerID: "user_123",
Feature: "api_calls",
})
if err != nil {
log.Printf("track failed: %v", err)
}
}()
// Continúa sin esperarCompletableFuture.runAsync(() -> {
try {
commet.usage().track(
TrackParams.builder("api_calls")
.customerId("user_123")
.build()
);
} catch (Exception e) {
log.error("track failed", e);
}
});
// Continúa sin esperartry {
$commet->usage->track(
customerId: 'user_123',
feature: 'api_calls',
);
} catch (\Throwable $e) {
error_log($e->getMessage());
}
// Continúa sin esperar¿Cómo está esta guía?