Skip to content

Error Codes

Error Codes Reference

Overview

M2M Protocol defines error types for all operations. This document covers session errors, compression errors, security errors, and cryptographic errors.

M2MError Variants

The main error type for M2M Protocol operations.

Compression Errors

VariantDescriptionCommon Causes
Compression(String)Compression operation failedInvalid input, Brotli error
Decompression(String)Decompression operation failedCorrupted data, wrong algorithm
InvalidCodec(String)Unknown or unsupported codecInvalid prefix, version mismatch

Session Errors

VariantDescriptionRecovery
SessionNotEstablishedOperation requires established sessionCall connect() first
SessionExpiredSession has timed outEstablish new session
NegotiationFailed(String)Capability negotiation failedCheck capabilities
CapabilityMismatch(String)Peers have incompatible capabilitiesAdjust capabilities
Protocol(String)Protocol-level errorCheck message format
InvalidMessage(String)Invalid message formatValidate input

Security Errors

VariantFieldsDescription
SecurityThreatthreat_type: String, confidence: f32Threat detected in content
ContentBlocked(String)-Content blocked by security policy

Cryptographic Errors

VariantDescriptionSource
Crypto(CryptoError)Cryptographic operation failedSee CryptoError

Note: The Crypto variant preserves the full error chain via #[source], enabling debugging tools to display complete error context.

Infrastructure Errors

VariantDescriptionCommon Causes
Network(String)Network communication errorConnection failed, timeout
Upstream(String)Upstream service errorLLM API error
Server(String)Server-side errorInternal server error
Config(String)Configuration errorInvalid config file
Io(std::io::Error)I/O errorFile not found, permission denied
Json(serde_json::Error)JSON parsing errorInvalid JSON

ML/Inference Errors

VariantDescriptionResolution
ModelNotLoaded(String)ML model not loadedLoad model first
ModelLoad(String)Failed to load ML modelCheck model path
ModelNotFound(String)Model not in registryRegister model
Inference(String)ML inference errorCheck input format
Tokenizer(String)Tokenizer errorCheck tokenizer config

CryptoError Variants

Unified error type for cryptographic operations. All variants preserve error source chain.

AEAD Errors

VariantDescriptionCause
Aead(AeadError::InvalidKey)Invalid AEAD keyKey too short (<32 bytes)
Aead(AeadError::EncryptionFailed)Encryption failedInternal crypto error
Aead(AeadError::DecryptionFailed)Decryption failedWrong key, corrupted data, tampered
Aead(AeadError::DataTooShort)Ciphertext too shortMissing nonce or tag

HMAC Errors

VariantDescriptionCause
Hmac(HmacError::InvalidKey)Invalid HMAC keyKey too short
Hmac(HmacError::VerificationFailed)HMAC verification failedWrong key, tampered data
Hmac(HmacError::DataTooShort)Data too short for HMACMissing tag

Key Management Errors

VariantDescriptionCause
Key(KeyError::Empty)Empty key materialZero-length key provided
Key(KeyError::TooShort)Key too shortBelow minimum length
Keyring(KeyringError::KeyNotFound)Key not found in keyringKey ID doesn’t exist
Keyring(KeyringError::DerivationFailed)Key derivation failedHKDF error

Nonce Errors

VariantDescriptionCause
Nonce(NonceError::RngFailure)CSPRNG failedSystem RNG unavailable

Key Exchange Errors

VariantDescriptionCause
Exchange(KeyExchangeError::InvalidPublicKey)Invalid public keyWrong size, invalid point
Exchange(KeyExchangeError::GenerationFailed)Key generation failedRNG error

ID Validation Errors

VariantDescriptionCause
Id(IdError::Empty)Empty IDAgentId or OrgId is empty
Id(IdError::TooLong)ID too longExceeds 128 characters
Id(IdError::InvalidChars)Invalid charactersMust be alphanumeric, hyphen, underscore

Session Rejection Codes

Returned in REJECT messages during session establishment.

CodeDescriptionRecovery
VERSION_MISMATCHProtocol version not supportedUse supported version
NO_COMMON_ALGORITHMNo mutually supported algorithmsAdd required algorithms
SECURITY_POLICYSecurity policy violationReview security settings
RATE_LIMITEDRate limit exceededRetry with backoff
SERVER_BUSYServer at capacityRetry later
UNKNOWNUnspecified errorContact support

Closure Reason Codes

Returned in CLOSE messages during session termination.

CodeDescription
NORMALClean shutdown
TIMEOUTSession timeout exceeded
ERRORProtocol error occurred
CLIENT_SHUTDOWNClient application closing
SERVER_SHUTDOWNServer shutting down

Security Scan Results

Threat types detected by security scanner.

Threat TypeSeverityDescription
PROMPT_INJECTIONHighAttempt to override system instructions
JAILBREAKCriticalAttempt to bypass safety measures
DATA_EXFILTRATIONHighAttempt to extract sensitive data
MALFORMED_INPUTMediumNull bytes, unicode exploits
EXCESSIVE_NESTINGMediumJSON depth exceeds limit

HTTP Status Codes (Proxy)

StatusEndpointMeaning
200AllSuccess
400/v1/*Invalid request format
401/v1/*Missing or invalid API key
413/v1/*Payload too large
422/v1/*Security scan failed (blocking mode)
429/v1/*Rate limit exceeded
500AllInternal server error
502/v1/*Upstream server error
504/v1/*Upstream timeout

Error Response Format

Proxy Error Response

{
"error": {
"code": "SECURITY_VIOLATION",
"message": "Content blocked by security scanner",
"details": {
"threat_type": "PROMPT_INJECTION",
"confidence": 0.95
}
}
}

Crypto Error Response

{
"error": {
"code": "CRYPTO_ERROR",
"message": "Crypto error: AEAD: Decryption failed: authentication tag mismatch",
"source": "AeadError::DecryptionFailed"
}
}

Rust Error Types

M2MError

pub enum M2MError {
// Compression
Compression(String),
Decompression(String),
InvalidCodec(String),
// Session
Protocol(String),
NegotiationFailed(String),
SessionNotEstablished,
SessionExpired,
InvalidMessage(String),
CapabilityMismatch(String),
// Security
SecurityThreat { threat_type: String, confidence: f32 },
ContentBlocked(String),
// Cryptography (preserves error chain via #[source])
Crypto(#[source] CryptoError),
// Infrastructure
Network(String),
Upstream(String),
Server(String),
Config(String),
// ML/Inference
ModelNotLoaded(String),
Inference(String),
ModelLoad(String),
ModelNotFound(String),
Tokenizer(String),
// Standard errors
Json(#[from] serde_json::Error),
Io(#[from] std::io::Error),
}

CryptoError

pub enum CryptoError {
Aead(#[source] AeadError),
Hmac(#[source] HmacError),
Key(#[source] KeyError),
Keyring(#[source] KeyringError),
Nonce(#[source] NonceError),
Exchange(#[source] KeyExchangeError), // crypto feature
Id(#[source] IdError), // crypto feature
}

Error Chain Example

The CryptoError type preserves the full error chain:

use std::error::Error;
fn handle_error(err: M2MError) {
println!("Error: {}", err);
// Walk the error chain
let mut source = err.source();
while let Some(cause) = source {
println!(" Caused by: {}", cause);
source = cause.source();
}
}
// Output:
// Error: Crypto error: AEAD: Decryption failed: authentication tag mismatch
// Caused by: Decryption failed: authentication tag mismatch