{"record":{"id":"f1818d730ddacee3","repo":"bevyengine/bevy","slug":"aspectratio-error-width-or-height-is-zero","errorCode":null,"errorMessage":"AspectRatio error: width or height is zero","messagePattern":"AspectRatio error: width or height is zero","errorType":"exception","errorClass":"AspectRatioError","httpStatus":null,"severity":"error","filePath":"crates/bevy_math/src/aspect_ratio.rs","lineNumber":95,"sourceCode":"    pub const fn is_square(&self) -> bool {\n        self.0 == 1.0\n    }\n}\n\nimpl TryFrom<Vec2> for AspectRatio {\n    type Error = AspectRatioError;\n\n    #[inline]\n    fn try_from(value: Vec2) -> Result<Self, Self::Error> {\n        Self::try_new(value.x, value.y)\n    }\n}\n\n/// An Error type for when [`AspectRatio`](`super::AspectRatio`) is provided invalid width or height values\n#[derive(Error, Debug, PartialEq, Eq, Clone, Copy)]\npub enum AspectRatioError {\n    /// Error due to width or height having zero as a value.\n    #[error(\"AspectRatio error: width or height is zero\")]\n    Zero,\n    /// Error due towidth or height being infinite.\n    #[error(\"AspectRatio error: width or height is infinite\")]\n    Infinite,\n    /// Error due to width or height being Not a Number (NaN).\n    #[error(\"AspectRatio error: width or height is NaN\")]\n    NaN,\n}\n","sourceCodeStart":77,"sourceCodeEnd":104,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_math/src/aspect_ratio.rs#L77-L104","documentation":"AspectRatio::try_new / try_from_pixels / TryFrom<Vec2> reject inputs where width or height is exactly 0.0, because the resulting ratio would be zero or require division by zero (crates/bevy_math/src/aspect_ratio.rs:38). try_from_pixels(u32, u32) with either dimension 0 is the most common producer.","triggerScenarios":"Constructing an AspectRatio from a Vec2::ZERO, from pixel dimensions where either is 0 (minimized window, unresized surface), or from an unmeasured layout value that defaulted to zero.","commonSituations":"Minimized windows on first frames; cameras with unset or zero-sized targets; uninitialized dimension fields; downstream panics such as bevy_light's ClusterConfig::FixedZ expect on try_from_pixels.","solutions":["Guard the call: only construct the AspectRatio when both dimensions are greater than 0.","Fall back to a known ratio (e.g. AspectRatio::SIXTEEN_NINE) when measurements are zero-sized.","Skip work that depends on aspect ratio for zero-sized render targets."],"exampleFix":"// before\nlet ar = AspectRatio::try_from_pixels(size.x, size.y).unwrap(); // panics when minimized\n\n// after\nlet ar = match AspectRatio::try_from_pixels(size.x, size.y) {\n    Ok(ar) => ar,\n    Err(_) => AspectRatio::SIXTEEN_NINE,\n};","handlingStrategy":"validation","validationCode":"use bevy_math::AspectRatio;\n\nfn valid_dimensions(w: f32, h: f32) -> bool {\n    w != 0.0 && h != 0.0\n}\n\nlet ar = if valid_dimensions(w, h) {\n    AspectRatio::try_new(w, h).ok()\n} else {\n    None // skip or fall back to a known ratio\n};","typeGuard":null,"tryCatchPattern":"match AspectRatio::try_from_pixels(size.x, size.y) {\n    Ok(ar) => { /* use ar.ratio() */ }\n    Err(AspectRatioError::Zero) => { /* skip zero-sized frames */ }\n    Err(e) => warn!(\"bad aspect ratio inputs: {e}\"),\n}","preventionTips":["Never construct aspect ratios from unmeasured or zero-sized dimensions.","Gate render-target-dependent math on width > 0 && height > 0.","Use a known fallback ratio for degenerate frames instead of unwrapping."],"tags":["bevy","math","aspect-ratio","validation","window-size"],"backgroundTag":"zero-dimension-value","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}