{"record":{"id":"6e20990485b497c0","repo":"flxzt/rnote","slug":"finishing-piet-context-failed-err-e-6e2099","errorCode":null,"errorMessage":"finishing piet context failed, Err: {e:?}","messagePattern":"finishing piet context failed, Err: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rnote-engine/src/svg.rs","lineNumber":170,"sourceCode":"            bounds,\n        })\n    }\n\n    /// Generate an Svg with piet, using the `piet_cairo` backend and cairo's SvgSurface.\n    ///\n    /// This might be preferable to the `piet_svg` backend, because especially text alignment and sizes can be different\n    /// with it.\n    pub fn gen_with_piet_cairo_backend<F>(draw_func: F, bounds: Aabb) -> anyhow::Result<Self>\n    where\n        F: FnOnce(&mut piet_cairo::CairoRenderContext) -> anyhow::Result<()>,\n    {\n        let cairo_draw_fn = |cairo_cx: &cairo::Context| {\n            let mut piet_cx = piet_cairo::CairoRenderContext::new(cairo_cx);\n            // Apply the draw function\n            draw_func(&mut piet_cx)?;\n            piet_cx\n                .finish()\n                .map_err(|e| anyhow::anyhow!(\"finishing piet context failed, Err: {e:?}\"))\n        };\n\n        Self::gen_with_cairo(cairo_draw_fn, bounds)\n    }\n\n    pub fn draw_to_cairo(&self, cx: &cairo::Context) -> anyhow::Result<()> {\n        let svg_data = rnote_compose::utils::wrap_svg_root(\n            self.svg_data.as_str(),\n            Some(self.bounds),\n            Some(self.bounds),\n            false,\n        );\n        let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from(svg_data.as_bytes()));\n        let handle = rsvg::Loader::new()\n            .with_unlimited_size(true)\n            .read_stream(&stream, None::<&gio::File>, None::<&gio::Cancellable>)\n            .context(\"reading stream to rsvg loader failed.\")?;\n        let renderer = rsvg::CairoRenderer::new(&handle);","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/flxzt/rnote/blob/bbc5354502ba2fc83eec2670b535348825e679a6/crates/rnote-engine/src/svg.rs#L152-L188","documentation":"Thrown by Svg::gen_with_piet_cairo_backend when piet_cx.finish() on the piet_cairo CairoRenderContext returns a piet::Error. piet defers some rendering work (text layout buffers, etc.) to finish(); failures there mean piet could not complete the render on the underlying cairo context.","triggerScenarios":"draw_func performs piet operations that fail on flush — e.g. invalid text layouts, unsupported text features, or an error already recorded on the cairo context during drawing.","commonSituations":"Rendering stroke text with fonts unavailable on the system, drawing piet primitives that map to failing cairo ops, or cairo context already in an error state when the piet context finishes.","solutions":["Check font availability and validity for piet text drawing (TextLayout/text rendering is a common finish() failure source)","Verify the cairo context is healthy before wrapping it in a CairoRenderContext","Inspect the piet::Error variant (e.g. BackendError wrapping cairo) for the root cause","Simplify draw_func to isolate which piet call triggers the failure"],"exampleFix":"// before\npiet_cx.finish().map_err(|e| anyhow::anyhow!(\"finishing piet context failed, Err: {e:?}\"))\n// after\npiet_cx.finish().map_err(|e| anyhow::anyhow!(\"finishing piet context failed: {e:#}\"))?;\nOk(())","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match piet_cx.finish() {\n    Ok(()) => Ok(()),\n    Err(piet::Error::BackendError(e)) => Err(anyhow::anyhow!(\"piet backend (cairo) failed: {e:?}\")),\n    Err(e) => Err(anyhow::anyhow!(\"piet finish failed: {e}\")),\n}","preventionTips":["Verify fonts used in text drawing are available on the system","Ensure the cairo context is error-free before wrapping in CairoRenderContext","Call finish() exactly once per piet context"],"tags":["piet","cairo","rendering"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bbc5354502ba2fc83eec2670b535348825e679a6","analyzedAt":"2026-09-08T13:20:33.747Z","contentChangedAt":"2026-09-08T13:20:33.747Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}