Error Codes
How the Anvio REST API reports failures — a mix of soft JSON errors and real HTTP status codes, by controller.
4 min read
Anvio's API does not have a single, centralized error envelope or global ExceptionFilter. Error reporting is controller-specific — grounded here directly in apps/api/src, not standardized after the fact. Read this page before assuming a 4xx/5xx status code means what it usually means elsewhere.
There is no global NestJS exception filter in apps/api/src. Each controller decides its own error shape. The two patterns below (soft JSON error vs real HTTP status) coexist in the same API.
Pattern 1: soft JSON errors (HTTP 200)
AgentsController and SessionsController return a 200 OK with an { "error": "..." } body for domain-level failures — "not found" is not a 404 here.
// apps/api/src/agents.controller.ts
@Get(':name')
async get(@Param('name') name: string) {
try {
return await this.appService.platform.workspace.loader.loadAgent(name);
} catch {
return { error: 'Not found' };
}
}| Endpoint | Condition | Response | HTTP status |
|---|---|---|---|
GET /api/agents/:name | Agent file doesn't exist in workspace | { "error": "Not found" } | 200 |
POST /api/sessions | agentName doesn't resolve to a loadable agent | { "error": "Agent not found" } | 200 |
GET /api/sessions/:id | Session doesn't exist, or auth is enabled and the caller's userId doesn't match the session's owner | { "error": "Not found" } | 200 |
POST /api/sessions/:id/messages | Same as above | { "error": "Not found" } | 200 |
Clients must check for an error key in the response body — checking response.ok or status === 200 alone is not sufficient for these four endpoints. This is a real gap between REST convention and current behavior; don't build client code that assumes HTTP status reflects success/failure here.
Pattern 2: real HTTP status codes
The three channel webhook controllers (WhatsAppController, TeamsController, MatrixController) use actual HttpStatus codes via Express Response injection (@Res()), because they're satisfying external platform contracts (Meta, Bot Framework) that expect real status codes.
| Status | Meaning | Where |
|---|---|---|
200 OK | Webhook processed / verification succeeded | All three webhook controllers |
400 Bad Request | Missing required field (roomId, senderId, or body) | POST /api/channels/matrix/webhook only |
403 Forbidden | WhatsApp hub.verify_token mismatch | GET /api/channels/whatsapp/webhook |
404 Not Found | Channel adapter not enabled/registered | All three webhook controllers |
// apps/api/src/matrix.controller.ts
if (!payload.roomId || !payload.senderId || !payload.body) {
res.status(HttpStatus.BAD_REQUEST).send('roomId, senderId, and body required');
return;
}Note the body on these error responses is a plain string, not JSON — e.g. res.status(HttpStatus.NOT_FOUND).send('Teams channel not registered'). Don't JSON.parse() webhook error bodies.
Health and metrics
GET /api/health and GET /api/metrics have no documented error paths in source — they always return 200. /api/metrics returns Prometheus text, not JSON, regardless of scrape success/failure of underlying collectors.
NestJS framework-level errors
Anything not explicitly handled by controller code (malformed JSON body, unknown route, unhandled exception thrown deep in platform code) falls through to Nest's default exception handling, which returns a standard Nest JSON error shape:
{ "statusCode": 500, "message": "Internal server error" }TODO: verify against
apps/api/src— no customExceptionFilterwas found, so this is Nest's built-in default, not an Anvio-specific contract. If a global filter is added later, update this section.
Event bus and async failures
Because POST /api/sessions/:id/messages only queues work (publishes anvio.agent.run.requested.v1 and returns), failures during actual agent execution do not surface as an HTTP error at all — the request already returned 200 { status: 'queued' }. Failures are reported asynchronously as an anvio.agent.run.failed.v1 event instead. If you need to know whether a run failed, subscribe to that event or poll session state — don't rely on the HTTP response. See Event Model and AgentRunFailedData in packages/events/src/types.ts:
export interface AgentRunFailedData {
sessionId: string;
error: string;
channel: string;
}Quick reference
| Symptom | What it actually means | Where to look next |
|---|---|---|
200 with { "error": "Not found" } | Resource missing, or ownership mismatch under auth | Check the error field, not the status code |
403 on WhatsApp webhook GET | hub.verify_token sent by Meta doesn't match configured token | WHATSAPP_VERIFY_TOKEN |
404 on any /api/channels/*/webhook | Adapter not registered (missing required config for that channel) | packages/channels/src/create-channels.ts |
400 on Matrix webhook | Missing roomId/senderId/body in POST body | Webhook payload shape |
| Session created but no reply ever arrives | Async failure — check anvio.agent.run.failed.v1 | Event bus / worker logs |
Last updated on