Errors & retries
Describes @stxapp/stx-typescript 0.12.1, the current release.
What each endpoint can return is in the API reference, and request limits are in Rate limits. The SDK turns every error response into a typed exception, so you can catch the case you care about:
import { STX, STXNotFoundException, STXRateLimitException } from "@stxapp/stx-typescript";
const client = new STX();
try { await client.order(orderId);} catch (err) { if (err instanceof STXNotFoundException) { console.log("no such order"); } else if (err instanceof STXRateLimitException) { console.log("slow down, retry after", err.retryAfter, "s"); } else { throw err; }}Exceptions
Section titled “Exceptions”| Exception | Thrown for |
|---|---|
STXValidationException |
400 |
STXAuthenticationException |
401 |
STXForbiddenException |
403 |
STXInsufficientScopeException |
403 on an OAuth token without the scope the call needs (requiredScopes) |
STXIPNotAllowedException |
403 ip_not_allowed: a write from an address the app’s IP allow-list lacks |
STXTermsRequiredException |
403 terms_required: the member has new STX terms to accept (termsUrl) |
STXBlockedException |
403 before the request reached the exchange (reason, reference); see Blocked requests |
STXRestrictedJurisdictionException |
blocked: a location the exchange does not serve. Extends STXBlockedException |
STXUnusualNetworkException |
blocked: a network the exchange does not accept traffic from. Extends STXBlockedException |
STXNotFoundException |
404 |
STXRejectedException |
422, including an order from outside the region; see Location checks |
STXFillModeNotAllowedException |
422: a fillMode the market does not take (combination markets), fillMode names it. Extends STXRejectedException |
STXPostOnlyWouldMatchException |
a post-only order would have matched on arrival (reason, and order when the exchange cancelled it). Extends STXRejectedException |
STXRateLimitException |
429 (retryAfter, in seconds; reference when blocked before the exchange) |
STXServerException |
5xx |
STXTransportException |
no response: connection, DNS, TLS or timeout |
STXConfigException |
a client misconfiguration, such as a missing key |
STXChannelException, STXTimeoutException |
a refused or unanswered WebSocket join |
STXNotAvailableException |
a feature this exchange does not have yet (feature, host); see What you can do |
All of them extend STXException. API errors extend STXAPIException and carry message (the API’s error text), statusCode, body, method and path.
Blocked requests
Section titled “Blocked requests”Some requests are refused in front of the exchange, before it sees them. Those throw STXBlockedException (403), with reason ("restricted_jurisdiction", "unusual_network", or "blocked" when no reason was given) and reference, the request id to quote to Support (from the x-amz-cf-id response header). They are never retried: the same request is blocked again. A 403 with no reason most often means the request had no User-Agent header; the SDK sends one, so check a custom fetch that replaces the headers.
Too many requests from one address is STXRateLimitException (429) with retryAfter and reference. The limits are in Rate limits.
import { STXBlockedException, STXRateLimitException } from "@stxapp/stx-typescript";
try { await client.me();} catch (err) { if (err instanceof STXBlockedException) { console.log(err.reason, err.reference, err.message); // unusual_network Fm2a5Xq0pZ8Lw3YbH7rT9kUeN1cVdJ6oSgA4iQ-BzMxPyWl5RtOk3A== We can't connect you from this network. Quote the reference when you contact STX support. } else if (err instanceof STXRateLimitException) { console.log("retry in", err.retryAfter, "s, reference", err.reference); // retry in 60 s, reference Fm2a5Xq0pZ8Lw3YbH7rT9kUeN1cVdJ6oSgA4iQ-BzMxPyWl5RtOk3A== } else { throw err; }}What is retried
Section titled “What is retried”| Failure | GET, DELETE |
POST |
|---|---|---|
| 429 | retried after Retry-After |
retried after Retry-After |
| 403 blocked before the exchange | not retried | not retried |
| 5xx | retried | not retried |
| No response | retried | not retried |
| Any other 4xx | not retried | not retried |
A failed POST may still have placed the order, so it is never resent; see Avoid duplicate orders. Each attempt is signed again. The default is 3 attempts with exponential backoff from 500 ms, capped at 30 s. When Retry-After asks for longer than the cap (maxBackoffMs), the call throws STXRateLimitException at once rather than retry early.
import { NO_RETRY, RetryPolicy, STX } from "@stxapp/stx-typescript";
const patient = new STX({ retry: new RetryPolicy({ maxAttempts: 5 }), timeoutMs: 10_000 });const noRetries = new STX({ retry: NO_RETRY });Logging requests
Section titled “Logging requests”onResponse is called after every attempt with the operation, status, duration and bodies:
const client = new STX({ onResponse: (e) => console.log(e.method, e.path, e.status, `${e.durationMs} ms`),});
await client.me();// GET /api/v1/me 200 84 ms
