# Error Handling

> ZopNight uses standard HTTP status codes and returns structured JSON error responses. Reference for status codes, retry strategy, and common error scenarios.

Source: https://zop.dev/developer-docs/reference/errors

---

ZopNight uses standard HTTP status codes and returns structured error responses.

## Error Response Format

Every error body has an `error` field. Its shape depends on where the error was raised.

**Gateway errors** (permissions, rate limits, routing, PAT checks) carry a string `error`. Some also carry a machine-readable `code`:

```json
{
  "error": "forbidden",
  "code": "rbac_denied"
}
```

**Service errors** (validation, not found, conflicts, server faults) carry an `error` object with a `message`. Some also carry a `code`:

```json
{
  "error": {
    "message": "'1' missing parameter(s): name"
  }
}
```

**OAuth endpoints** (`/oauth/register`, `/oauth/token`, `/oauth/revoke`) answer errors in the RFC 6749 form, with a string `error` code and an `error_description`.

**Info**

The message text is produced by the service that handled the request, so wording
can vary between services (resources, schedules, deployments, etc.). Match on
`code` where one is sent, not on the message text.

## HTTP Status Codes

<table>
  <thead>
    <tr>
      <th>Code</th>
      <th>Meaning</th>
      <th>Common Causes</th>
    </tr>
  </thead>
  <tbody>
    {[
      ['200', 'OK', 'Request succeeded'],
      ['201', 'Created', 'Resource created successfully'],
      ['400', 'Bad Request', 'Invalid request body, missing required fields, validation errors'],
      ['401', 'Unauthorized', 'Missing, invalid, or expired JWT token'],
      ['403', 'Forbidden', "Insufficient permissions or accessing another organization's resources"],
      ['404', 'Not Found', 'Resource, schedule, or endpoint does not exist'],
      ['409', 'Conflict', 'Duplicate resource, schedule name already exists'],
      ['429', 'Too Many Requests', 'Rate limit exceeded, see Rate Limiting; or BUSY: another change to the same data held it, see Busy below'],
      ['500', 'Internal Server Error', 'Unexpected server error, retry with backoff'],
      ['502', 'Bad Gateway', 'Gateway could not reach the backend service'],
      ['503', 'Service Unavailable', 'Service is temporarily unavailable'],
    ].map(([code, meaning, causes]) => (
      <tr key={code}>
        <td>
          <code className={`px-1.5 py-0.5 rounded text-xs font-bold ${
            code.startsWith('2') ? 'bg-emerald-100 text-emerald-700' :
            code.startsWith('4') ? 'bg-amber-100 text-amber-700' :
            'bg-red-100 text-red-700'
          }`}>{code}</code>
        </td>
        <td><strong>{meaning}</strong></td>
        <td>{causes}</td>
      </tr>
    ))}
  </tbody>
</table>

## Retry Strategy

| Status | Action |
|---|---|
| **4xx (except 429)** | Do not retry. Fix the request and try again. |
| **429** | If the response has a `Retry-After` header, wait that many seconds. The per-org REST limit and BUSY send none: wait for the next clock minute (REST limit) or retry after a short backoff of 1s, 2s, 4s (BUSY). |
| **500** | Retry with exponential backoff (1s, 2s, 4s, 8s). |
| **502/503** | Retry with exponential backoff. The service may be restarting. |

**Tip**

Repeating a start or stop on a resource that is already in that state is usually harmless, but
each call is recorded as a separate action. Check the action's status before retrying rather
than assuming the first call failed.

## Common Error Scenarios

### Expired Token

An expired or invalid JWT access token is refused at the gateway, before it reaches any service:

```json
HTTP 401
{
  "error": {
    "message": "invalid auth header in key 'Authorization'"
  }
}
```

Use the `/auth/refresh` endpoint to get a new access token.

### Invalid Request Body

Creating a schedule without a `name`:

```json
HTTP 400
{
  "error": {
    "message": "'1' missing parameter(s): name"
  }
}
```

### Resource Not Found

```json
HTTP 404
{
  "error": {
    "message": "No entity found with dashboard: 3f2b8c1e-6d4a-4b7e-9a51-2c8e7f0d1a93"
  }
}
```

A `404` with `route not found` usually means the path is wrong, for example because it includes
`/orgs/{orgId}`. Call the path without the org; the gateway adds it.

### Rate Limited

```json
HTTP 429
{
  "error": "rate limit exceeded"
}
```

The MCP server adds a `code` so an agent can tell a retryable limit from a permission error. It also sends a `Retry-After` header:

```json
HTTP 429
Retry-After: 42

{
  "error": "rate limit exceeded",
  "code": "rate_limited"
}
```

The `/oauth/*` endpoints use the RFC 6749 form, also with `Retry-After`:

```json
HTTP 429
Retry-After: 42

{
  "error": "slow_down",
  "error_description": "Rate limit exceeded on /oauth. Retry after the cool-down window."
}
```

### Busy

```json
HTTP 429
{
  "error": {
    "code": "BUSY",
    "message": "the service is busy with another change to the same data; retry the request"
  }
}
```

Another request was changing the same data and the database gave up waiting for it.
Nothing was changed. The response carries no wait period (no `Retry-After`
header): retry the same request after a short backoff. Match on
`error.code` = `BUSY`, not on the message text. Routes that can answer it
include `POST /users`, `PATCH /users/{email}`, `DELETE /users/{email}`,
`POST /teams/{teamID}/members`, the preference writes under `/me/preferences`, and
`DELETE /cloud-accounts/{caID}/iam-import/disconnect`.
