Error Handling
Handle errors with automatic retries and typed error classes
Error Classes
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();
}| Class | Use case |
|---|---|
CommetAPIError / CommetApiException / ApiException | HTTP errors (4xx, 5xx) |
CommetValidationError / CommetValidationException / ValidationException | Invalid input with field details |
CommetError / CommetException | Base class for all errors |
Go exposes *commet.CommetError and *commet.ValidationError as concrete types — check them with errors.As.
Automatic Retries
Failed requests retry with exponential backoff (1s → 2s → 4s, max 8s).
Retryable: 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
);Non-Blocking Usage
Don't let tracking errors break your app:
commet.usage.track({
customerId: 'user_123',
feature: 'api_calls',
}).catch(console.error)
// Continue without waitingtry:
commet.usage.track(customer_id='user_123', feature='api_calls')
except Exception as e:
logger.error(e)
# Continue without waitinggo func() {
_, err := client.Usage.Track(context.Background(), &commet.TrackUsageParams{
CustomerID: "user_123",
Feature: "api_calls",
})
if err != nil {
log.Printf("track failed: %v", err)
}
}()
// Continue without waitingCompletableFuture.runAsync(() -> {
try {
commet.usage().track(
TrackParams.builder("api_calls")
.customerId("user_123")
.build()
);
} catch (Exception e) {
log.error("track failed", e);
}
});
// Continue without waitingtry {
$commet->usage->track(
customerId: 'user_123',
feature: 'api_calls',
);
} catch (\Throwable $e) {
error_log($e->getMessage());
}
// Continue without waitingHow is this guide?