bevyengine/bevy · error · GridPlacementError

Zero is not a valid grid position

Error message

Zero is not a valid grid position

What it means

GridPlacementError::InvalidZeroIndex is the thiserror variant behind every zero-line panic in GridPlacement: the internal try_into_grid_index helper returns it when an i16 grid line of 0 fails NonZero::new. In the public API the constructors/setters immediately .expect() this error away (panicking with 'Invalid start/end value of 0.'), so the Display text 'Zero is not a valid grid position' surfaces whenever the error itself is formatted or propagated — for example in user-written fallible wrappers that match on GridPlacementError, reflection tooling, or logging that reports the cause.

Source

Thrown at crates/bevy_ui/src/ui_node.rs:2250

/// Convert an `i16` to `NonZero<i16>`, fails on `0` and returns the `InvalidZeroIndex` error.
fn try_into_grid_index(index: i16) -> Result<Option<NonZero<i16>>, GridPlacementError> {
    Ok(Some(
        NonZero::<i16>::new(index).ok_or(GridPlacementError::InvalidZeroIndex)?,
    ))
}

/// Convert a `u16` to `NonZero<u16>`, fails on `0` and returns the `InvalidZeroSpan` error.
fn try_into_grid_span(span: u16) -> Result<Option<NonZero<u16>>, GridPlacementError> {
    Ok(Some(
        NonZero::<u16>::new(span).ok_or(GridPlacementError::InvalidZeroSpan)?,
    ))
}

/// Errors that occur when setting constraints for a `GridPlacement`
#[derive(Debug, Eq, PartialEq, Clone, Copy, Error)]
pub enum GridPlacementError {
    #[error("Zero is not a valid grid position")]
    InvalidZeroIndex,
    #[error("Spans cannot be zero length")]
    InvalidZeroSpan,
}

/// The background color of the node
///
/// This serves as the "fill" color.
#[derive(Component, Copy, Clone, Debug, Deref, DerefMut, PartialEq, Reflect)]
#[reflect(Component, Default, Debug, PartialEq, Clone)]
#[cfg_attr(
    feature = "serialize",
    derive(serde::Serialize, serde::Deserialize),
    reflect(Serialize, Deserialize)
)]
pub struct BackgroundColor(pub Color);

impl BackgroundColor {

View on GitHub (pinned to 227d3a6c66)

Solutions

  1. Never pass 0 as a grid line: use 1-based positive lines or negative lines counted from the end.
  2. In fallible wrappers, match GridPlacementError::InvalidZeroIndex and map it to an automatic/None placement instead of propagating.
  3. When reporting, include the field name (start vs end) so callers know which argument was 0.
  4. Use GridPlacement::auto()/start()/end() variants that leave the offending line unset when the value is unknown.

Example fix

// before
fn place(start: i16) -> Result<GridPlacement, GridPlacementError> {
    Ok(GridPlacement::start(start)) // panics on 0 before the Result can help
}

// after
fn place(start: i16) -> Result<GridPlacement, GridPlacementError> {
    if start == 0 { return Err(GridPlacementError::InvalidZeroIndex); }
    Ok(GridPlacement::start(start))
}
Defensive patterns

Strategy: validation

Validate before calling

use std::num::NonZero;

// gate every line before it reaches a GridPlacement API
fn checked_line(line: i16) -> Result<NonZero<i16>, GridPlacementError> {
    NonZero::new(line).ok_or(GridPlacementError::InvalidZeroIndex)
}

Type guard

fn is_valid_grid_line(line: i16) -> bool { line != 0 }

Try / catch

// in fallible wrappers, match the enum and degrade to automatic placement
match checked_line(line) {
    Ok(line) => Ok(GridPlacement::start(line.get())),
    Err(e @ GridPlacementError::InvalidZeroIndex) => {
        warn!("{e}");
        Ok(GridPlacement::auto())
    }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: Passing 0 as start or end to any GridPlacement constructor/setter (which converts this error into the corresponding panic); calling the conversion helpers from code copied into your crate; formatting or matching a GridPlacementError::InvalidZeroIndex produced by a fallible wrapper around GridPlacement.

Common situations: Writing a validate-placement layer that returns Result<GridPlacement, GridPlacementError>; upgrading Bevy versions where placement validation moved from ad-hoc checks to this enum; error reporting pipelines that Display the underlying cause chain of a layout failure.

Related errors


AI-assisted analysis of bevyengine/bevy@227d3a6c66 (2026-08-20). Data as JSON: /api/errors/0be54f196e6940e8. Report an issue: GitHub.