# Errors

> Every failure returns an HTTP status and a stable error code to check.

Source: https://rivo.markets/docs/errors

Failure body:

```json
{
  "success": false,
  "error": "cursor: not a cursor from this listing",
  "code": "VALIDATION"
}
```

The `error` message is written for people and may change. The `code` never changes, so your error handling should check the code. A `VALIDATION` message names the field that failed, such as `signal.priceMax`.

## Codes

| Code | Status | Means | Retry |
| --- | --- | --- | --- |
| `UNAUTHORIZED` | 401 | The request has no `Authorization` header, or the key is wrong or revoked. | No |
| `SUBSCRIPTION_INACTIVE` | 403 | The key is valid, but its account has no active subscription. The key works again as soon as the subscription is renewed. | No |
| `SCOPE_REQUIRED` | 403 | The route changes data, and the key does not have the `write` scope. Create a key that has it. | No |
| `VALIDATION` | 400 | A parameter or the body is invalid, or the request includes an unknown parameter. The message names the field. | No |
| `NOT_FOUND` | 404 | The market, trader, bot or order does not exist on this account. The API gives the same answer for something you do not own. | No |
| `IDEMPOTENCY_IN_PROGRESS` | 409 | A request with the same `Idempotency-Key` is still running. Wait, then send it again with the same key. | After a wait |
| `QUOTA_EXCEEDED` | 429 | Rivo could not check a daily allowance for a moment (wait the `retry-after` seconds), or you sent more than 120 requests this minute, read more than 5,000 trades older than an hour today, opened more than 100 different traders today, or listed traders more than 10 times today. The `retry-after` header says how many seconds to wait. | After a wait |
| `UPSTREAM_FAILED` | 502 | A venue did not answer, for example while Rivo read its live order book. Rivo recorded nothing. | Yes |
| `INTERNAL` | 500 | Something failed on Rivo's side. Retry with backoff, and contact us if it keeps happening. | Yes |
| `GONE` | 410 | The route was switched off. Every `/v1` route answers this from the v1 sunset on. Move to v2. | No |

## Handling failures

```typescript
const res = await fetch(url, { headers });
if (res.ok) return (await res.json()).data;

const { code, error } = await res.json();
switch (code) {
  case "QUOTA_EXCEEDED":
    await sleep(Number(res.headers.get("retry-after") ?? 60) * 1000);
    return retry();
  case "UPSTREAM_FAILED":
  case "INTERNAL":
    return retryWithBackoff();
  default:
    // UNAUTHORIZED, SCOPE_REQUIRED, VALIDATION, NOT_FOUND: fix the request, do not resend it.
    throw new Error(`${code}: ${error}`);
}
```

## Retrying a paper order

Send an `Idempotency-Key` header with `POST /v2/paper-orders`, and reuse the same key every time you retry that order. A retry with the same key returns the first result instead of placing the trade twice. If you send the same key with a different body, the API refuses it with `VALIDATION`. If the API refuses an order, you can use its key again for another try.
