Go SDK Documentation

Error Handling in the Go SDK

Robust error handling is a cornerstone of reliable software development. The Go SDK provides a consistent and idiomatic way to manage errors that arise from SDK operations.

The error Type

In Go, errors are typically represented by the built-in error interface. This interface has a single method:

type error interface {
    Error() string
}

Any type that implements this interface can be used as an error. The Error() method should return a descriptive string representation of the error.

Returning Errors

Most functions in the Go SDK that can fail will return an error as their last return value. It's conventional to check for errors immediately after a function call.


import "github.com/your-sdk/client"
import "fmt"

func processRequest(c *client.Client, id string) error {
    data, err := c.GetData(id)
    if err != nil {
        // Handle the error appropriately
        return fmt.Errorf("failed to get data for ID %s: %w", id, err)
    }
    // Process data...
    fmt.Println("Successfully retrieved data:", data)
    return nil // No error occurred
}
            

Common Error Patterns

SDK-Specific Errors

The SDK might define its own error types or sentinel errors for specific failure conditions. These are often exported for programmatic checking.


import "github.com/your-sdk/client"
import "fmt"

func performAction(c *client.Client) error {
    err := c.InitiateOperation()
    if err != nil {
        if err == client.ErrOperationTimeout {
            return fmt.Errorf("operation timed out: %w", err)
        }
        if apiErr, ok := err.(client.APIError); ok {
            return fmt.Errorf("API error occurred: Status %d, Message: %s", apiErr.StatusCode, apiErr.Message)
        }
        return fmt.Errorf("an unexpected error occurred: %w", err)
    }
    return nil
}
            

Error Wrapping (%w)

The fmt.Errorf function with the %w verb is crucial for error wrapping. It allows you to create new errors that contain the original error, preserving the error's context and allowing for programmatic unwrapping using errors.Is and errors.As.

Tip: Always use %w when wrapping an error to maintain the original error's identity for checking.

Checking for Specific Errors

Using errors.Is

This function checks if an error in the chain matches a specific target error. It's ideal for checking against sentinel errors (predefined error variables).


import "errors"
import "github.com/your-sdk/client"

if errors.Is(err, client.ErrRateLimited) {
    fmt.Println("Request was rate limited. Please try again later.")
    // Implement retry logic or notify the user
}
            

Using errors.As

This function checks if an error in the chain can be assigned to a target type. It's used to extract specific error details from custom error types.


import "errors"
import "github.com/your-sdk/client"

var apiErr client.APIError
if errors.As(err, &apiErr) {
    fmt.Printf("Received API Error: Code=%d, Message=%s\n", apiErr.StatusCode, apiErr.Message)
    // Handle based on API error code
}
            

Best Practices

Understanding and implementing Go's error handling patterns will lead to more robust and maintainable applications using the SDK.