Error handling
An HTTP error returns *addresszen.APIError. A failed connection returns the net/http error, wrapped with the method and path. Match them with errors.AsType and errors.Is.
APIError
*APIError carries the HTTP status, the API error code and the body.
| Field | Contents |
|---|---|
StatusCode | HTTP status, such as 401 |
Code | API error code, such as 4010. 0 when the body carries none |
Message | API error message, or the HTTP status text |
Body | The response body as received, as []byte |
package main
import (
"context"
"errors"
"fmt"
"log"
addresszen "go.addresszen.com/sdk"
)
func main() {
client := addresszen.NewClient("ak_test")
res, err := client.AddressVerify(context.Background(), addresszen.AddressVerifyBody{
Query: "1010 Cauthen Ln, Alamogordo, NM 88310",
}, nil)
apiErr, isAPIErr := errors.AsType[*addresszen.APIError](err)
switch {
case isAPIErr && apiErr.StatusCode == 401:
fmt.Println("Check the key, and that address verification is enabled on it")
case isAPIErr:
fmt.Println(apiErr.StatusCode, apiErr.Code, apiErr.Message)
case err != nil:
log.Fatal(err)
default:
if m := res.Result.AsUsaVerifyMatch(); m != nil {
fmt.Println(m.ZipCode)
}
}
}The message and body can echo the address you sent, so treat them as personal data when you log them. The error codes guide lists each code and how to fix it.
Other errors
| Error | When |
|---|---|
ErrMissingManagementKey | A key, licensee or configuration operation on a client with no Management Key. Returned before any request is sent. Match it with errors.Is |
*ResponseError | A success response whose body is not JSON. It holds StatusCode, Body and the decoding error, which errors.Unwrap returns |
| Anything else | The HTTP client could not complete the request: DNS, connection, TLS or timeout failure. The net/http error is wrapped, so errors.AsType[*url.Error] finds it |
Timeouts and cancellation
A context deadline, a cancelled context and the HTTP client's Timeout all return an error that errors.Is matches to context.DeadlineExceeded or context.Canceled.
package main
import (
"context"
"errors"
"fmt"
"time"
addresszen "go.addresszen.com/sdk"
)
func main() {
client := addresszen.NewClient("ak_test")
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_, err := client.FindAddress(ctx, &addresszen.FindAddressParams{Query: "1010 Cauthen Ln"})
if errors.Is(err, context.DeadlineExceeded) {
fmt.Println("timed out")
}
}Retries
The SDK never retries. A timed-out request may still have been processed and charged, so retries are left to you. Retry an *APIError with status 429 or 5xx, and a network error, with a bounded number of attempts.