Use hdf5_rs silence_errors

The hdf5 library seems to really like to call out errors on stderr, but I don't want that.

What's the correct way to silence errors? There's no documentation that I can find on this.

use hdf5::{File, Group};

fn main() {
    println!("Hello, world!");

    hdf5::silence_errors();

    File::open("bob");
}

Output:

Hello, world!
HDF5-DIAG: Error detected in HDF5 (1.10.4) thread 139628433381440:
  #000: ../../../src/H5F.c line 509 in H5Fopen(): unable to open file
    major: File accessibilty
    minor: Unable to open file
  #001: ../../../src/H5Fint.c line 1400 in H5F__open(): unable to open file
    major: File accessibilty
    minor: Unable to open file
  #002: ../../../src/H5Fint.c line 1546 in H5F_open(): unable to open file: time = Wed May 26 16:32:30 2021
, name = 'bob', tent_flags = 0
    major: File accessibilty
    minor: Unable to open file
  #003: ../../../src/H5FD.c line 734 in H5FD_open(): open failed
    major: Virtual File Layer
    minor: Unable to initialize object
  #004: ../../../src/H5FDsec2.c line 346 in H5FD_sec2_open(): unable to open file: name = 'bob', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0
    major: File accessibilty
    minor: Unable to open file

Sometimes you have to publicly expose your ignorance before you can find an answer yourself.

silence_errors() is an alias for SilenceErrors::new(), which is private.

So you need to create a variable that Rust will cause to persist. Not

{
  silence_errors();

  // other code
}

but rather:

{
  let _guard = silence_errors();

  // other code
}
3 Likes

Been there, done that, got soooooooo many T-shirts.

Curiously enough, while I've ignorantly bumbled around the hdf5 crate, taking silly amounts of time to understand all sorts of things, this particular issue never arose, because I had that guard in my code from the very first attempt, even if I couldn't tell you where I found it.

Nice. Rust has a way to deal with this - a must use lint on the function that creates the guard, and it should be better.

Can you point me to an example of this, or give me a keyword to search on?

The way the thing is written it's not a must-use -- either you don't use it and you get pummeled with diagnostics from the hdf5 code, or you do use it and you don't. Arguably, the function should take a boolean as an argument -- but it doesn't.

I have not been finding much material on this crate -- I'm just going by the very sparse documentation.

Here's some must_use documentation, but SilenceErrors already has it.

Did you not get:

warning: unused `SilenceErrors` that must be used

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.