[librsvg: 6/32] Map image loading errors from Cairo to LoadingError




commit f24e85ac75d78f12c54b7270daeb6a8cd0d65ea4
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Nov 25 12:13:51 2020 -0600

    Map image loading errors from Cairo to LoadingError
    
    This really should be done in shared_surface.rs, so that it does not
    expose Cairo error codes to the outside - for when we move to a
    non-cairo-based representation for images.

 src/document.rs | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
---
diff --git a/src/document.rs b/src/document.rs
index a2ba4bf2..1d47c711 100644
--- a/src/document.rs
+++ b/src/document.rs
@@ -237,11 +237,31 @@ fn load_image(
         None
     };
 
-    let surface = SharedImageSurface::from_pixbuf(&pixbuf, bytes, content_type.as_deref())?;
+    let surface = SharedImageSurface::from_pixbuf(&pixbuf, bytes, content_type.as_deref())
+        .map_err(|e| image_loading_error_from_cairo(e, aurl))?;
 
     Ok(surface)
 }
 
+fn image_loading_error_from_cairo(status: cairo::Status, aurl: &AllowedUrl) -> LoadingError {
+    let human_readable_url = if aurl.scheme() == "data" {
+        // avoid printing a huge data: URL for image data
+        "data URL"
+    } else {
+        aurl.as_ref()
+    };
+
+    match status {
+        cairo::Status::NoMemory => {
+            LoadingError::OutOfMemory(format!("loading image: {}", human_readable_url))
+        }
+        cairo::Status::InvalidSize => {
+            LoadingError::LimitExceeded(format!("image too big: {}", human_readable_url))
+        }
+        _ => LoadingError::Unknown,
+    }
+}
+
 pub struct AcquiredNode {
     stack: Option<Rc<RefCell<NodeStack>>>,
     node: Node,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]