-
Notifications
You must be signed in to change notification settings - Fork 1
Example migration
Build:
cmake --preset linux-ninja-release && cmake --build --preset linux-ninja-release
This directory contains example code demonstrating the migration from legacy error handling patterns to the new production-ready tl::expected-based error handling system.
ThemisDB is migrating to a unified, type-safe error handling system using tl::expected<T, E> (forward-compatible with C++23 std::expected). This provides:
- Type-safe error propagation with zero overhead (no exceptions)
- Structured error codes with machine-readable metadata
- Rich error context embedded in error objects
- Composable error handling with monadic operations
- Compiler-enforced error checking
File: index_manager_migration_example.cpp
Pattern: Methods returning pointers that may be null
Before:
ISecondaryIndex* index = manager.createSecondaryIndex("my_index", "field", "{}");
if (!index) {
// Lost error context - why did it fail?
spdlog::error("Failed to create index");
return;
}
// Use index...After:
auto result = manager.createSecondaryIndex("my_index", "field", "{}");
if (result) {
ISecondaryIndex* index = *result;
// Use index...
} else {
const Error& err = result.error();
spdlog::error("Failed to create index: {} (code: {})",
err.message(), static_cast<int>(err.code()));
// Get detailed solution
auto metadata = err.metadata();
spdlog::info("Solution:\n{}", metadata.solution);
// Handle specific error types
switch (err.code()) {
case ErrorCode::ERR_INDEX_NOT_INITIALIZED:
// Take specific action for this error
break;
case ErrorCode::ERR_INDEX_CREATION_FAILED:
// Different action for creation failure
break;
}
}Benefits:
- Distinguish between "not initialized", "creation failed", "invalid request", etc.
- Error context includes details (e.g., index name)
- Structured solutions from error registry
- Type-safe error codes
File: contentfs_migration_example.cpp
Pattern: Custom Status struct with bool + message
Before:
auto status = contentfs.put("doc123", data, "text/plain");
if (!status.ok) {
spdlog::error("Put failed: {}", status.message); // Just a string!
return;
}After:
auto result = contentfs.put("doc123", data, "text/plain");
if (!result) {
const Error& err = result.error();
spdlog::error("Put failed: {}", err.message());
// Handle specific errors programmatically
if (err.code() == ErrorCode::ERR_STORAGE_DISK_FULL) {
// Cleanup and retry
cleanup_old_data();
result = contentfs.put("doc123", data, "text/plain");
}
}Benefits:
- Eliminates duplicate Status struct definitions across modules
- Machine-readable error codes (vs. parsing string messages)
- Richer error metadata (cause, solution, related docs)
- Type-safe composition with
and_then
File: tsstore_migration_example.cpp
Pattern: std::optional for operations that may fail
Before:
auto components = ts_store.parseKey("ts:metrics:123:cpu");
if (!components) {
// Why did it fail? Wrong prefix? Invalid timestamp? Empty field?
spdlog::error("Parse failed");
return;
}
// Use components...After:
auto result = ts_store.parseKey("ts:metrics:123:cpu");
if (result) {
const auto& comp = *result;
// Use components...
} else {
const Error& err = result.error();
spdlog::error("Parse failed: {}", err.message());
// Different handling based on error type
switch (err.code()) {
case ErrorCode::ERR_API_INVALID_REQUEST:
// Missing prefix - not a TS key
break;
case ErrorCode::ERR_SCHEMA_INVALID_TYPE:
// Invalid timestamp format - corrupted?
break;
case ErrorCode::ERR_QUERY_PARSE_FAILED:
// Malformed key structure
break;
}
}Benefits:
- Detailed error information (vs. just "nullopt")
- Error context preserved through operation chains
- Specific error codes for each failure mode
- Composable with monadic operations
Location: include/utils/expected.h
class Error {
ErrorCode code_; // Structured error code
std::string context_; // Dynamic context (paths, IDs, etc.)
std::string message() const; // Formatted message
ErrorMetadata metadata() const; // Rich metadata from registry
};template<typename T>
using Result = tl::expected<T, Error>;// Create success result
Result<int> Ok(42);
// Create error result
Result<int> Err<int>(ErrorCode::ERR_STORAGE_FILE_NOT_FOUND, "/tmp/config.yaml");
// For void operations
Result<void> OkVoid();
Result<void> ErrVoid(ErrorCode::ERR_STORAGE_DISK_FULL, "Insufficient space");
// Convert legacy patterns
Result<T*> fromNullable(ptr, ErrorCode::ERR_INDEX_NOT_FOUND);
Result<T> fromOptional(opt, ErrorCode::ERR_QUERY_PARSE_FAILED);
Result<void> fromBoolStatus(ok, message, ErrorCode::ERR_STORAGE_CORRUPTION);Error codes are organized by category:
| Category | Range | Examples |
|---|---|---|
| Storage | 1000-1999 | FILE_NOT_FOUND, PERMISSION_DENIED, DISK_FULL |
| LLM | 2000-2099 | MODEL_NOT_FOUND, CONTEXT_CREATION_FAILED, GPU_OOM |
| LoRA | 2100-2199 | NOT_LOADED, FUSION_FAILED, WEIGHT_MISMATCH |
| MCP | 3000-3999 | TRANSPORT_FAILED, INVALID_REQUEST, TOOL_NOT_FOUND |
| Schema | 4000-4999 | TABLE_NOT_FOUND, INVALID_TYPE, CACHE_MISS |
| Network | 5000-5999 | CONNECTION_REFUSED, TIMEOUT, DNS_FAILURE |
| Index | 6000-6099 | NOT_INITIALIZED, CREATION_FAILED, NOT_FOUND |
| Query | 6100-6199 | PARSE_FAILED, INVALID_SYNTAX, EXECUTION_FAILED |
| API | 6200-6299 | INVALID_REQUEST, UNAUTHORIZED, RATE_LIMIT |
| Plugin | 6300-6399 | NOT_FOUND, LOAD_FAILED, INCOMPATIBLE |
Each error code includes:
- Category (e.g., "Storage", "Query")
- Severity ("Critical", "Error", "Warning")
- Message template (with placeholders)
- Cause (detailed explanation)
- Solution (step-by-step resolution)
- Related docs (documentation links)
- Keywords (for searching)
Result supports monadic composition:
// Chain operations with automatic error propagation
auto result = readConfig("/etc/config.yaml")
.and_then([](const std::string& content) {
return parseYaml(content);
})
.and_then([](const Config& cfg) {
return validateConfig(cfg);
})
.and_then([](const Config& cfg) {
return applyConfig(cfg);
});
if (result) {
spdlog::info("Configuration applied successfully");
} else {
// Error from any step is automatically propagated
spdlog::error("Configuration failed: {}", result.error().message());
}- β
Add
tl-expecteddependency - β
Create
include/utils/expected.hwrapper - β Add error codes for Index, Query, API, Plugin
- β Register error codes in ErrorRegistry
- β Add unit tests
- β IndexManager migration example
- β ContentFS migration example
- β TSStore migration example
- Migrate high-value/high-traffic code paths first
- Keep conversion helpers for gradual migration
- Update tests alongside code migration
- Maintain backward compatibility during transition
- Migrate remaining ~300+ error sites
- Remove legacy error patterns
- Update all documentation
- Deprecate conversion helpers
Unit tests for the error handling infrastructure:
File: tests/test_expected.cpp
Tests cover:
- Error class construction and metadata
- Result success and error cases
- Result for void operations
- Conversion helpers (fromNullable, fromOptional, fromBoolStatus)
- Monadic operations (and_then, value_or)
- Error code metadata retrieval
Run tests:
ctest -R ErrorHandlingTests --output-on-failure-
P0709R4: "Zero-overhead deterministic exceptions" (Herb Sutter, 2019)
- Foundation for
std::expectedproposal - Demonstrates zero-overhead error propagation
- Foundation for
-
P1886R0: "Error Speed Benchmarking" (Ben Craig, 2019)
- Performance analysis of expected vs exceptions
- Shows expected is faster in error-heavy codepaths
-
tl::expected: C++11/14/17 implementation of expected
- Header-only library
- Forward-compatible with C++23
std::expected - Zero-overhead abstraction
-
Error Registry:
include/utils/error_registry.h -
Expected Wrapper:
include/utils/expected.h -
Migration Examples:
examples/migration/ -
Tests:
tests/test_expected.cpp
For questions about migration patterns or error handling best practices, see:
- Migration examples in this directory
- Error Registry documentation:
include/utils/error_registry.h - Unit tests:
tests/test_expected.cpp
Note: These are EXAMPLE files showing migration patterns. Actual migration involves:
- Updating method signatures in header files
- Updating implementations in source files
- Updating all call sites
- Updating tests
- Maintaining backward compatibility during transition
ThemisDB 1.9.0-beta Β· Home Β· Module-Index Β· GitHub Β· Issues
ThemisDB 1.9.0-beta Β· Home Β· Wiki-Index Β· Module-Index Β· FAQ Β· Quick-Reference Β· GitHub Β· Issues Β· Discussions Β· License
- Batch Operations
- Best Practices
- CRUD Tutorial
- Custom Document Ingestion
- Getting Started Tutorial
- Interactive Examples
- Schema Design
- Video Tutorials
- AQL Reference
- AQL Examples
- AQL Overview
- AQL Feature Roadmap
- AQL Geospatial Guide
- AQL LLM Migration Guide
- AQL API
- AQL Grammar (EBNF)
- AQL Root Overview
- AQL Examples (root)
- API Reference
- API Module README
- OpenAPI Overview
- Client SDK Overview
- SDK Overview
- Operations
- Operations Overview
- Operations Runbook
- Operations Handbook
- ThemisCtl Admin Guide
- Pipeline E2E SOPs
- Deploy Overview
- Docker Overview
- Docker Hub README
- Helm Overview
- Packaging Overview
- Operator Overview
- Security Policy
- Production Hardening Checklist
- Security Hardening Guide
- Encryption Key Management
- Access Control Framework
- Zero Trust Policy
- API Authentication & Authorization
- HSM Production Setup
- PKCS11 Integration
- DSGVO / SOC2 Checklist
- Access Model Runbooks
- Access Model Dashboard
- Maturity Automation Runbook
- Access Review Automation
- Access Model Dashboard
- Access Model Runbooks
- Rights Revocation
- Dr Checklists
- Dr Testing
- Incident Response Playbook
- Incident Response Testing
- GPU Oom Recovery
- Grammar Debugging
- Metrics Scrape Troubleshooting
- Model Swap Procedure
- Quota Tuning
- Subagent Deployment
- Logging Configuration
- Content Model
- Crypto & Keys
- Feature Flags Reference
- Modular Architecture Roadmap
- Modularization Guide
- Module Architecture Index
- PostgreSQL Wire Protocol
- Query Scheduling
- Raft Consensus Design
- Resource Pooling
- Source Directory Guide
- Unified Access Model
- E1 001 Layered Retrieval Design
- E1 002 Ann Abstraction Strategy
- E1 003 Tensor Summary Types
- E1 004 Lora Package Distinction
- E1 005 Model Switch Compatibility
- E1 006 Federated Tensor Summaries
- E2 001 Evaluation Framework Design
- E2 002 Hardware Profile Strategy
- E2 003 Query Planner Routing Model
- E2 004 Approximation Governance Rules
- E2 005 Cross Layer Fallback Confidence Policy
- E3 001 Distributed Tensor Design
- E3 002 Manifest Coordination Strategy
- E3 003 Recovery And Erasure Choice
- E3 004 Tensor Fabric Infrastructure
- Contributing
- Contributing (root)
- Code of Conduct
- Support
- Maintainers
- CTest Guide
- Build Quick Reference
- Developer Wiki Index
- Build / Test / CI
- Module Index
- Branching Strategy
- Disabled Stub Policy
- Docs PR Policy
- GA Promotion Sign Off
- Github Milestones Setup
- Maturity Claim Verification Checklist
- Maturity Evidence Registry
- Merge Gate Bot Config
- Merge Gate Status Live
- Phase 1 Closure Report
- Phase Closure Policy
- Phase Dependency Graph
- Phase3 Enforcement Runbook
- Plugin Submodule Rollback
- PR Version Targeting
- PR Version Targeting Backfill
- Production Ready 2026 Delivery Plan
- Query Module Status
- Readme
- Release Promotion Gate Policy
- Release Validation Checklist
- Security Module 5671 Evidence Summary
- Sharding P6 Residual Risk Acceptance
- Sourcecode Compliance Governance
- Updates Development Status Sign Off
- Wave C Implementation Complete
- Blob Storage
- Cuda
- Ethics Ai
- Exporters
- Huggingface
- Image Analysis
- Importers
- RPC
- Scraper
- Themisdb Ai Watermark Detector
- User Storage Encrypted
- Chimera Architecture
- Chimera Future
- Chimera Readme
- Chimera Roadmap
- Covina Fastapi Ingestion Architecture
- Covina Fastapi Ingestion Future
- Covina Fastapi Ingestion Roadmap
- Vcc Base Architecture
- Vcc Base Future
- Vcc Base Roadmap
- Vcc Clara Ingestion Architecture
- Vcc Clara Ingestion Future
- Vcc Clara Ingestion Roadmap
- Vcc Veritas Architecture
- Vcc Veritas Future
- Vcc Veritas Roadmap
- 01 Hello World
- 02 Todo App
- 03 Contact Manager
- 04 Inventory System
- 05 Time Series Monitor
- 06 Graph Social Network
- 07 Vector Search Documents
- 08 Dms Erp System
- 09 Iot Sensor Network
- 10 Drone Image Analysis
- 11 Blog Wiki
- 12 Expense Tracker
- 13 Recipe Manager
- 14 Ecommerce Catalog
- 15 Event Management
- 16 Kanban Board
- 17 Crm
- 18 Realtime Chat
- 19 Recommendation Engine
- 20 Smart Home
- 21 Coding Platform
- 22 AQL Diagram Tool
- 23 Traveling Salesman
- 24 Moral Philosophy Debates
- API Versioning
- Distributed Sharding
- Feedback Plugins
- Geo
- Gnn
- Image Analysis
- Legal Lora Training
- LLM
- Lora Sync
- Migration
- Nlp
- Performance
- Railway
- Replication
- Rope Visualization
- Sample Product Config
- Security
- Client SDK Overview
- Quickstart
- Sdk Enhancements
- Sdk Implementation Summary
- Test Suite Readme
- Go
- Java
- Javascript
- Php
- Python
- Ruby
- Rust
- Typescript
- 01 Grundlegende Operationen
- 02 AQL Queries
- 03 Graph Daten
- 04 Multimodell Anwendung
- 01 Quickstart Guide
- 02 AQL Referenz Kurzuebersicht
- 03 Datenmodellierung Guide
- 04 Uebungsaufgaben
- 05 Best Practices Guide
- Training Documents
- Training Overview
- 01 Einfuehrung Und Uebersicht
- 02 Datenmodelle Und Architektur
- 03 AQL Abfragesprache
- 04 Installation Und Setup
- 05 Anwendungsbeispiele
- Training Presentations
- Dependencies Readme
- Processmonitor Readme
- Themis.admintools.shared Readme
- Themis.aqlquerybuilder Readme
- Themis.aqlquerybuilder Roadmap
- Themis.auditlogviewer Readme
- Themis.auditlogviewer Roadmap
- Themis.classificationdashboard Readme
- Themis.classificationdashboard Roadmap
- Themis.compliancereports Readme
- Themis.compliancereports Roadmap
- Themis.gisviewer.controlpanel Readme
- Themis.gisviewer.controlpanel Roadmap
- Themis.impactanalysisviewer Readme
- Themis.impactanalysisviewer Roadmap
- Themis.ingestiontool Readme
- Themis.ingestiontool Roadmap
- Themis.keyrotationdashboard Readme
- Themis.keyrotationdashboard Roadmap
- Themis.piimanager Readme
- Themis.piimanager Roadmap
- Themis.retentionmanager Readme
- Themis.retentionmanager Roadmap
- Themis.sagaverifier Readme
- Themis.sagaverifier Roadmap
- Themis.usbadmintool Readme
- Themis.usbadmintool Roadmap
- CI Readme
- CI Roadmap
- Compiler Diagnostics Readme
- Compiler Diagnostics Roadmap
- Completion Readme
- Copilot Ollama Router Readme
- Copilot Ollama Router Roadmap
- Gnn Readme
- Gnn Roadmap
- Rope Visualizer Readme
- Rope Visualizer Roadmap
- Tco Calculator Readme
- Tco Calculator Roadmap
- Tests Readme
- Tests Roadmap
- Themis Config Wx Readme
- Themis Docs Builder Readme
- Wikipedia Ingestion Readme