Better Auth RS

Errors

Error types, HTTP status codes, and error response format.

Error Response Format

All errors return JSON:

{
  "message": "Description of the error"
}

For 5xx errors, the actual error details are hidden and a generic message is returned to avoid leaking internal information.

AuthError Variants

VariantHTTP StatusDescription
BadRequest400Malformed request
InvalidRequest400Invalid request parameters
Validation400Input validation failed
InvalidCredentials401Wrong email/password
Unauthenticated401No valid session token
SessionNotFound401Session token not found or expired
Forbidden403Action not allowed
Unauthorized403Insufficient permissions
UserNotFound404User does not exist
NotFound404Resource not found
Conflict409Duplicate resource (email, username)
RateLimited429Too many requests
NotImplemented501Feature not implemented
Config500Configuration error
Database500Database operation failed
Serialization500JSON serialization error
Plugin500Plugin-specific error
Internal500Unexpected internal error
PasswordHash500Password hashing failure
Jwt500JWT encoding/decoding error

DatabaseError Variants

Database errors are wrapped in AuthError::Database:

VariantDescription
ConnectionFailed to connect to database
QueryQuery execution failed
MigrationMigration failed
ConstraintUnique constraint violation
TransactionTransaction failed

Programmatic Error Handling

use better_auth::error::{AuthError, AuthResult};

fn handle_result(result: AuthResult<()>) {
    match result {
        Ok(()) => println!("Success"),
        Err(e) => {
            println!("Status: {}", e.status_code());
            println!("Message: {}", e.message());
        }
    }
}

Constructors for common errors:

AuthError::bad_request("Invalid input");
AuthError::forbidden("Not allowed");
AuthError::not_found("User not found");
AuthError::conflict("Email already exists");
AuthError::internal("Unexpected error");
AuthError::validation("Password too short");
AuthError::plugin("my-plugin", "Something went wrong");

On this page