rune-rs / rune

An embeddable dynamic programming language for Rust.
https://rune-rs.github.io
Apache License 2.0
1.74k stars 89 forks source link

More descriptive errors (file and line, what it was trying to do) #740

Closed VorpalBlade closed 3 months ago

VorpalBlade commented 3 months ago

Consider this rune example (involving some custom types):

    let sysinfo = sysinfo::SysInfo::new();
    println!("1: Configuring for host {} (distro: {})", sysinfo.host_name()?, sysinfo.os_id());
    println!("2: Configuring for host {} (distro: {})", sysinfo.host_name(), sysinfo.os_id());

sysinfo.host_name returns a Option<String>. The first print works, the second doesn't, since Option cannot be formatted like this.

The issue however is that of the error message:

1: Configuring for host athena (distro: arch)
Error: Got error while executing phase phase_system_discovery

Caused by:
    Missing function with hash `0x811b62957ea9d9f9`

I don't see a way to extract a more useful error message from this. I would like one or more of:

Anything to make this error more human readable.

Even the at or chain members on VmError contain nothing useful when I print them (and VmErrorAt appears to be a private type as well according to the docs, since it isn't a clickable link):

Error: Got error while executing phase phase_system_discovery
  At VmErrorAt { index: 0, kind: MissingFunction { hash: 0x811b62957ea9d9f9 } }
  Chain: []
VorpalBlade commented 3 months ago

first_location is also useless, another private type, and it appears to print a bunch of byte code with Debug, and doesn't implement Display.

udoprog commented 3 months ago

To convert an error into one which is more descriptive you use the associated emit methods. If you want to capture it into a string, you can use a buffer like what's being done in rune-wasm here:

https://github.com/rune-rs/rune/blob/main/crates/rune-wasm/src/lib.rs#L269

https://github.com/rune-rs/rune/blob/main/crates/rune-wasm/src/lib.rs#L301

To explain, detailed diagnostics has a memory overhead. To emit it you need to provide the context that was used to compile the program, all loaded sources, and the compiled unit (with debug info).

VorpalBlade commented 3 months ago

Thank you, I propose adding some cross references to the docs for VmError about this. In fact I'll make a PR later today for it.

Discoverability when trying to solve an issue is a probkem in the rustdoc of many large Rust projects (not only Rune, I have struggled with clap and bpaf before for example). Part of that is due to the sub-par search function of rustdoc (only matching names of types/functions).

VorpalBlade commented 3 months ago

Actually, looking at it again, I'm not sure how I missed the emit method on the VmError type. Don't think there is much to improve (apart from my own reading comprehension).