Skip to content
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

bug: Add diagnostic information on font load error #235

Merged
merged 3 commits into from
Mar 9, 2024

Conversation

shanecelis
Copy link
Contributor

@shanecelis shanecelis commented Feb 21, 2024

I ran into a bug a while back that prevent me from using the plotters library for months. I did finally figure out what was causing it: a file in my font library documented in issue #231. Removing the file fixed my issue, but the opaque problem is sure to ensnare others.

This PR adds a LoadError(Cow<'static, str>) variant to SelectionError enum. It removes Copy because Cow isn't copyable. And now when I run the test suite with the bad file in place, it reports this error, which is far and away more helpful than before.

> RUST_BACKTRACE=1 cargo test --all
...
running 5 tests
test loaders::core_text::test::test_core_text_to_css_font_stretch ... ok
test loaders::core_text::test::test_core_text_to_css_font_weight ... ok
test sources::core_text::test::test_css_to_core_text_font_stretch ... ok
test sources::core_text::test::test_css_to_core_text_font_weight ... ok
test loaders::core_text::test::test_from_core_graphics_font ... FAILED

failures:

---- loaders::core_text::test::test_from_core_graphics_font stdout ----
thread 'loaders::core_text::test::test_from_core_graphics_font' panicked at src/loaders/core_text.rs:940:14:
called `Result::unwrap()` on an `Err` value: LoadError("Parse error on path \"/Users/shane/Library/Fonts/Arial\"")
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1649:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1073:23
   4: font_kit::loaders::core_text::test::test_from_core_graphics_font
             at ./src/loaders/core_text.rs:938:21
   5: font_kit::loaders::core_text::test::test_from_core_graphics_font::{{closure}}
             at ./src/loaders/core_text.rs:937:38
   6: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

failures:
    loaders::core_text::test::test_from_core_graphics_font

test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s

error: test failed, to rerun pass `--lib`

A variant that used String would suffice for this case. I tend to use Cow<'static, str> on errors since many times they can be &'static str.

I ran into a bug a while back that prevent me from using the plotters
library for months. I did finally figure out what was causing it: a file
in my font library documented in issue#231. Removing the file fixed my
issue, but the opaque problem is sure to ensnare others.

This PR adds a `LoadError(Cow<'static, str>)` variant to
`SelectionError` enum. It removes `Copy` because `Cow` isn't copyable.
And now when I run the test suite with the bad file in place, it reports
this error, which is far and away more helpful than before.

```sh
> RUST_BACKTRACE=1 cargo test --all
...
running 5 tests
test loaders::core_text::test::test_core_text_to_css_font_stretch ... ok
test loaders::core_text::test::test_core_text_to_css_font_weight ... ok
test sources::core_text::test::test_css_to_core_text_font_stretch ... ok
test sources::core_text::test::test_css_to_core_text_font_weight ... ok
test loaders::core_text::test::test_from_core_graphics_font ... FAILED

failures:

---- loaders::core_text::test::test_from_core_graphics_font stdout ----
thread 'loaders::core_text::test::test_from_core_graphics_font' panicked at src/loaders/core_text.rs:940:14:
called `Result::unwrap()` on an `Err` value: LoadError("Parse error on path \"/Users/shane/Library/Fonts/Arial\"")
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1649:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1073:23
   4: font_kit::loaders::core_text::test::test_from_core_graphics_font
             at ./src/loaders/core_text.rs:938:21
   5: font_kit::loaders::core_text::test::test_from_core_graphics_font::{{closure}}
             at ./src/loaders/core_text.rs:937:38
   6: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

failures:
    loaders::core_text::test::test_from_core_graphics_font

test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s

error: test failed, to rerun pass `--lib`
```

A variant that used `String` would suffice for this case. I tend to use
`Cow<'static, str>` on errors since many times they can be `&'static
str`.
src/error.rs Outdated Show resolved Hide resolved
Copy link
Member

@jdm jdm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jdm
Copy link
Member

jdm commented Mar 8, 2024

Can you run cargo fmt on these changes as well?

@shanecelis
Copy link
Contributor Author

No problem.

@jdm jdm dismissed mrobinson’s stale review March 9, 2024 02:30

addressed changes.

@jdm jdm added this pull request to the merge queue Mar 9, 2024
Merged via the queue into servo:master with commit 428a2e5 Mar 9, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants