# Error Handling

All error responses follow the same envelope:

```json
{
  "success": false,
  "data": null,
  "error": {
    "code": "PROTOCOL_NOT_FOUND",
    "message": "No protocol found with id \"nonexistent\"",
    "status": 404
  }
}
```

### 5.1 Error Code Reference

| **HTTP** | **Code**              | **Description**                                                                                                                |
| -------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| 400      | INVALID\_REQUEST      | Malformed request: missing required fields, wrong types, or constraint violations. Check error.details for field-level errors. |
| 401      | UNAUTHORIZED          | API key missing, invalid, or revoked                                                                                           |
| 403      | FORBIDDEN             | Valid key but endpoint requires a higher plan tier                                                                             |
| 404      | NOT\_FOUND            | Requested resource does not exist                                                                                              |
| 404      | PROTOCOL\_NOT\_FOUND  | No protocol matching the provided ID                                                                                           |
| 422      | UNPROCESSABLE         | Request is valid but cannot be processed                                                                                       |
| 429      | RATE\_LIMIT\_EXCEEDED | Daily or per-minute rate limit hit. Check Retry-After header.                                                                  |
| 500      | INTERNAL\_ERROR       | Unexpected server error. Retryable.                                                                                            |
| 503      | DATA\_UNAVAILABLE     | Upstream data pipeline temporarily unavailable. Retry-After header present.                                                    |

### 5.2 TypeScript: Error Handling Pattern

```typescript
const res = await fetch("https://api.diapleo.xyz/v1/protocols/nonexistent", {
  headers: { "X-API-Key": process.env.DIAPLEO_API_KEY! },
});

if (!res.ok) {
  const body = await res.json();
  const { code, message, status } = body.error;

  switch (code) {
    case "PROTOCOL_NOT_FOUND":
      console.error("Protocol ID not in Diapleo registry");
      break;
    case "RATE_LIMIT_EXCEEDED":
      const retryAfter = res.headers.get("Retry-After") ?? "60";
      console.error(`Rate limit hit. Retry in ${retryAfter}s`);
      break;
    case "FORBIDDEN":
      console.error("Upgrade your plan to access this endpoint");
      break;
    default:
      console.error(`API error: ${message} [${status}]`);
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.diapleo.com/api-reference/overview/error-handling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
