Skip to main content

Errors

cairn uses structured error types to distinguish between different failure categories. Each language follows its idiomatic error handling pattern.

Error Categories

CategoryDescriptionExamples
ConnectionFailed to establish or maintain a connectionTimeout, peer unreachable, NAT traversal failed
PairingPairing process failedInvalid PIN, pairing rejected, SPAKE2 failure
SessionError during an active sessionSend failed, session closed, encryption error
ConfigurationInvalid configuration valuesInvalid URL, missing required fields
TransportLow-level transport failureWebSocket failure, TURN relay failure, mDNS failure

Error Handling Patterns

cairn uses Result<T, CairnError>. Handle errors with match or the ? operator.

use cairn_p2p::error::CairnError;

match node.connect(&peer_id).await {
Ok(session) => {
println!("Connected!");
}
Err(CairnError::Connection(e)) => {
eprintln!("Connection failed: {}", e);
}
Err(CairnError::Pairing(e)) => {
eprintln!("Pairing issue: {}", e);
}
Err(e) => {
eprintln!("Unexpected error: {}", e);
}
}

// Or use the ? operator for propagation
let session = node.connect(&peer_id).await?;
session.send("chat", b"hello").await?;

Common Error Codes

CodeCategoryDescription
CONNECTION_TIMEOUTConnectionPeer did not respond within the timeout period
PEER_UNREACHABLEConnectionNo transport path to the peer could be found
NAT_TRAVERSAL_FAILEDConnectionAll NAT traversal methods exhausted
INVALID_PINPairingThe entered PIN does not match
PAIRING_REJECTEDPairingThe remote peer rejected the pairing request
SPAKE2_FAILUREPairingSPAKE2 key exchange failed
SESSION_CLOSEDSessionOperation attempted on a closed session
SEND_FAILEDSessionMessage could not be delivered
INVALID_CONFIGConfigurationConfiguration value is invalid
TRANSPORT_FAILURETransportUnderlying transport connection failed