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
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: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