Lightpanda: the headless browser designed for AI and automation
Improve screenshot error mapping
Karl Seguinb6960ab5cabc
diff --git a/src/browser/screenshot.zig b/src/browser/screenshot.zigindex 182105117..394b4c3f5 100644--- a/src/browser/screenshot.zig+++ b/src/browser/screenshot.zig@@ -98,11 +98,20 @@ pub const Prepared = struct {.flags = flags,}, &content_height, &sink, Sink.write);- if (rc == 0) {+ if (rc == RC_OK) {return content_height;}- log.err(.browser, "screenshot render", .{ .rc = rc });+ // Any non-zero rc means nothing usable was written, so this has to+ // fail rather than hand back a truncated PNG. WriteFailed is the only+ // error jsonStringify's signature can carry, hence the log line.+ log.err(.browser, "screenshot render", .{ .reason = switch (rc) {+ RC_WRITE_REFUSED => "write refused",+ RC_INVALID => "invalid options",+ RC_NO_RASTER => "raster allocation failed",+ RC_ENCODE_FAILED => "png encoding failed",+ else => "unknown",+ }, .rc = rc });return error.WriteFailed;}@@ -177,6 +186,12 @@ const BLOCK_TIGHT: u8 = 1 << 0;const RENDER_MEASURE_ONLY: u32 = 1 << 0;+const RC_OK: i32 = 0;+const RC_WRITE_REFUSED: i32 = 1;+const RC_INVALID: i32 = 2;+const RC_NO_RASTER: i32 = 3;+const RC_ENCODE_FAILED: i32 = 4;+const LINK_COLOR: u32 = 0x1a0dab;const MUTED_COLOR: u32 = 0x6b6b6b;@@ -699,6 +714,22 @@ test "browser.screenshot: raster is bounded" {try testing.expectEqual(16384, std.mem.readInt(u32, aw.written()[20..24], .big));}+test "browser.screenshot: a refused write fails the capture" {+ defer testing.test_session.closeAllPages();+ testing.silenceLog(&.{.browser});+ const frame = try testing.createFrame();+ frame.url = "http://localhost/";+ const doc = frame.window._document;+ const div = try doc.createElement("div", null, frame);+ try Frame.parse.htmlAsChildren(frame, div.asNode(), "<p>hello</p>");++ // Far too small for the PNG, so the sink refuses partway through. That+ // has to surface as an error and not as a truncated image.+ var buf: [64]u8 = undefined;+ var w = std.Io.Writer.fixed(&buf);+ try testing.expectError(error.WriteFailed, png(div.asNode(), .{ .width = 300 }, &w, frame));+}+test "browser.screenshot: json streams base64" {defer testing.test_session.closeAllPages();const frame = try testing.createFrame();diff --git a/src/rust/render/lib.rs b/src/rust/render/lib.rsindex d9faf34e0..97256fe38 100644--- a/src/rust/render/lib.rs+++ b/src/rust/render/lib.rs@@ -104,9 +104,18 @@ pub struct LpRenderOpts {pub type LpWriteFn = extern "C" fn(ctx: *mut c_void, data: *const u8, len: usize) -> bool;+/// Return codes. Anything but OK means nothing usable reached `write`, so a+/// caller must not treat what it received as a PNG.+pub const RC_OK: i32 = 0;+pub const RC_WRITE_REFUSED: i32 = 1;+pub const RC_INVALID: i32 = 2;+/// The pixmap could not be allocated, even after the MAX_RASTER_* clamps.+pub const RC_NO_RASTER: i32 = 3;+pub const RC_ENCODE_FAILED: i32 = 4;+/// Renders `blocks` to PNG, streaming the encoded bytes to `write`.-/// `content_height` receives the full-page height in CSS px. Returns 0 on-/// success, 1 if `write` refused data, 2 on invalid input.+/// `content_height` receives the full-page height in CSS px, and is filled in+/// even when rendering fails. Returns one of the RC_* codes above.#[no_mangle]pub unsafe extern "C" fn lp_render_png(blocks: *const LpBlock,@@ -117,7 +126,7 @@ pub unsafe extern "C" fn lp_render_png(write: LpWriteFn,) -> i32 {if opts.width == 0 || !(opts.scale > 0.0) || opts.scale > 8.0 {- return 2;+ return RC_INVALID;}let blocks = if blocks_len == 0 {&[][..]@@ -135,7 +144,7 @@ pub unsafe extern "C" fn lp_render_png(for s in spans {let bytes = std::slice::from_raw_parts(s.text, s.len);let Ok(text) = std::str::from_utf8(bytes) else {- return 2;+ return RC_INVALID;};out_spans.push(Span { text, flags: s.flags, color: s.color });}@@ -159,14 +168,15 @@ pub unsafe extern "C" fn lp_render_png(.unwrap_or_else(|e| e.into_inner());let mut sink = Sink { ctx, write, failed: false };- let h = r.render(&doc, opts, &mut sink);+ let (h, rc) = r.render(&doc, opts, &mut sink);if !content_height.is_null() {*content_height = h;}+ // A refused write surfaces as an encode failure too; report the cause.if sink.failed {- 1+ RC_WRITE_REFUSED} else {- 0+ rc}}@@ -299,8 +309,9 @@ impl Renderer {fams[0].0}- /// Returns the content height in CSS px.- fn render(&mut self, blocks: &[Block], opts: LpRenderOpts, sink: &mut Sink) -> u32 {+ /// Returns the content height in CSS px, and RC_OK only if a complete+ /// PNG reached `sink`.+ fn render(&mut self, blocks: &[Block], opts: LpRenderOpts, sink: &mut Sink) -> (u32, i32) {let scale = opts.scale;let content_w = (opts.width as f32 - 2.0 * PAGE_MARGIN).max(1.0);let mut y = PAGE_MARGIN;@@ -417,7 +428,7 @@ impl Renderer {y += PAGE_MARGIN;let content_h = y.ceil().max(1.0) as u32;if opts.flags & RENDER_MEASURE_ONLY != 0 {- return content_h;+ return (content_h, RC_OK);}// Rasterize the requested strip (viewport or full page), then crop.@@ -433,7 +444,7 @@ impl Renderer {.min((MAX_RASTER_PIXELS / pw).max(1));let (pw, ph) = (pw as u32, ph as u32);let Some(mut pixmap) = Pixmap::new(pw, ph) else {- return content_h;+ return (content_h, RC_NO_RASTER);};pixmap.fill(Color::WHITE);@@ -508,11 +519,12 @@ impl Renderer {// base64'd over CDP, so size matters.enc.set_deflate_compression(png::DeflateCompression::Level(2));enc.set_filter(png::Filter::Adaptive);- if let Ok(mut w) = enc.write_header() {- let _ = w.write_image_data(view.data());- let _ = w.finish();- }- content_h+ let encoded = (|| -> Result<(), png::EncodingError> {+ let mut w = enc.write_header()?;+ w.write_image_data(view.data())?;+ w.finish()+ })();+ (content_h, if encoded.is_ok() { RC_OK } else { RC_ENCODE_FAILED })}fn draw_layout(&mut self, pixmap: &mut Pixmap, layout: &Layout<Rgb>, ox: f32, oy: f32) {