项目作者: rust-lang-nursery

项目描述 :
Error management
高级语言: Rust
项目地址: git://github.com/rust-lang-nursery/failure.git
创建时间: 2017-09-25T20:35:41Z
项目社区:https://github.com/rust-lang-nursery/failure

开源协议:Other

下载


failure - a new error management story

Notice: failure is deprecated. If you liked failure‘s API, consider using:

  • Anyhow is a good replacement for failure::Error.
  • thiserror is a good, near drop-in replacement for #[derive(Fail)].

Build Status
Latest Version
docs

failure is designed to make it easier to manage errors in Rust. It is
intended to replace error management based on std::error::Error with a new
system based on lessons learned over the past several years, including those
learned from experience with quick-error and error-chain.

failure provides two core components:

  • Fail: A new trait for custom error types.
  • Error: A struct which any type that implements Fail can be cast into.

Evolution

Failure is currently evolving as a library. First of all there is work going
on in Rust itself to fix the error trait
secondarily the original plan for Failure towards 1.0 is unlikely to happen
in the current form.

As such the original master branch towards 1.0 of failure was removed and
master now represents the future iteration steps of 0.1 until it’s clear
what happens in the stdlib.

The original 1.0 branch can be found in evolution/1.0.

Example

  1. extern crate serde;
  2. extern crate toml;
  3. #[macro_use] extern crate failure;
  4. #[macro_use] extern crate serde_derive;
  5. use std::collections::HashMap;
  6. use std::path::PathBuf;
  7. use std::str::FromStr;
  8. use failure::Error;
  9. // This is a new error type that you've created. It represents the ways a
  10. // toolchain could be invalid.
  11. //
  12. // The custom derive for Fail derives an impl of both Fail and Display.
  13. // We don't do any other magic like creating new types.
  14. #[derive(Debug, Fail)]
  15. enum ToolchainError {
  16. #[fail(display = "invalid toolchain name: {}", name)]
  17. InvalidToolchainName {
  18. name: String,
  19. },
  20. #[fail(display = "unknown toolchain version: {}", version)]
  21. UnknownToolchainVersion {
  22. version: String,
  23. }
  24. }
  25. pub struct ToolchainId {
  26. // ... etc
  27. }
  28. impl FromStr for ToolchainId {
  29. type Err = ToolchainError;
  30. fn from_str(s: &str) -> Result<ToolchainId, ToolchainError> {
  31. // ... etc
  32. }
  33. }
  34. pub type Toolchains = HashMap<ToolchainId, PathBuf>;
  35. // This opens a toml file containing associations between ToolchainIds and
  36. // Paths (the roots of those toolchains).
  37. //
  38. // This could encounter an io Error, a toml parsing error, or a ToolchainError,
  39. // all of them will be thrown into the special Error type
  40. pub fn read_toolchains(path: PathBuf) -> Result<Toolchains, Error>
  41. {
  42. use std::fs::File;
  43. use std::io::Read;
  44. let mut string = String::new();
  45. File::open(path)?.read_to_string(&mut string)?;
  46. let toml: HashMap<String, PathBuf> = toml::from_str(&string)?;
  47. let toolchains = toml.iter().map(|(key, path)| {
  48. let toolchain_id = key.parse()?;
  49. Ok((toolchain_id, path))
  50. }).collect::<Result<Toolchains, ToolchainError>>()?;
  51. Ok(toolchains)
  52. }

Requirements

Both failure and failure_derive are intended to compile on all stable versions
of Rust newer than 1.31.0, as well as the latest beta and the latest nightly.
If either crate fails to compile on any version newer than 1.31.0, please open
an issue.

failure is no_std compatible, though some aspects of it (primarily the
Error type) will not be available in no_std mode.

License

failure is licensed under the terms of the MIT License or the Apache License
2.0, at your choosing.

Code of Conduct

Contribution to the failure crate is organized under the terms of the
Contributor Covenant, the maintainer of failure, @withoutboats, promises to
intervene to uphold that code of conduct.