flxzt/rnote · warning · anyhow::Error

Could not parse text as page number

Error message

Could not parse text as page number, '{page}' outside valid range '{page_range:?}'.

What it means

Thrown by `parse_page_text` in `strokecontentpreview.rs` when the user-entered text parses as a valid `usize` but falls outside the 1..=n_pages range of the referenced document. The function wants a 1-indexed page number; out-of-range values are rejected with this error so the preview does not render a nonexistent page.

Solutions

  1. Enter a page number between 1 and the document's total page count.
  2. Clamp the parsed value to 1..=n_pages (or to the nearest bound) instead of erroring.
  3. Update the entry's allowed range/validator whenever the referenced document's page count changes.
  4. Check `n_pages` of the linked stroke content to confirm the intended page exists.

Example fix

// before
Ok(page) => Err(anyhow::anyhow!(
    "Could not parse text as page number, '{page}' outside valid range '{page_range:?}'.",
)),
// after
Ok(page) => {
    let clamped = page.clamp(1, n_pages);
    Ok(clamped - 1)
}
Defensive patterns

Strategy: validation

Validate before calling

let page: usize = text.trim().parse()?;
if page == 0 || page > n_pages { return Err(anyhow!("page {page} outside 1..={n_pages}")); }

Type guard

fn valid_page(text: &str, n_pages: usize) -> Option<usize> {
    text.trim().parse::<usize>().ok().filter(|p| (1..=n_pages).contains(p))
}

Prevention

When it happens

Trigger: In `update_paintable_content`, the user types a page number greater than the document's page count (e.g. 12 in a 5-page document) or 0 into the stroke content preview's page entry, and the text is parsed as a valid usize.

Common situations: Typing 0 or a page beyond the document length; document changed (pages removed) after the entry text was set; pasting a page number from a different document.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of flxzt/rnote@bbc5354502 (2026-09-08). Data as JSON: /api/errors/a7209f808b6de227. Report an issue: GitHub.

Appendix: source

Thrown at crates/rnote-ui/src/strokecontentpreview.rs:216

            match parse_page_text(&self.page_entry.text(), n_pages) {
                Ok(page) if page == current_page => {
                    // Don't update entry if it is already the current page
                }
                Ok(_) | Err(_) => {
                    // user facing page number is 1 indexed
                    self.page_entry.set_text(&(current_page + 1).to_string());
                }
            }
            self.n_pages_button.set_label(&n_pages.to_string());
        }
    }

    fn parse_page_text(text: &str, n_pages: usize) -> anyhow::Result<usize> {
        // user facing page number is 1 indexed
        let page_range = 1..=n_pages;
        match text.parse::<usize>() {
            Ok(page) if page_range.contains(&page) => Ok(page - 1),
            Ok(page) => Err(anyhow::anyhow!(
                "Could not parse text as page number, '{page}' outside valid range '{page_range:?}'.",
            )),
            Err(e) => Err(anyhow::anyhow!(
                "Could not parse text as page number, parsing error: {e:?}"
            )),
        }
    }
}

glib::wrapper! {
    pub(crate) struct RnStrokeContentPreview(ObjectSubclass<imp::RnStrokeContentPreview>)
        @extends Widget,
        @implements gtk4::Accessible, gtk4::Buildable, gtk4::ConstraintTarget;
}

impl Default for RnStrokeContentPreview {
    fn default() -> Self {
        Self::new()

View on GitHub (pinned to bbc5354502)