# Errors & retries

> The typed errors the SDK throws, and what it retries.

Source: https://docs.stxapp.io/sdks/typescript/errors-and-retries/

What each endpoint can return is in the [API reference](/api/rest/), and request limits are in [Rate limits](/concepts/rate-limits/). The SDK turns every error response into a typed exception, so you can catch the case you care about:

```ts
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

| Exception | Thrown for |
|---|---|
| `STXValidationException` | 400 |
| `STXAuthenticationException` | 401 |
| `STXForbiddenException` | 403 |
| `STXNotFoundException` | 404 |
| `STXRejectedException` | 422 |
| `STXGeoLocationException` | 422 from geo-fencing (`reason`) |
| `STXRateLimitException` | 429 (`retryAfter`, in seconds) |
| `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 |

All of them extend `STXException`. API errors extend `STXAPIException` and carry `message` (the API's error text), `statusCode`, `body`, `method` and `path`.

## What is retried

| Failure | `GET`, `DELETE` | `POST` |
|---|---|---|
| 429 | retried after `Retry-After` | retried after `Retry-After` |
| 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](/sdks/typescript/trading#avoid-duplicate-orders). Each attempt is signed again. The default is 3 attempts with exponential backoff from 500 ms, capped at 30 s.

```ts
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

`onResponse` is called after every attempt with the operation, status, duration and bodies:

```ts
const client = new STX({
  onResponse: (e) => console.log(e.method, e.path, e.status, `${e.durationMs} ms`),
});
```
