Error Handling
Robust error handling is crucial for data integrity.
FailOnException
The CDPConfig.FailOnException boolean controls how the SDK reports errors.
Default: false
When false, the SDK behaves in a "fire and forget" manner for API failures.
- Validation Errors: Still returned immediately (e.g., missing User ID).
- Network/API Errors: Logged but the method returns
nil.
Use Case: You want analytics to be best-effort and strictly non-blocking for your main application logic.
Enabled: true (Recommended)
When true, all errors are returned to the caller.
Use Case: You want to know if data failed to sync so you can retry or log it to a dead-letter queue.
config := cdp.CDPConfig{
CDPAPIKey: "...",
FailOnException: true,
}
Common Errors
Validation Errors
Occur before any network request if arguments are invalid.
err := client.Identify(ctx, "", traits) // invalid ID
if err != nil {
// Handle validation error
}
API Errors
Occur when Conviso returns a non-200 status code. The error message will typically contain the status code and response body.
if err := client.Track(ctx, id, event, props); err != nil {
log.Println(err) // e.g., "API error 401: Unauthorized"
}
Context Cancellation
If the passed context is cancelled or times out, the SDK returns a context error.
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()
err := client.Identify(ctx, "user", traits)
// err will look like "context deadline exceeded"