{"record":{"id":"0be54f196e6940e8","repo":"bevyengine/bevy","slug":"zero-is-not-a-valid-grid-position","errorCode":null,"errorMessage":"Zero is not a valid grid position","messagePattern":"Zero is not a valid grid position","errorType":"exception","errorClass":"GridPlacementError","httpStatus":null,"severity":"error","filePath":"crates/bevy_ui/src/ui_node.rs","lineNumber":2250,"sourceCode":"\n/// Convert an `i16` to `NonZero<i16>`, fails on `0` and returns the `InvalidZeroIndex` error.\nfn try_into_grid_index(index: i16) -> Result<Option<NonZero<i16>>, GridPlacementError> {\n    Ok(Some(\n        NonZero::<i16>::new(index).ok_or(GridPlacementError::InvalidZeroIndex)?,\n    ))\n}\n\n/// Convert a `u16` to `NonZero<u16>`, fails on `0` and returns the `InvalidZeroSpan` error.\nfn try_into_grid_span(span: u16) -> Result<Option<NonZero<u16>>, GridPlacementError> {\n    Ok(Some(\n        NonZero::<u16>::new(span).ok_or(GridPlacementError::InvalidZeroSpan)?,\n    ))\n}\n\n/// Errors that occur when setting constraints for a `GridPlacement`\n#[derive(Debug, Eq, PartialEq, Clone, Copy, Error)]\npub enum GridPlacementError {\n    #[error(\"Zero is not a valid grid position\")]\n    InvalidZeroIndex,\n    #[error(\"Spans cannot be zero length\")]\n    InvalidZeroSpan,\n}\n\n/// The background color of the node\n///\n/// This serves as the \"fill\" color.\n#[derive(Component, Copy, Clone, Debug, Deref, DerefMut, PartialEq, Reflect)]\n#[reflect(Component, Default, Debug, PartialEq, Clone)]\n#[cfg_attr(\n    feature = \"serialize\",\n    derive(serde::Serialize, serde::Deserialize),\n    reflect(Serialize, Deserialize)\n)]\npub struct BackgroundColor(pub Color);\n\nimpl BackgroundColor {","sourceCodeStart":2232,"sourceCodeEnd":2268,"githubUrl":"https://github.com/bevyengine/bevy/blob/227d3a6c661b3bdf3020d3e8290b5a6ffd0226f7/crates/bevy_ui/src/ui_node.rs#L2232-L2268","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Never pass 0 as a grid line: use 1-based positive lines or negative lines counted from the end.","In fallible wrappers, match GridPlacementError::InvalidZeroIndex and map it to an automatic/None placement instead of propagating.","When reporting, include the field name (start vs end) so callers know which argument was 0.","Use GridPlacement::auto()/start()/end() variants that leave the offending line unset when the value is unknown."],"exampleFix":"// before\nfn place(start: i16) -> Result<GridPlacement, GridPlacementError> {\n    Ok(GridPlacement::start(start)) // panics on 0 before the Result can help\n}\n\n// after\nfn place(start: i16) -> Result<GridPlacement, GridPlacementError> {\n    if start == 0 { return Err(GridPlacementError::InvalidZeroIndex); }\n    Ok(GridPlacement::start(start))\n}","handlingStrategy":"validation","validationCode":"use std::num::NonZero;\n\n// gate every line before it reaches a GridPlacement API\nfn checked_line(line: i16) -> Result<NonZero<i16>, GridPlacementError> {\n    NonZero::new(line).ok_or(GridPlacementError::InvalidZeroIndex)\n}","typeGuard":"fn is_valid_grid_line(line: i16) -> bool { line != 0 }","tryCatchPattern":"// in fallible wrappers, match the enum and degrade to automatic placement\nmatch checked_line(line) {\n    Ok(line) => Ok(GridPlacement::start(line.get())),\n    Err(e @ GridPlacementError::InvalidZeroIndex) => {\n        warn!(\"{e}\");\n        Ok(GridPlacement::auto())\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Normalize domain indices to NonZero<i16> as soon as they enter layout code.","Keep a single index-to-grid-line conversion (+1) helper shared by all call sites.","Report which field (start/end) failed when surfacing this error upstream."],"tags":["rust","bevy","bevy-ui","grid","error-enum","validation"],"backgroundTag":"grid-placement-zero-value","analyzedSha":"227d3a6c661b3bdf3020d3e8290b5a6ffd0226f7","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}