How to customize the `main` error message when returning an `Error`

Since Rust 1.26, main can return a Result: “If main returns an error, this will exit with an error code, and print out a debug representation of the error”.

For instance the following code:

use failure::{err_msg, Error};
use std::fs::File;
fn main() -> Result<(), Error> {
    let _ = File::open("bar.txt").map_err(|_| err_msg("test"))?;

prints out:

Error: ErrorMessage { msg: "test" }

(debug representation) while I would like it to print out:

Error: test

(display representation).

One way would be to revert main back to the previous style:

fn main() {
    if let Err(e) = File::open("bar.txt").map_err(|_| err_msg("test")) {
        eprintln!("Error: {}", e);

but is there a simple way to customize the error message while keeping the new Rust 1.26 functionality? I hope I do not need to implement my custom Error type only for this…

The least amount of code for this is indeed to “revert back to the previous style” with if let Err(e) = run().

Any custom error newtype and trait impl is going to have more boilerplate (but AFAIK there are some crates providing that boilerplate for you if you want it out of sight).

The motivation for ? in main was reducing amount of code for minimal code examples, not to move error handling to a mysterious hidden place outside of main.