> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arc.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Onramp error handling

> Catch and classify Onramp errors using KitError fields, see common error codes, and understand how the session route handler maps errors to HTTP status codes.

Every error the App Kit SDK throws for Onramp is a `KitError`, exported from
`@circle-fin/app-kit`. Its structured fields let you branch on the failure type
instead of parsing message strings. For a full integration walkthrough, see the
[embed widget quickstart](/app-kit/quickstarts/onramp-embed-widget).

## `KitError` fields

| Field            | Description                                                                                      |
| ---------------- | ------------------------------------------------------------------------------------------------ |
| `type`           | Classifies the failure. One of `INPUT`, `NETWORK`, `SERVICE`, `RATE_LIMIT`, `RPC`, or `UNKNOWN`. |
| `recoverability` | Indicates whether to retry. One of `RETRYABLE`, `RESUMABLE`, or `FATAL`.                         |
| `name`           | Stable identifier for exact matching, logging, and telemetry.                                    |
| `code`           | Numeric sub-classification in a `name`. Stable identifier for telemetry.                         |
| `message`        | Human-readable diagnostic. Not suitable for end-user display without review.                     |
| `cause`          | The underlying error, if one was wrapped.                                                        |

Prefer `type` and `recoverability` for control flow. They are the stable,
forward-compatible contract. Use `name` and `code` only for exact matching,
logging, and telemetry.

## Error handling

Catch a `KitError` and branch on `type` and `recoverability` to decide how to
recover:

```typescript TypeScript theme={null}
import { KitError } from "@circle-fin/app-kit";

try {
  const session = await kit.onramp.fetchSession({ url, body });
  kit.onramp.mountIframe({ session, container });
} catch (err) {
  if (err instanceof KitError) {
    if (err.recoverability === "RETRYABLE") {
      return scheduleRetry();
    }
    if (err.type === "INPUT") {
      return showValidationError(err.message);
    }
    if (err.type === "RATE_LIMIT") {
      return showRateLimitToast();
    }
  }
  throw err;
}
```

## Common error codes

| Code   | Name                                | Type      | When it fires                                                                                             |
| ------ | ----------------------------------- | --------- | --------------------------------------------------------------------------------------------------------- |
| `1907` | `INPUT_INVALID_API_KEY`             | `INPUT`   | API key is missing, invalid, or unauthorized (401 or 403).                                                |
| `1910` | `INPUT_WIDGET_URL_ORIGIN_MISMATCH`  | `INPUT`   | `mountIframe` or `openWindow` called with a `widgetBaseUrl` that doesn't match the server's.              |
| `1914` | `INPUT_NO_WINDOW`                   | `INPUT`   | The client kit was used in a non-browser environment. Run client code in the browser, or pass a `window`. |
| `8923` | `SERVICE_SESSION_ENDPOINT_REJECTED` | `SERVICE` | Your session route returned a non-2xx response. Check auth on your route or the request body.             |

## HTTP status code mapping

`createSessionRouteHandler` maps thrown errors to HTTP status codes
automatically:

| `type`       | HTTP status | Meaning                                   |
| ------------ | :---------: | ----------------------------------------- |
| `INPUT`      |     400     | The request body failed validation.       |
| `RATE_LIMIT` |     429     | Too many requests. Retry after a backoff. |
| `NETWORK`    |     504     | Upstream connection failed or timed out.  |
| `SERVICE`    |     502     | Upstream service returned an error.       |
| `RPC`        |     502     | Upstream RPC call failed.                 |
| `UNKNOWN`    |     500     | Unclassified error.                       |

The handler also returns:

* `405 Method Not Allowed` for any method other than `POST`.
* `400 Bad Request` if the body cannot be parsed as JSON.
* `401 Unauthorized` if your `authorize` callback returns `false`.

Upstream response bodies are never echoed into the response. Only the status
code and length of the upstream response are kept on `KitError.cause` for
diagnostic purposes.

## Error sources

| Source                                 | Throws when                                                                   | `type`                                      |
| -------------------------------------- | ----------------------------------------------------------------------------- | ------------------------------------------- |
| `new AppKit()`, `createAppServerKit()` | Constructor options are missing or invalid.                                   | `INPUT`                                     |
| `kit.onramp.mountIframe()`             | The session has expired or is malformed, or the container is missing.         | `INPUT`                                     |
| `kit.onramp.openWindow()`              | The session has expired or is malformed.                                      | `INPUT`                                     |
| `server.onramp.createSession()`        | Input is invalid, the upstream API rejects the request, or the network fails. | `INPUT`, `NETWORK`, `SERVICE`, `RATE_LIMIT` |
| `kit.onramp.fetchSession()`            | Your session route returns a non-2xx response, or the body can't be parsed.   | `INPUT`, `NETWORK`, `SERVICE`               |

Runtime widget errors are surfaced through the `INITIALIZATION_ERROR` event
rather than thrown. See
[Handle lifecycle events](/app-kit/tutorials/onramp/handle-lifecycle-events).
