Repository navigation
Make nodes report errors instead of panicking or failing silently #4837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1156d04
77ff5ed
f0a0092
b163709
1396317
3235cdd
ad61525
34719b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -189,6 +189,10 @@ impl Footprint { | |||||
| }; | ||||||
|
|
||||||
| pub fn viewport_bounds_in_local_space(&self) -> AxisAlignedBbox { | ||||||
| if self.transform.matrix2.determinant() == 0. { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: This guard only handles an exactly zero determinant. NaN or subnormal determinants still reach Prompt for AI agents
Suggested change
|
||||||
| return AxisAlignedBbox::ZERO; | ||||||
| } | ||||||
|
|
||||||
| let inverse = self.transform.inverse(); | ||||||
| let res = self.resolution.as_dvec2(); | ||||||
| let c0 = inverse.transform_point2(DVec2::ZERO); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,6 +130,14 @@ impl<P: core_types::CacheHash + Pixel> core_types::CacheHash for Image<P> { | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum ImageError { | ||
| #[error("The {width}×{height} px image is too large")] | ||
| TooLarge { width: u32, height: u32 }, | ||
| #[error("The file is not an image in a supported format, or it is damaged")] | ||
| Undecodable(#[source] ::image::ImageError), | ||
| } | ||
|
|
||
| impl<P: Pixel> Image<P> { | ||
| pub fn new(width: u32, height: u32, color: P) -> Self { | ||
| Self { | ||
|
|
@@ -139,6 +147,23 @@ impl<P: Pixel> Image<P> { | |
| base64_string: None, | ||
| } | ||
| } | ||
|
|
||
| /// An image filled with `color`, failing where the pixel count overflows the `u32` pixel indices or can't be allocated. | ||
| pub fn try_new(width: u32, height: u32, color: P) -> Result<Self, ImageError> { | ||
| let too_large = || ImageError::TooLarge { width, height }; | ||
| let count = width.checked_mul(height).ok_or_else(too_large)? as usize; | ||
|
|
||
| let mut data = Vec::new(); | ||
| data.try_reserve_exact(count).map_err(|_| too_large())?; | ||
| data.resize(count, color); | ||
|
|
||
| Ok(Self { | ||
| width, | ||
| height, | ||
| data, | ||
| base64_string: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Image<Color> { | ||
|
|
@@ -153,8 +178,8 @@ impl Image<Color> { | |
| } | ||
| } | ||
|
|
||
| /// Decodes an image file of any supported format. | ||
| pub fn from_encoded(data: &[u8]) -> Option<Self> { | ||
| /// Decodes an image file of any supported format, or gives the reason it can't be. | ||
| pub fn from_encoded(data: &[u8]) -> Result<Self, ImageError> { | ||
| let image = decode(data)?; | ||
|
|
||
| // Float samples hold linear light, as in HDR and EXR files, while integer samples are gamma encoded | ||
|
|
@@ -178,7 +203,7 @@ impl Image<Color> { | |
| }) | ||
| .collect(); | ||
|
|
||
| Some(Image { | ||
| Ok(Image { | ||
| width: image.width(), | ||
| height: image.height(), | ||
| data, | ||
|
|
@@ -188,7 +213,7 @@ impl Image<Color> { | |
|
|
||
| /// The pixel size of an image file, if it decodes in full as [`Image::from_encoded`] needs it to. | ||
| pub fn encoded_size(data: &[u8]) -> Option<(u32, u32)> { | ||
| decode(data).map(|image| (image.width(), image.height())) | ||
| decode(data).ok().map(|image| (image.width(), image.height())) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents |
||
| } | ||
|
|
||
| pub fn to_png(&self) -> Vec<u8> { | ||
|
|
@@ -202,9 +227,17 @@ impl Image<Color> { | |
| } | ||
|
|
||
| /// A TGA file has no signature to recognize it by, so a file of no recognized format is tried as one. | ||
| fn decode(data: &[u8]) -> Option<::image::DynamicImage> { | ||
| fn decode(data: &[u8]) -> Result<::image::DynamicImage, ImageError> { | ||
| let format = ::image::guess_format(data).unwrap_or(::image::ImageFormat::Tga); | ||
| ::image::load_from_memory_with_format(data, format).ok() | ||
| ::image::load_from_memory_with_format(data, format).map_err(|error| { | ||
| // The memory limit error carries no size, which the header gives | ||
| if let ::image::ImageError::Limits(_) = error | ||
| && let Ok((width, height)) = ::image::ImageReader::with_format(std::io::Cursor::new(data), format).into_dimensions() | ||
| { | ||
| return ImageError::TooLarge { width, height }; | ||
| } | ||
| ImageError::Undecodable(error) | ||
| }) | ||
| } | ||
|
|
||
| use super::*; | ||
|
|
@@ -346,12 +379,37 @@ mod test { | |
| let mut encoded = Vec::new(); | ||
| pixels.write_to(&mut std::io::Cursor::new(&mut encoded), format).unwrap(); | ||
|
|
||
| let image = Image::from_encoded(&encoded).unwrap_or_else(|| panic!("{format:?} should decode")); | ||
| let image = Image::from_encoded(&encoded).unwrap_or_else(|error| panic!("{format:?} should decode: {error}")); | ||
| assert_eq!(Image::encoded_size(&encoded), Some((2, 1)), "{format:?}"); | ||
| assert_eq!(image.to_flat_u8().0, pixels.as_raw().as_slice(), "{format:?}"); | ||
| } | ||
|
|
||
| assert!(Image::from_encoded(b"not an image").is_none()); | ||
| assert!(Image::from_encoded(b"not an image").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_file_that_does_not_decode_gives_its_reason() { | ||
| use super::*; | ||
|
|
||
| assert!(matches!(Image::from_encoded(b"not an image"), Err(ImageError::Undecodable(_)))); | ||
|
|
||
| // A BMP header for a 32-bit image of 30000×30000 pixels, which is far beyond the decoder's memory limit | ||
| let mut file = b"BM".to_vec(); | ||
| for field in [54_u32, 0, 54, 40, 30000, 30000] { | ||
| file.extend(field.to_le_bytes()); | ||
| } | ||
| file.extend([1, 0, 32, 0]); | ||
| file.extend([0; 24]); | ||
| let error = Image::from_encoded(&file).unwrap_err(); | ||
| assert!(matches!(error, ImageError::TooLarge { width: 30000, height: 30000 }), "{error:?}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_image_too_large_for_its_pixel_indices_is_not_made() { | ||
| use super::*; | ||
|
|
||
| assert!(matches!(Image::try_new(65536, 65536, Color::BLACK), Err(ImageError::TooLarge { width: 65536, height: 65536 }))); | ||
| assert_eq!(Image::try_new(3, 2, Color::BLACK).unwrap().data, vec![Color::BLACK; 6]); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -371,7 +429,7 @@ mod test { | |
| file.extend([0, 255, 0, 0]); | ||
|
|
||
| assert!(::image::load_from_memory_with_format(&file, ::image::ImageFormat::Tga).is_ok()); | ||
| assert!(Image::from_encoded(&file).is_none()); | ||
| assert!(Image::from_encoded(&file).is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: This discards every
TextToVectorErrorand silently renders no overlay text. The bounding box still drives layout, so labels can disappear while their background remains; log the error or propagate it instead of converting it to an empty list.Prompt for AI agents