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

# Error handling

> Catch and handle typed errors thrown by the SDK.

Every error the SDK throws extends `AuthenticaError`. Use `instanceof` to handle each case.

## Error types

| Error | Meaning | Typical action |
| - | - | - |
| `AuthenticaError` | Base class for every SDK error. | Catch-all for SDK errors. |
| `AuthenticaValidationError` | Invalid client options or method input. Thrown before any request is sent. | Fix the input in your code. Nothing was sent. |
| `AuthenticaApiError` | The API responded with a non-2xx status. | Check `status` to decide what to do. |
| `AuthenticaAuthenticationError` | The API responded with 401: the API key is missing or invalid. Extends `AuthenticaApiError`. | Check the API key on your server. Retrying will not help. |
| `AuthenticaConnectionError` | No response was received (DNS, TLS or network failure). | Check outbound connectivity. |
| `AuthenticaTimeoutError` | The request exceeded `timeout`. Extends `AuthenticaConnectionError`. | The outcome is unknown. Do not resend OTPs automatically. |
| `AuthenticaResponseError` | The API responded with 2xx, but the body was empty, not valid JSON or missing expected fields. | Upgrade the SDK; contact support if it continues. |

## Properties

| Property | Type | Description |
| - | - | - |
| `AuthenticaApiError.status` | `number` | HTTP status code, for example `422`, `429` or `503`. |
| `AuthenticaApiError.retryAfterSeconds` | `number` \| `undefined` | On HTTP 429, the seconds the API asked you to wait, from a valid `Retry-After` header. Otherwise `undefined`. |
| `AuthenticaApiError.errors` | `{ message: string }[]` | Error messages from the API. May echo request values such as phone numbers; do not log blindly. |
| `AuthenticaTimeoutError.status` | `number` \| `undefined` | Set if the HTTP status arrived before the timeout fired. |
| `AuthenticaResponseError.status` | `number` | HTTP status of the response. |

`AuthenticaAuthenticationError` has the same properties as `AuthenticaApiError`.

## Example

Check subclasses before their parent: `AuthenticaAuthenticationError` before `AuthenticaApiError`, and `AuthenticaTimeoutError` before `AuthenticaConnectionError`.

```ts theme={null}
import {
  AuthenticaApiError,
  AuthenticaAuthenticationError,
  AuthenticaConnectionError,
  AuthenticaError,
  AuthenticaResponseError,
  AuthenticaTimeoutError,
  AuthenticaValidationError,
} from '@authenticasa/sdk';

try {
  await authentica.otp.send({ method: 'sms', phone: '+9665XXXXXXXX' });
} catch (error) {
  if (error instanceof AuthenticaAuthenticationError) {
    // 401: check the API key configured on your server
  } else if (error instanceof AuthenticaValidationError) {
    // Invalid input: nothing was sent
  } else if (error instanceof AuthenticaApiError) {
    // Non-2xx response: use error.status (e.g. 422, 429)
  } else if (error instanceof AuthenticaTimeoutError) {
    // Outcome unknown: the OTP may still have been sent
  } else if (error instanceof AuthenticaConnectionError) {
    // Network problem: no response received
  } else if (error instanceof AuthenticaResponseError) {
    // Unexpected response body
  } else if (error instanceof AuthenticaError) {
    // Any other SDK error
  } else {
    throw error;
  }
}
```

## Logging errors safely

SDK error messages are written by the SDK and never contain your API key or request data. `AuthenticaApiError.errors` is the exception: it holds the API's own text, which can echo values from your request.

Log the error name and status, not the whole error:

```ts theme={null}
if (error instanceof AuthenticaApiError) {
  logger.warn('Authentica request failed', {
    name: error.name,
    status: error.status,
  });
}
```

> [!WARNING]
>
> Do not log entire SDK error objects or `error.errors` without reviewing what they may contain.

## Rate limits (HTTP 429)

When the API responds with HTTP 429, the SDK throws `AuthenticaApiError` with `status` `429`. If the response includes a valid `Retry-After` header, `retryAfterSeconds` holds the wait time in seconds.

```ts theme={null}
try {
  await authentica.otp.send({ method: 'sms', phone: '+9665XXXXXXXX' });
} catch (error) {
  if (
    error instanceof AuthenticaApiError &&
    error.status === 429
  ) {
    // May be undefined if the response had no valid Retry-After header
    console.log(error.retryAfterSeconds);
    // Ask the user to try again later
  } else {
    throw error;
  }
}
```

The SDK never retries requests automatically. Your application decides whether and when to retry.

> [!WARNING]
>
> Do not blindly retry OTP sends or face and voice verifications. They may not be idempotent: a retried send can deliver a second code and use extra credits. A timeout does not prove a request failed.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.