Skip to main content

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.)
Contract uses protocol-agnostic codes that automatically translate to the right format:

Quick Reference Table

Detailed Error Code Guide

INVALID_ARGUMENT

Use when: The user sent bad input.
Common scenarios:
  • Missing required field
  • Value too long or too short
  • Invalid format (email, URL, phone)
  • Number out of allowed range
  • Wrong data type
HTTP Status: 400 Bad Request

NOT_FOUND

Use when: The requested item doesn’t exist.
Common scenarios:
  • Getting item by ID that doesn’t exist
  • Referencing deleted resource
  • Looking up user by email that’s not registered
With extra details:
HTTP Status: 404 Not Found

ALREADY_EXISTS

Use when: Trying to create something that already exists.
Common scenarios:
  • Creating user with existing email
  • Creating resource with duplicate unique ID
  • Adding item that’s already in a list
HTTP Status: 409 Conflict

PERMISSION_DENIED

Use when: User is logged in but not allowed to do this.
Common scenarios:
  • User trying to access another user’s data
  • User without admin role trying admin action
  • User trying to modify read-only resource
HTTP Status: 403 Forbidden Note: Use UNAUTHENTICATED if user isn’t logged in at all.

UNAUTHENTICATED

Use when: User needs to log in first.
Common scenarios:
  • No authentication token provided
  • Token has expired
  • Token is invalid or malformed
HTTP Status: 401 Unauthorized vs PERMISSION_DENIED:
  • 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.
Common scenarios:
  • Rate limit exceeded
  • Storage quota full
  • Too many items created
  • Concurrent request limit
HTTP Status: 429 Too Many Requests

FAILED_PRECONDITION

Use when: System isn’t in the right state for this operation.
Common scenarios:
  • Resource in wrong state for operation
  • Required setup not complete
  • Dependency not satisfied
  • Order of operations violated
HTTP Status: 412 Precondition Failed

INTERNAL

Use when: Something unexpected went wrong on the server.
With cause for logging:
Common scenarios:
  • Database errors
  • Unexpected nil values
  • Bug in your code
  • External service returned unexpected response
HTTP Status: 500 Internal Server Error Important: Never expose internal details to clients:

UNAVAILABLE

Use when: Service is temporarily unavailable.
Common scenarios:
  • Planned maintenance
  • Database connection lost
  • Dependent service is down
  • Server overloaded
HTTP Status: 503 Service Unavailable vs INTERNAL:
  • UNAVAILABLE: Temporary problem, try again later
  • INTERNAL: Something is broken, might need fixing

UNIMPLEMENTED

Use when: Feature doesn’t exist yet.
Common scenarios:
  • Feature not built yet
  • Method stub that’s not implemented
  • Format or option not supported
HTTP Status: 501 Not Implemented

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, use NewError:

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