# Errors

> Sentinel errors and the error types every call can return.

Source: https://docs.stxapp.io/sdks/go/reference/errors/

## Constants and variables

```go
var (
	// ErrConfig: the client is misconfigured (unknown environment, missing key,
	// bad PEM, unreadable credentials file).
	ErrConfig = errors.New("stx: configuration error")

	// ErrBadRequest: 400, a parameter or body field was refused.
	ErrBadRequest = errors.New("stx: bad request")
	// ErrUnauthorized: 401, the signature, key id or timestamp was not
	// accepted. A machine clock more than 30 seconds off also lands here.
	ErrUnauthorized = errors.New("stx: unauthorized")
	// ErrInvalidToken: 401 on an OAuth bearer request whose access token is
	// missing, expired or revoked. Also matches ErrUnauthorized.
	ErrInvalidToken = errors.New("stx: invalid token")
	// ErrForbidden: 403, the key is read-only or the account may not do this.
	ErrForbidden = errors.New("stx: forbidden")
	// ErrInsufficientScope: 403 on an OAuth bearer request whose grant lacks a
	// scope; see APIError.RequiredScopes. Also matches ErrForbidden.
	ErrInsufficientScope = errors.New("stx: insufficient scope")
	// ErrTermsRequired: 403, the member has not accepted the current terms;
	// see APIError.TermsURL. Also matches ErrForbidden.
	ErrTermsRequired = errors.New("stx: terms not accepted")
	// ErrIPNotAllowed: 403, the write came from an address the app's IP
	// allow-list lacks. Also matches ErrForbidden.
	ErrIPNotAllowed = errors.New("stx: ip address not allowed")
	// ErrNotFound: 404, the resource does not exist or belongs to another
	// account.
	ErrNotFound = errors.New("stx: not found")
	// ErrRejected: 422, the exchange refused the request, for example
	// insufficient funds or a closed market.
	ErrRejected = errors.New("stx: rejected")
	// ErrRateLimited: 429, too many requests; see APIError.RetryAfter.
	ErrRateLimited = errors.New("stx: rate limited")
	// ErrServer: 5xx, the server failed.
	ErrServer = errors.New("stx: server error")

	// ErrTransport: the request got no answer (connection, DNS, TLS or
	// timeout). The error also wraps the underlying network error.
	ErrTransport = errors.New("stx: transport error")

	// ErrBlocked: 403 from the edge firewall in front of the API, before the
	// request reached the exchange; see APIError.Code and
	// APIError.Reference. Never retried. Also matches ErrForbidden.
	ErrBlocked = errors.New("stx: blocked")
	// ErrRestrictedJurisdiction: blocked because the request came from a
	// location the exchange does not serve. Also matches ErrBlocked.
	ErrRestrictedJurisdiction = errors.New("stx: restricted jurisdiction")
	// ErrUnusualNetwork: blocked because the request came through a network
	// the exchange does not accept traffic from, such as an anonymizing
	// proxy. Also matches ErrBlocked.
	ErrUnusualNetwork = errors.New("stx: unusual network")

	// ErrFillModeNotAllowed: 422, the market does not take the order's fill
	// mode. Combination markets take neither post_only nor fill_or_kill.
	// Also matches ErrRejected.
	ErrFillModeNotAllowed = errors.New("stx: fill mode not allowed on this market")
	// ErrPostOnlyWouldMatch: a post-only order would have matched on arrival,
	// so the exchange did not rest it; see *PostOnlyWouldMatchError.
	ErrPostOnlyWouldMatch = errors.New("stx: post-only order would match")

	// ErrNotAvailable: this exchange does not have the feature yet, for
	// example combos where they are not switched on. Nothing is wrong with the
	// request or the credentials; see *NotAvailableError for the feature.
	ErrNotAvailable = errors.New("stx: not available")
)
```

Sentinel errors. Match them with [errors.Is](https://pkg.go.dev/errors#Is); an \*[APIError](/sdks/go/reference/errors/#apierror) matches the sentinel for its status, and the more specific ones listed below.

```go
var (
	// ErrStreamClosed: the stream was closed, or gave up reconnecting.
	ErrStreamClosed = errors.New("stx: stream closed")
	// ErrAlreadySubscribed: the stream already holds a subscription to this
	// topic. Close it first, or read from the one you have.
	ErrAlreadySubscribed = errors.New("stx: already subscribed")
	// ErrNotJoined: a channel command was sent while the topic is not joined,
	// for example during a reconnect.
	ErrNotJoined = errors.New("stx: channel not joined")
	// ErrChannelRefused: the exchange refused a join or a channel command;
	// see *ChannelError for its reason.
	ErrChannelRefused = errors.New("stx: channel refused")
	// ErrTimeout: no reply to a join or a channel command in time. The error
	// also matches context.DeadlineExceeded.
	ErrTimeout = errors.New("stx: no reply in time")
)
```

Sentinel errors for streams.

```go
var ErrComboPriceMoved = errors.New("stx: combo price moved")
```

ErrComboPriceMoved: an accept found nothing live at least as good as the amount; the request stays open. See ComboRefusedError.Best for the live quote.

## APIError

```go
type APIError struct {
	// StatusCode is the HTTP status, for example 422.
	StatusCode int
	// Code is the machine-readable error code when the API sends one, such as
	// "terms_required" or "not_found"; otherwise empty.
	Code string
	// Message is the API's human-readable message, kept verbatim.
	Message string
	// RequestID is the X-Request-Id response header, when present. Quote it
	// when you contact support.
	RequestID string
	// Method and Path are the request that failed; Path includes the query.
	Method string
	Path   string
	// Body is the raw response body.
	Body []byte
	// RetryAfter is the server's Retry-After on a 429, or zero.
	RetryAfter time.Duration
	// RequiredScopes names the OAuth scopes that would satisfy a 403
	// insufficient-scope answer.
	RequiredScopes []string
	// TermsURL is the page where the member accepts the terms, on a
	// terms-required answer.
	TermsURL string
	// Reference identifies a block by the edge firewall in front of the API
	// (a 403 matching ErrBlocked, or a 429 with Code "rate_limited"): the
	// request's CloudFront id, from the x-amz-cf-id response header. Quote it
	// when you contact support.
	Reference string
	// contains filtered or unexported fields
}
```

APIError is an error status answered by the API.

**Example**

Matching API errors.

```go
err := error(&stx.APIError{StatusCode: 422, Message: "Insufficient assets.", Method: "POST", Path: "/api/v1/orders"})
var apiErr *stx.APIError
if errors.As(err, &apiErr) {
	fmt.Println(apiErr.StatusCode, apiErr.Message)
}
fmt.Println(errors.Is(err, stx.ErrRejected))
// Output:
// 422 Insufficient assets.
// true
```

```text
422 Insufficient assets.
true
```

### APIError.Error

```go
func (e *APIError) Error() string
```

Error returns "stx: METHOD PATH: STATUS: message".

### APIError.Is

```go
func (e *APIError) Is(target error) bool
```

Is matches the sentinel errors for this status and code.

## ChannelError

```go
type ChannelError struct {
	// Topic is the wire topic, for example "orders:7fb55544-3a7e-4f6b-bd66-fc89bd3a1087".
	Topic string
	// Event is what was sent: "phx_join", "select_market_ids", ...
	Event string
	// Reason is the reply's "reason", or the server event ("phx_close",
	// "phx_error") that ended a join before it was answered.
	Reason string
	// Reply is the reply's raw "response" object.
	Reply json.RawMessage
	// contains filtered or unexported fields
}
```

ChannelError is the exchange refusing a join or a channel command, with the reason it gave, such as "market\_ids\_required" or "unauthorized".

### ChannelError.Error

```go
func (e *ChannelError) Error() string
```

Error returns "stx: EVENT on TOPIC refused: REASON".

### ChannelError.Is

```go
func (e *ChannelError) Is(target error) bool
```

Is matches [ErrChannelRefused](/sdks/go/reference/errors/#constants-and-variables) for a refusal, and [ErrUnauthorized](/sdks/go/reference/errors/#constants-and-variables) and [ErrInsufficientScope](/sdks/go/reference/errors/#constants-and-variables) for those reasons.

## ComboRefusedError

```go
type ComboRefusedError struct {
	// Command is what was sent: "request_quote", "accept", "phx_join", ...
	Command string
	// Topic is the wire topic.
	Topic string
	// Code is the refusal reason, such as "not_found" or "price_moved". A
	// "player_refused:<detail>" reason has Code "player_refused" and the rest
	// in Detail.
	Code   string
	Detail string
	// Reason is the reason exactly as sent.
	Reason string
	// Reply is the raw reply.
	Reply json.RawMessage
	// Best is the live best quote on "price_moved", or nil.
	Best *ComboBestQuote
}
```

ComboRefusedError is a combo command or join the exchange refused.

### ComboRefusedError.Error

```go
func (e *ComboRefusedError) Error() string
```

Error returns "stx: COMMAND on TOPIC refused: REASON".

### ComboRefusedError.Is

```go
func (e *ComboRefusedError) Is(target error) bool
```

Is matches [ErrComboPriceMoved](/sdks/go/reference/errors/#constants-and-variables), [ErrNotFound](/sdks/go/reference/errors/#constants-and-variables) for "not\_found", [ErrUnauthorized](/sdks/go/reference/errors/#constants-and-variables) for "unauthorized" and [ErrInsufficientScope](/sdks/go/reference/errors/#constants-and-variables) for "insufficient\_scope".

## ForbiddenError

```go
type ForbiddenError struct {
	// Failure reason: `terms_required` or `terms_not_delegable` for a terms
	// refusal, otherwise a human-readable message.
	Error string `json:"error,omitempty"`
	// Present on a terms refusal: what the app should do, in plain words.
	ErrorDescription string `json:"error_description,omitempty"`
	// Present on a terms refusal: the STX page where the member accepts the terms,
	// e.g. `<STX host>/connect/terms?client_id=<client_id>`.
	TermsURL string `json:"terms_url,omitempty"`
}
```

ForbiddenError: The 403 failure envelope. A refusal on the member's STX terms adds \`error\_description\` and \`terms\_url\` to \`error\`.

## HandshakeError

```go
type HandshakeError struct {
	// StatusCode is the HTTP status, for example 403.
	StatusCode int
	// Reason is the exchange's x-stx-error header, such as
	// "5300 - OAuth token auth failed", when it sent one.
	Reason string
}
```

HandshakeError is a WebSocket handshake the exchange refused with an HTTP status, for example 403 for a bad signature or a missing User-Agent.

### HandshakeError.Error

```go
func (e *HandshakeError) Error() string
```

Error returns "stx: websocket handshake refused: HTTP STATUS (REASON)".

### HandshakeError.Is

```go
func (e *HandshakeError) Is(target error) bool
```

Is matches [ErrUnauthorized](/sdks/go/reference/errors/#constants-and-variables) on 401 and [ErrForbidden](/sdks/go/reference/errors/#constants-and-variables) on 403.

## NotAvailableError

```go
type NotAvailableError struct {
	// Feature is a stable id: "combos", "markets.create", "market_types",
	// "event_players", "leaderboard", "oauth", "oauth.member", "oauth.app",
	// "connect", "terms.stx_page", "loyalty", "orders.post_only",
	// "orders.fill_or_kill", or "scope:<name>".
	Feature string
	// Host is the exchange that lacks it, for example "demo.stxapp.ca".
	Host string
	// Err is the answer the decision came from, when there was one.
	Err error
}
```

NotAvailableError reports a feature this exchange does not have yet. It matches [ErrNotAvailable](/sdks/go/reference/errors/#constants-and-variables).

### NotAvailableError.Error

```go
func (e *NotAvailableError) Error() string
```

Error returns a sentence such as "stx: Combos are not available on demo.stxapp.ca yet."

### NotAvailableError.Is

```go
func (e *NotAvailableError) Is(target error) bool
```

Is matches [ErrNotAvailable](/sdks/go/reference/errors/#constants-and-variables).

### NotAvailableError.Unwrap

```go
func (e *NotAvailableError) Unwrap() error
```

Unwrap returns the underlying answer, if any.

## PostOnlyWouldMatchError

```go
type PostOnlyWouldMatchError struct {
	// Reason is "auto_matching" when it would have matched another member's
	// order: the exchange cancelled it and Order is the cancelled order. It is
	// "match_with_self" when the first order it would have matched is your
	// own: the exchange rejected it (a 422) and Order is nil.
	Reason string
	// Order is the cancelled order, for "auto_matching".
	Order *Order
	// Err is the 422, for "match_with_self".
	Err error
}
```

PostOnlyWouldMatchError reports a [PostOnly](/sdks/go/reference/types/#constants-and-variables) order that would have matched on arrival, so the exchange did not rest it. Nothing traded and the book is unchanged; re-price and place again. It matches [ErrPostOnlyWouldMatch](/sdks/go/reference/errors/#constants-and-variables) and [ErrRejected](/sdks/go/reference/errors/#constants-and-variables).

### PostOnlyWouldMatchError.Error

```go
func (e *PostOnlyWouldMatchError) Error() string
```

Error describes the refusal.

### PostOnlyWouldMatchError.Is

```go
func (e *PostOnlyWouldMatchError) Is(target error) bool
```

Is matches [ErrPostOnlyWouldMatch](/sdks/go/reference/errors/#constants-and-variables) and [ErrRejected](/sdks/go/reference/errors/#constants-and-variables).

### PostOnlyWouldMatchError.Unwrap

```go
func (e *PostOnlyWouldMatchError) Unwrap() error
```

Unwrap returns the 422, if any.

## TransportError

```go
type TransportError struct {
	Method string
	Path   string
	Err    error
}
```

TransportError is a request that got no answer. It matches [ErrTransport](/sdks/go/reference/errors/#constants-and-variables) and unwraps to the network error.

### TransportError.Error

```go
func (e *TransportError) Error() string
```

Error describes the failed request.

### TransportError.Is

```go
func (e *TransportError) Is(target error) bool
```

Is matches [ErrTransport](/sdks/go/reference/errors/#constants-and-variables).

### TransportError.Unwrap

```go
func (e *TransportError) Unwrap() error
```

Unwrap returns the network error.
