10 个版本

使用旧 Rust 2015

0.1.8 2020 年 5 月 2 日
0.1.7 2020 年 3 月 5 日
0.1.6 2019 年 10 月 11 日
0.1.5 2019 年 1 月 2 日
0.1.1 2017 年 11 月 30 日

⚠️ 已报告问题

#23 in #error-chain

Download history 118581/week @ 2024-03-14 114708/week @ 2024-03-21 113043/week @ 2024-03-28 100787/week @ 2024-04-04 90797/week @ 2024-04-11 98112/week @ 2024-04-18 92576/week @ 2024-04-25 84556/week @ 2024-05-02 79841/week @ 2024-05-09 101900/week @ 2024-05-16 97712/week @ 2024-05-23 92376/week @ 2024-05-30 95631/week @ 2024-06-06 90129/week @ 2024-06-13 91102/week @ 2024-06-20 68648/week @ 2024-06-27

361,756 每月下载量
此 crate 已失去人气

MIT/Apache

67KB
1K SLoC

failure - 一个新的错误管理故事

注意failure 已弃用。如果您喜欢 failure 的 API,请考虑使用

  • Anyhowfailure::Error 的一个好替代品。
  • thiserror#[derive(Fail)] 的近完美替代品。

Build Status Latest Version docs

failure 设计用来使 Rust 中的错误管理更简单。它的目的是用过去几年学到的经验,包括从 quick-error 和 error-chain 获得的经验,来取代基于 std::error::Error 的错误管理。

failure 提供了两个核心组件

  • Fail:一个新的用于自定义错误类型的 trait。
  • Error:一个结构体,任何实现了 Fail 的类型都可以转换为。

发展历程

Failure 目前作为一个库在发展。首先,Rust 本身在 修复错误 trait 方面正在开展工作;其次,Failure 向 1.0 的原始计划在当前形式下不太可能实现。

因此,原始的 master 分支(指向 1.0 的 Failure)已被移除,现在 master 代表 0.1 的未来迭代步骤,直到在 stdlib 中明确知道会发生什么。

原始的 1.0 分支可以在 evolution/1.0 中找到。

示例

extern crate serde;
extern crate toml;

#[macro_use] extern crate failure;
#[macro_use] extern crate serde_derive;

use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;

use failure::Error;

// This is a new error type that you've created. It represents the ways a
// toolchain could be invalid.
//
// The custom derive for Fail derives an impl of both Fail and Display.
// We don't do any other magic like creating new types.
#[derive(Debug, Fail)]
enum ToolchainError {
    #[fail(display = "invalid toolchain name: {}", name)]
    InvalidToolchainName {
        name: String,
    },
    #[fail(display = "unknown toolchain version: {}", version)]
    UnknownToolchainVersion {
        version: String,
    }
}

pub struct ToolchainId {
    // ... etc
}

impl FromStr for ToolchainId {
    type Err = ToolchainError;

    fn from_str(s: &str) -> Result<ToolchainId, ToolchainError> {
        // ... etc
    }
}

pub type Toolchains = HashMap<ToolchainId, PathBuf>;

// This opens a toml file containing associations between ToolchainIds and
// Paths (the roots of those toolchains).
//
// This could encounter an io Error, a toml parsing error, or a ToolchainError,
// all of them will be thrown into the special Error type
pub fn read_toolchains(path: PathBuf) -> Result<Toolchains, Error>
{
    use std::fs::File;
    use std::io::Read;

    let mut string = String::new();
    File::open(path)?.read_to_string(&mut string)?;

    let toml: HashMap<String, PathBuf> = toml::from_str(&string)?;

    let toolchains = toml.iter().map(|(key, path)| {
        let toolchain_id = key.parse()?;
        Ok((toolchain_id, path))
    }).collect::<Result<Toolchains, ToolchainError>>()?;

    Ok(toolchains)
}

要求

失败和failure_derive都旨在在所有比1.31.0版本更新的稳定版Rust、最新的beta版和最新的nightly版上编译。如果任何crate在比1.31.0版本更新的版本上编译失败,请提交一个issue。

failure与no_std兼容,尽管其某些方面(主要是Error类型)在no_std模式下不可用。

许可证

failure根据MIT许可证或Apache许可证2.0的条款进行许可,您可自行选择。

行为准则

对failure crate的贡献是在贡献者协约的条款下组织的,failure的维护者@withoutboats承诺将介入以维护该行为准则。

依赖

~0–780KB
~16K SLoC