HTTPException

The single class you use to signal HTTP errors from anywhere in your code.

Usage

from zeromcp.exception import HTTPException

if not user.is_admin:
    raise HTTPException(403, 'Admins only')

if not row:
    raise HTTPException(404, 'Not found')

if duplicate:
    raise HTTPException(409, 'Already exists')

Response shape

ExceptionMiddleware catches HTTPException and renders:

{
  "success": false,
  "status": 403,
  "detail": "Admins only"
}

The HTTP status matches the first argument.

Validation errors

Pydantic validation errors are surfaced as HTTPException(422, [...])detail is a list of {field, message} objects:

{
  "success": false,
  "status": 422,
  "detail": [
    {"field": "email", "message": "value is not a valid email address"}
  ]
}

Common status codes

CodeUsed by 0-mcp for
400Invalid JSON body, malformed request
401Authentication required, missing/invalid session
403Authenticated but forbidden (token invalid, field not allowed, blocked IP, ALLOWED_ORIGINS rejection)
404Row not found, segment not found, ownership mismatch
405Method not allowed for this resource
409Integrity error (duplicate key)
422Pydantic validation failed
429Rate limit exceeded
500Unhandled error in production (sanitized)
🚦

HTTPException is the only "controlled" error. Anything else raised by your handler is logged and turned into a sanitized 500 in production. In DEBUG=True mode, exceptions propagate so you see the stack trace.

0-mcp by Stamatios Stamou Jr — github.com/ssjunior/0-mcp