influxdata/influxdb · error · NamespaceNameError

namespace name ' ' contains invalid character, character…

Error message

namespace name '{}' contains invalid character, character number {} is not allowed

What it means

NamespaceNameError::BadChars is returned when a namespace name contains a character that is not permitted. The message names the offending name and the byte/character offset (bad_char_offset) of the first disallowed character. The library validates the character set to keep namespace names safe as identifiers and URL/path components.

Solutions

  1. Use the reported character offset to locate and remove/replace the invalid character.
  2. Restrict names to the allowed set (typically alphanumeric, '-', '_').
  3. Sanitize user-supplied names with a filter that strips or transliterates disallowed characters before constructing the namespace.
  4. If the original name must be preserved, store it elsewhere and use a sanitized, valid namespace name.

Example fix

// before
let ns = NamespaceName::new("my database")?; // invalid space at offset 2
// after
let ns = NamespaceName::new("my-database")?;
Defensive patterns

Strategy: validation

Validate before calling

fn sanitize_namespace_name(s: &str) -> String {
    s.chars()
        .map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '-' })
        .collect()
}

Try / catch

match NamespaceName::new(user_name) {
    Ok(ns) => use(ns),
    Err(NamespaceNameError::BadChars { name, bad_char_offset }) => {
        log::error!("invalid char at offset {bad_char_offset} in '{name}'");
        NamespaceName::new(&sanitize_namespace_name(user_name))
    }
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: Calling NamespaceName::new (or try_from(&str)) with a name containing characters outside the allowed set, e.g. spaces, '/', ':', '!', or other punctuation; the offset points at the first such character.

Common situations: Users passing human-readable names like 'my database' or paths like 'prod/eu-west' as database names; copy-pasted names with trailing whitespace or unicode characters; v1 write paths mapping bucket names with slashes.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


AI-assisted analysis of influxdata/influxdb@06200ef96b (2026-09-19). Data as JSON: /api/errors/ca26dc0ac8a967c2. Report an issue: GitHub.

Appendix: source

Thrown at core/data_types/src/namespace_name.rs:48

/// [`NamespaceName`] name validation errors.
#[derive(Debug, Error)]
pub enum NamespaceNameError {
    /// The provided namespace name does not fall within the valid length of a
    /// namespace.
    #[error(
        "namespace name {} length must be between {} and {} characters",
        name,
        LENGTH_CONSTRAINT.start(),
        LENGTH_CONSTRAINT.end()
    )]
    LengthConstraint {
        /// The user-provided namespace that failed validation.
        name: String,
    },

    /// The provided namespace name contains an unacceptable character.
    #[error(
        "namespace name '{}' contains invalid character, character number {} \
        is not allowed",
        name,
        bad_char_offset
    )]
    BadChars {
        /// The zero-indexed (multi-byte) character position that failed
        /// validation.
        bad_char_offset: usize,
        /// The user-provided namespace that failed validation.
        name: String,
    },
}

/// A correctly formed namespace name.
///
/// Using this wrapper type allows the consuming code to enforce the invariant
/// that only valid names are provided.

View on GitHub (pinned to 06200ef96b)