Error Handling

Handle Salesforce API errors gracefully with typed error checking.

Error Types

Error Type Description Helper Function
APIError General API errors -
AuthError Authentication failures IsAuthError()
NotFoundError Record not found (404) IsNotFoundError()
RateLimitError API limit exceeded IsRateLimitError()
ValidationError Field validation failed IsValidationError()

Basic Error Handling

Go
record, err := client.SObjects().Get(ctx, "Account", accountID)
if err != nil {
    if types.IsNotFoundError(err) {
        // Handle 404 - record doesn't exist
        log.Info("Account not found")
        return nil
    }
    if types.IsAuthError(err) {
        // Handle auth error - refresh token
        client.RefreshToken(ctx)
        return retry()
    }
    if types.IsRateLimitError(err) {
        // Handle rate limit - back off
        time.Sleep(60 * time.Second)
        return retry()
    }
    // Unknown error
    return err
}

Error Details

Go
_, err := client.SObjects().Create(ctx, "Account", data)
if err != nil {
    var apiErr *types.APIError
    if errors.As(err, &apiErr) {
        fmt.Printf("Status: %d\n", apiErr.StatusCode)
        fmt.Printf("Error Code: %s\n", apiErr.ErrorCode)
        fmt.Printf("Message: %s\n", apiErr.Message)
        
        for _, field := range apiErr.Fields {
            fmt.Printf("Field: %s\n", field)
        }
    }
}

Retryable Errors

Go
// Check if error is retryable
if types.IsRetryableError(err) {
    // 503, 429, network errors - can retry
    return retryWithBackoff(operation)
}

// 400, 404, 403 - don't retry
return err

Validation Errors

Go
var valErr *types.ValidationError
if errors.As(err, &valErr) {
    for _, issue := range valErr.Fields {
        fmt.Printf("Invalid field: %s - %s\n", issue.Field, issue.Message)
    }
}

Built-in Retry

The SDK includes automatic retry with exponential backoff for transient errors:

Go
client, _ := salesforce.NewClient(
    salesforce.WithOAuthRefresh(clientID, secret, token),
    salesforce.WithMaxRetries(5),  // Default: 3
)