nautechsystems/nautilus_trader · error
invalid `CacheConfig`
Error message
invalid `CacheConfig`
What it means
Cache::new unwraps Cache::try_new and panics with this message when the provided CacheConfig fails validation. The library requires tick and bar cache capacities to be within [1, 1_000_000]; any config outside that range (or otherwise rejected by try_new) makes construction a hard panic instead of returning a Result.
Source
Thrown at crates/common/src/cache/mod.rs:2273
Self::new(Some(CacheConfig::default()), None)
}
}
impl Cache {
/// Creates a new [`Cache`] instance with optional configuration and database adapter.
#[must_use]
/// # Note
///
/// Uses provided `CacheConfig` or defaults, and optional `CacheDatabaseAdapter` for persistence.
///
/// # Panics
///
/// Panics if the cache config has a tick or bar capacity outside `[1, 1_000_000]`.
pub fn new(
config: Option<CacheConfig>,
database: Option<Box<dyn CacheDatabaseAdapter>>,
) -> Self {
Self::try_new(config, database).expect("invalid `CacheConfig`")
}
/// Creates a new [`Cache`] instance with optional configuration and database adapter.
///
/// # Errors
///
/// Returns a [`crate::config::ConfigError`] if the cache configuration is invalid.
pub fn try_new(
config: Option<CacheConfig>,
database: Option<Box<dyn CacheDatabaseAdapter>>,
) -> crate::config::ConfigResult<Self> {
let config = config.unwrap_or_default();
config.validate()?;
Ok(Self {
config,
index: CacheIndex::default(),
database,View on GitHub (pinned to 18893faf8b)
Solutions
- Validate the CacheConfig before passing it: ensure tick_capacity and bar_capacity are within [1, 1_000_000]
- Use Cache::try_new instead and handle the Err to get the real validation message
- Clamp or sanitize values read from env/config files (e.g. max(1).min(1_000_000))
- Check the log/panic payload from try_new for which specific field was rejected
Example fix
// before
let cache = Cache::new(Some(config), database);
// after
let cache = Cache::try_new(Some(config), database).expect("valid CacheConfig"); Defensive patterns
Strategy: validation
Validate before calling
fn is_valid_cache_config(cfg: &CacheConfig) -> bool {
(1..=1_000_000).contains(&cfg.tick_capacity)
&& (1..=1_000_000).contains(&cfg.bar_capacity)
} Try / catch
// Rust panics cannot be caught; use the fallible API instead
let cache = Cache::try_new(config, database)
.expect("cache config rejected — check tick/bar capacities in [1, 1_000_000]"); Prevention
- Always construct CacheConfig through its builder/validated constructor
- Clamp capacities read from env vars or config files to [1, 1_000_000]
- Prefer Cache::try_new in application code to get a real error
- Add a startup assertion that logs the config before building the Cache
When it happens
Trigger: Calling Cache::new(Some(config), ...) where config has tick_capacity or bar_capacity equal to 0, greater than 1_000_000, or another value rejected by CacheConfig validation inside try_new.
Common situations: Loading cache capacities from environment variables or config files without clamping; a default of 0 for 'unbounded'; copy-pasting a config from another system; hand-rolled struct construction bypassing CacheConfig builders.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
- Order {client_order_id} not found
- Position {position_id} not found
- Order for {} not found to determine position ID
- Order {client_order_id} not found in cache.
- venue-leg quantity calculation validated cumulative fills
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/3a317c6787c51dfa.
Report an issue: GitHub.