I want to reduce the boilerplate code when handling errors when multiple lines are following each other. I have the following code, working, but I want to know if it is possible to reduce it:
let mut sto = match Storage::new() {
Ok(v) => v,
Err(e) => {
// if needed the error message could be the same as in the next
// bloc, but I want to stop the process here
error!("Failed to open the connection.");
return Err(e);
},
};
// If Ok(), do not care about
if let Err(e) = sto.set("key", item) {
error!("Failed to save the item.");
return Err(e);
}
Note: the error!
macro is a logger macro.
Would it be possible to have something like this ?
if let Err(e) = Storage::new().MAGICAL_FUNC().set("key", item) {
error!("Failed to save the item.");
return Err(e);
}
My searches:
unwrap
as MAGICAL_FUNC cause a panic, so it is not recoverable.- Using
and_then
with a closure will preventif let
from "catching" theErr
of theset
function in the closure. map_err
will not "unwrap"Ok()
. So a new line should be added to "unwrap".
Edit: precise error!
macro behaviour
➡️Solution: accepted answer + comment of Masklinn