rust - 是否有一种紧凑且惯用的方式来打印错误并返回而不返回错误?

标签 rust

我正在编写一个将在无限循环中调用的函数,并且仅在从 Web 服务获取格式正确的数据时才执行某些操作。如果服务关闭,返回非 json,或者返回我们不理解的 json,函数应该记录错误并返回(暂停后再次调用)。

我发现自己在复制和粘贴如下内容:

let v = match v {
    Ok(data) => data,
    Err(error) => {
        println!("Error decoding json: {:?}", error);
        return;
    }
};

错误匹配器的主体每次都会不同。有时它是 panic ,有时它有不同的消息,有时 error 的元素可以进一步分解以形成更好的消息,但结构的其余部分将是相同的。

这个有简写吗?我知道 ? syntax ,但这是为了传播。当您需要稍微不同的处理以防出现上述情况中的错误时,我认为传播不会对这种情况有所帮助。这是因为处理方面的特殊差异就在这里,而不是在堆栈上。

我还没有用 Rust 编写很多代码,所以很可能我遗漏了一些明显的东西。

在 C# 中,上面的代码看起来像这样:

if (v == null)
{
  Console.WriteLine("Error decoding json!");
  return;
}

if (error != null)
{
  Console.WriteLine($"Error decoding json: {error}");
  return;
}

两者都比 Rust 简洁得多。

如果我理解下面的评论,一种缩短的方法是这样的:

if let Err(error) = v {
    println!("Error decoding json: {:?}", error);
    return;
}
let v = v.unwrap();

这看起来更紧凑,谢谢。这是惯用语吗?你会这样写吗?

最佳答案

I don't feel that propagation will help with the scenario when you need slightly different processing in case of the error like in the scenario described above. This is because the particular differences in handling belong right here, not up the stack.

这是自定义错误类型可以帮助解决的问题。在这种情况下,您有一个共同的行为(“记录错误”),并且您希望针对不同的值以稍微不同的方式来做到这一点。将“记录错误”部分移至调用方是有意义的(让我们调用函数 try_poll ):

loop {
    if let Err(e) = try_poll() {
        println!("{}", e);
    }
    sleep(100);
}

并创建一个实现 Display 的类型, 和 From<E>对于每种错误类型 E :

enum PollError {
    NetworkError(NetworkError),
    JsonParseError(JsonParseError),
}

impl fmt::Display for PollError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            PollError::NetworkError(ref e) => write!(f, "Error downloading file: {:?}", e),
            PollError::JsonParseError(ref e) => write!(f, "Error parsing JSON: {:?}", e),
        }
    }
}

impl From<NetworkError> for PollError {
    fn from(e: NetworkError) -> Self {
        PollError::NetworkError(e)
    }
}

impl From<JsonParseError> for PollError {
    fn from(e: JsonParseError) -> Self {
        PollError::JsonParseError(e)
    }
}

现在您可以使用 ?传播错误,但调用者仍然不必关心具体是哪个错误。

fn try_poll() -> Result<(), PollError> {
    let data = try_fetch_content()?;
    let json = try_parse_json(data)?;
    println!("Parsed {:?}", json);
    Ok(())
}

( playground )


好的,我想要那个,但没有所有 From实现。

关于这一切的繁琐部分都是 impl From s,这是必需的,因为自定义错误类型。如果对错误所做的唯一事情就是记录并忽略它,那么自定义错误类型就不是特别有用了——唯一真正需要返回的是错误消息本身。

在这种情况下,有 try_poll而是返回 Result<(), String> ,并使用 Result::map_err在使用 ? 之前立即将每个单独的错误转换为错误消息传播它:

fn try_poll() -> Result<(), String> {
    let data = try_fetch_content()
        .map_err(|e| format!("Error downloading file: {:?}", e))?;
    let json = try_parse_json(data)
        .map_err(|e| format!("Error parsing JSON: {:?}", e))?;
    println!("Parsed {:?}", json);
    Ok(())
}

( playground )

first edition of The Rust Programming Language关于 String 有什么要说的吗?作为错误类型:

A rule of thumb is to define your own error type, but a String error type will do in a pinch, particularly if you're writing an application. If you're writing a library, defining your own error type should be strongly preferred so that you don't remove choices from the caller unnecessarily.

关于rust - 是否有一种紧凑且惯用的方式来打印错误并返回而不返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259716/

相关文章:

hash - 如何使用氧化钠 crate 对字符串进行哈希处理?

Rust 泛型, "` T` 可能活得不够长”

rust - 将类型之间的关系指定为特征或宏

rust - 尽管已实现,但尚未实现特征Serialize?

rust - 基于特征标志的特征实现的条件界限?

enums - 我应该使用枚举还是盒装特征对象来模拟多态性?

rust - 有条件地在Rust中对Vec进行排序

rust - 为什么将rand/rand_core与#![no_std]一起使用会导致 “duplicate lang item”?

memory - 如何不安全地增加可变 byte slice 的大小?

android - 为移动应用程序集成 Rust + Flutter + Kotlin