bottlerocket-os/bottlerocket · error · datastore::Error

InvalidKeySnafu

InvalidKeySnafu

Error message

Key name '' has invalid format: strip_prefix of '{prefix}' matches key

What it means

`strip_prefix` refuses to strip a prefix that equals the entire key name, because the result would be an empty (invalid) key. The resulting key is reported with an empty name ('') since no valid remainder exists.

Solutions

  1. Check `prefix == key.name()` before calling strip_prefix and handle that case explicitly (e.g. return an empty/zero-length key)
  2. Strip a strictly shorter prefix ending with the '.' separator
  3. Change the loop to skip keys identical to the prefix

Example fix

// before
let child = key.strip_prefix(prefix)?; // fails when prefix == key
// after
let child = if key.name() == prefix {
    None
} else {
    Some(key.strip_prefix(prefix)?)
};
Defensive patterns

Strategy: try-catch

Validate before calling

if key.name() == prefix { /* handle identity */ }

Try / catch

match key.strip_prefix(&prefix) { Err(e) if e.to_string().contains("matches key") => None, o => Some(o?) }

Prevention

When it happens

Trigger: Calling key.strip_prefix(p) where p.to_string() == key.name() exactly — stripping the full key instead of a proper ancestor prefix.

Common situations: Prefix-based store listing code that iterates prefixes and accidentally includes the queried prefix itself as a key; computing relative keys where caller and callee use the same constant prefix.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of bottlerocket-os/bottlerocket@0be31b34d2 (2026-09-10). Data as JSON: /api/errors/7e8c009bd04d1ade. Report an issue: GitHub.

Appendix: source

Thrown at sources/api/datastore/src/key.rs:108

    }

    /// Removes the given prefix from the key name, returning a new Key.
    ///
    /// This is intended to remove key name segments from the beginning of the name, therefore
    /// this only makes sense for Data keys, not Meta keys.  A Data key will be returned.
    ///
    /// You should not include an ending separator (dot), it will be removed for you.
    ///
    /// If the key name does not begin with the given prefix, the returned key will be
    /// identical.
    ///
    /// Fails if the new key would be invalid, e.g. if the prefix is the entire key.
    pub(super) fn strip_prefix<S>(&self, prefix: S) -> Result<Self>
    where
        S: AsRef<str>,
    {
        let prefix = prefix.as_ref();
        ensure!(
            prefix != self.name,
            error::InvalidKeySnafu {
                name: "",
                msg: format!("strip_prefix of '{prefix}' matches key")
            }
        );

        let strip = prefix.to_string() + ".";

        // Check starts_with so we don't replace in the middle of the string...
        let name = if self.name.starts_with(&strip) {
            self.name.replacen(&strip, "", 1)
        } else {
            self.name.clone()
        };

        Self::new(KeyType::Data, name)
    }

View on GitHub (pinned to 0be31b34d2)