Skip to content

Errors and Reliability

Igor Sazonov edited this page Aug 9, 2026 · 1 revision

The SDK combines standard Go error wrapping with Bitget-specific detail. Callers should use errors.Is for known sentinel conditions and errors.As to retrieve a *bitget.BitgetError containing the exchange's code, message, and raw response body. Network, HTTP-client, JSON, and context errors remain wrapped so standard Go inspection continues to work. 1 2

Error-handling pattern

result, err := client.Account.GetAssets(ctx)
if err != nil {
    switch {
    case errors.Is(err, bitget.ErrUnauthorized):
        return fmt.Errorf("check API key, secret, passphrase, and permissions: %w", err)

    case errors.Is(err, bitget.ErrRateLimited):
        return fmt.Errorf("back off before retrying: %w", err)

    case errors.Is(err, context.DeadlineExceeded):
        return fmt.Errorf("request deadline exceeded: %w", err)

    default:
        var apiErr *bitget.BitgetError
        if errors.As(err, &apiErr) {
            return fmt.Errorf("Bitget API error %s: %s", apiErr.Code, apiErr.Message)
        }
        return fmt.Errorf("unexpected Bitget request failure: %w", err)
    }
}
_ = result

Add the standard imports used by the example:

import (
    "context"
    "errors"
    "fmt"

    bitget "github.com/tigusigalpa/bitget-go"
)

Sentinel errors

Sentinel Meaning Common response-code mappings in the SDK
ErrUnauthorized API credentials are invalid. 40001, 40006, 40009, 40037
ErrInvalidSignature Request signature is invalid. 40002, 40003
ErrInvalidTimestamp Timestamp is invalid or expired. 40004, 40005
ErrPermissionDenied The API key lacks permission. 40012, 40014, 40017
ErrRateLimited Exchange rate limit was exceeded. HTTP 429; 429, 40429, 30007
ErrInvalidParameter A submitted request parameter is invalid. 4001840023, 22001
ErrInsufficientFunds Available balance is insufficient. 43012, 45006
ErrOrderNotFound The referenced order was not found. 43025, 43001
ErrInternalServer Bitget returned an internal-server condition. 50000, 50001, 50002

The mappings are an SDK convenience layer. A code not recognized by MapErrorCode still returns a *BitgetError, so applications should preserve the errors.As branch rather than relying only on sentinel matching. 1

BitgetError details

*bitget.BitgetError has three exported fields:

Field Type Purpose
Code string Exact Bitget response code.
Message string Exact Bitget response message.
Raw []byte Raw response envelope for diagnostics, subject to your log-retention policy.

Avoid treating the human-readable message as a stable programmatic interface. Use sentinels for well-known cases and the raw Code only where your application deliberately supports an exchange-specific workflow. 1

Contexts and timeouts

Every SDK network call starts with context.Context, and the REST transport creates requests with http.NewRequestWithContext. Pass a deadline when a delayed response should no longer be useful. 2

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

orders, err := client.Trade.GetOpenOrders(ctx, trade.GetOpenOrdersOptions{
    Category: models.CategorySpot,
    Symbol:   "BTCUSDT",
})

A caller timeout means the caller did not receive a timely result; it does not by itself establish whether an exchange-side mutation such as PlaceOrder occurred. Reconcile uncertain mutations by querying open/history orders and, when appropriate, using private stream updates. 3 4

Rate limits and retry design

The REST transport maps an HTTP 429 response directly to ErrRateLimited. Bitget states that endpoints have individual limits, REST and WebSocket share the same quota, and the common domain's overall limit is 6000 requests per IP per minute; verify current endpoint limits in Bitget documentation before deployment. 2 6

A reliable retry policy should be selective:

Condition Suggested behavior
ErrRateLimited Back off with jitter and reduce polling/subscription churn.
Temporary network failure Retry only idempotent reads automatically; apply a bounded policy.
Context canceled/deadline exceeded Respect the caller cancellation; do not retry unconditionally.
Uncertain write outcome Reconcile by client order ID and exchange queries before any repeat action.
ErrInvalidParameter, ErrPermissionDenied, ErrUnauthorized Correct configuration or input; retrying unchanged data is not useful.

Logging safely

WithLogger and WithWSLogger can surface request and connection diagnostics. The REST logger is documented not to log ACCESS-KEY, ACCESS-SIGN, or ACCESS-PASSPHRASE; still, ensure application-level error logging does not expose secret environment values or unredacted request bodies. 2 5

References

Clone this wiki locally