The Square Go SDK provides structured error handling through theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/square/square-go-sdk/llms.txt
Use this file to discover all available pages before exploring further.
core.APIError type, which preserves HTTP status codes and headers from API responses.
Understanding API Errors
When an API call returns a non-success status code, the SDK wraps the error in acore.APIError type that includes:
- StatusCode: The HTTP status code from the response
- Header: The HTTP headers from the response
- Error message: The underlying error details
Checking Error Status Codes
You can check the HTTP status code to handle specific error conditions:Using errors.As for Type Assertion
Thecore.APIError type is compatible with Go’s standard errors.As function:
Using
errors.As is the recommended approach for type assertions in Go, as it works correctly even when errors are wrapped.Wrapping Errors
You can wrap API errors with additional context while preserving the ability to unwrap them:errors.As or errors.Is:
Common HTTP Status Codes
400 Bad Request
400 Bad Request
The request was invalid or malformed. Check your request parameters and body.
401 Unauthorized
401 Unauthorized
404 Not Found
404 Not Found
The requested resource does not exist. Verify the resource ID is correct.
429 Too Many Requests
429 Too Many Requests
You’ve exceeded the rate limit. The SDK automatically retries these requests with exponential backoff.
500 Internal Server Error
500 Internal Server Error
Square’s servers encountered an error. The SDK automatically retries these requests.
Error Structure
Thecore.APIError type is defined as:
Best Practices
Always check errors
Never ignore error return values from API calls. Always handle them appropriately.
Use type assertions carefully
Use
errors.As instead of type assertions to handle wrapped errors correctly.