Error Codes Reference
Contract uses a standard set of error codes that work across all protocols. This page explains each code, when to use it, and how it maps to HTTP, JSON-RPC, and other protocols.Why Standard Error Codes?
Different protocols handle errors differently:- HTTP uses status codes (404, 500, etc.)
- JSON-RPC uses negative error codes (-32600, -32601, etc.)
- gRPC uses its own codes (NOT_FOUND, INTERNAL, etc.)
Quick Reference Table
| Error Code | HTTP | JSON-RPC | Use When |
|---|---|---|---|
INVALID_ARGUMENT | 400 | -32602 | Bad input from user |
NOT_FOUND | 404 | -32601 | Resource doesn’t exist |
ALREADY_EXISTS | 409 | -32003 | Trying to create duplicate |
PERMISSION_DENIED | 403 | -32004 | User can’t do this |
UNAUTHENTICATED | 401 | -32011 | User not logged in |
RESOURCE_EXHAUSTED | 429 | -32005 | Rate limited |
FAILED_PRECONDITION | 412 | -32006 | System not ready |
INTERNAL | 500 | -32603 | Server bug |
UNAVAILABLE | 503 | -32009 | Service down |
UNIMPLEMENTED | 501 | -32601 | Feature not built |
Detailed Error Code Guide
INVALID_ARGUMENT
Use when: The user sent bad input.- Missing required field
- Value too long or too short
- Invalid format (email, URL, phone)
- Number out of allowed range
- Wrong data type
NOT_FOUND
Use when: The requested item doesn’t exist.- Getting item by ID that doesn’t exist
- Referencing deleted resource
- Looking up user by email that’s not registered
ALREADY_EXISTS
Use when: Trying to create something that already exists.- Creating user with existing email
- Creating resource with duplicate unique ID
- Adding item that’s already in a list
PERMISSION_DENIED
Use when: User is logged in but not allowed to do this.- User trying to access another user’s data
- User without admin role trying admin action
- User trying to modify read-only resource
UNAUTHENTICATED if user isn’t logged in at all.
UNAUTHENTICATED
Use when: User needs to log in first.- No authentication token provided
- Token has expired
- Token is invalid or malformed
UNAUTHENTICATED: “Who are you?” (not logged in)PERMISSION_DENIED: “I know who you are, but you can’t do this” (logged in but not allowed)
RESOURCE_EXHAUSTED
Use when: Some limit has been reached.- Rate limit exceeded
- Storage quota full
- Too many items created
- Concurrent request limit
FAILED_PRECONDITION
Use when: System isn’t in the right state for this operation.- Resource in wrong state for operation
- Required setup not complete
- Dependency not satisfied
- Order of operations violated
INTERNAL
Use when: Something unexpected went wrong on the server.- Database errors
- Unexpected nil values
- Bug in your code
- External service returned unexpected response
UNAVAILABLE
Use when: Service is temporarily unavailable.- Planned maintenance
- Database connection lost
- Dependent service is down
- Server overloaded
UNAVAILABLE: Temporary problem, try again laterINTERNAL: Something is broken, might need fixing
UNIMPLEMENTED
Use when: Feature doesn’t exist yet.- Feature not built yet
- Method stub that’s not implemented
- Format or option not supported
Other Error Codes
These are less commonly used but available:| Code | HTTP | Use When |
|---|---|---|
CANCELED | 499 | Request was canceled by client |
UNKNOWN | 500 | Error doesn’t fit other categories |
DEADLINE_EXCEEDED | 504 | Operation timed out |
ABORTED | 409 | Concurrent modification conflict |
OUT_OF_RANGE | 400 | Pagination or index out of bounds |
DATA_LOSS | 500 | Unrecoverable data corruption |
Creating Errors
Convenience Functions
The most common errors have shortcut functions:General Constructor
For other codes, useNewError:
With Format String
Adding Context to Errors
Add Details
Include structured data for debugging:Add Multiple Details
Add Cause
Wrap the underlying error (useful for logging):Checking Error Types
In Your Code
Get HTTP Status
Convert HTTP Status to Error Code
How Errors Look in Different Protocols
REST
JSON-RPC
MCP
Decision Guide
Use this flowchart to pick the right error code:Best Practices
1. Be Specific But Safe
2. Use the Right Code
3. Log Internal Errors
4. Add Context When Helpful
See Also
- Error Handling - Error patterns and examples
- Transports Overview - How errors travel
- API Reference - Complete error API