Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

[WIP] Failure 0.2 #296

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ homepage = "https://rust-lang-nursery.github.io/failure/"
license = "MIT OR Apache-2.0"
name = "failure"
repository = "https://github.com/rust-lang-nursery/failure"
version = "0.1.5"
version = "0.2.0"
edition = "2018"

[dependencies.failure_derive]
optional = true
version = "0.1.5"
version = "0.2.0"
path = "./failure_derive"

[dependencies.backtrace]
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;

use failure::Error;
use failure::DefaultError;

// This is a new error type that you've created. It represents the ways a
// toolchain could be invalid.
Expand All @@ -49,11 +49,11 @@ use failure::Error;
// We don't do any other magic like creating new types.
#[derive(Debug, Fail)]
enum ToolchainError {
#[fail(display = "invalid toolchain name: {}", name)]
#[error(display = "invalid toolchain name: {}", name)]
InvalidToolchainName {
name: String,
},
#[fail(display = "unknown toolchain version: {}", version)]
#[error(display = "unknown toolchain version: {}", version)]
UnknownToolchainVersion {
version: String,
}
Expand Down
14 changes: 7 additions & 7 deletions book/src/custom-fail.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ To implement this pattern, you should define your own type that implements
example:

```rust
#[derive(Fail, Debug)]
#[fail(display = "Input was invalid UTF-8")]
#[derive(Error, Debug)]
#[error(display = "Input was invalid UTF-8")]
pub struct Utf8Error;
```

Expand All @@ -24,8 +24,8 @@ case. It can be an enum with a different variant for each possible error, and
it can carry data with more precise information about the error. For example:

```rust
#[derive(Fail, Debug)]
#[fail(display = "Input was invalid UTF-8 at index {}", index)]
#[derive(Error, Debug)]
#[error(display = "Input was invalid UTF-8 at index {}", index)]
pub struct Utf8Error {
index: usize,
}
Expand All @@ -50,11 +50,11 @@ be inclined to add it as a variant on your own error type. When you do that,
you should tag the underlying error as the `#[fail(cause)]` of your error:

```rust
#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum MyError {
#[fail(display = "Input was invalid UTF-8 at index {}", _0)]
#[error(display = "Input was invalid UTF-8 at index {}", _0)]
Utf8Error(usize),
#[fail(display = "{}", _0)]
#[error(display = "{}", _0)]
Io(#[fail(cause)] io::Error),
}
```
Expand Down
46 changes: 23 additions & 23 deletions book/src/derive-fail.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ In its smallest form, deriving Fail looks like this:

use std::fmt;

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
struct MyError;

impl fmt::Display for MyError {
Expand All @@ -36,8 +36,8 @@ You can derive an implementation of `Display` with a special attribute:
```rust
#[macro_use] extern crate failure;

#[derive(Fail, Debug)]
#[fail(display = "An error occurred.")]
#[derive(Error, Debug)]
#[error(display = "An error occurred.")]
struct MyError;
```

Expand All @@ -54,8 +54,8 @@ formatting and printing macros:
```rust
#[macro_use] extern crate failure;

#[derive(Fail, Debug)]
#[fail(display = "An error occurred with error code {}. ({})", code, message)]
#[derive(Error, Debug)]
#[error(display = "An error occurred with error code {}. ({})", code, message)]
struct MyError {
code: i32,
message: String,
Expand All @@ -81,13 +81,13 @@ prefixed with an underscore:
```rust
#[macro_use] extern crate failure;

#[derive(Fail, Debug)]
#[fail(display = "An error occurred with error code {}.", _0)]
#[derive(Error, Debug)]
#[error(display = "An error occurred with error code {}.", _0)]
struct MyError(i32);


#[derive(Fail, Debug)]
#[fail(display = "An error occurred with error code {} ({}).", _0, _1)]
#[derive(Error, Debug)]
#[error(display = "An error occurred with error code {} ({}).", _0, _1)]
struct MyOtherError(i32, String);
```

Expand All @@ -100,13 +100,13 @@ will match over the enum to generate the correct error message. For example:
```rust
#[macro_use] extern crate failure;

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
enum MyError {
#[fail(display = "{} is not a valid version.", _0)]
#[error(display = "{} is not a valid version.", _0)]
InvalidVersion(u32),
#[fail(display = "IO error: {}", error)]
#[error(display = "IO error: {}", error)]
IoError { error: io::Error },
#[fail(display = "An unknown error has occurred.")]
#[error(display = "An unknown error has occurred.")]
UnknownError,
}
```
Expand All @@ -122,19 +122,19 @@ field with the type `Backtrace`. This works for both structs and enums.
use failure::Backtrace;

/// MyError::backtrace will return a reference to the backtrace field
#[derive(Fail, Debug)]
#[fail(display = "An error occurred.")]
#[derive(Error, Debug)]
#[error(display = "An error occurred.")]
struct MyError {
backtrace: Backtrace,
}

/// MyEnumError::backtrace will return a reference to the backtrace only if it
/// is Variant2, otherwise it will return None.
#[derive(Fail, Debug)]
#[derive(Error, Debug)]
enum MyEnumError {
#[fail(display = "An error occurred.")]
#[error(display = "An error occurred.")]
Variant1,
#[fail(display = "A different error occurred.")]
#[error(display = "A different error occurred.")]
Variant2(Backtrace),
}
```
Expand All @@ -159,19 +159,19 @@ This can be used in fields of enums as well as structs.
use std::io;

/// MyError::cause will return a reference to the io_error field
#[derive(Fail, Debug)]
#[fail(display = "An error occurred.")]
#[derive(Error, Debug)]
#[error(display = "An error occurred.")]
struct MyError {
#[fail(cause)] io_error: io::Error,
}

/// MyEnumError::cause will return a reference only if it is Variant2,
/// otherwise it will return None.
#[derive(Fail, Debug)]
#[derive(Error, Debug)]
enum MyEnumError {
#[fail(display = "An error occurred.")]
#[error(display = "An error occurred.")]
Variant1,
#[fail(display = "A different error occurred.")]
#[error(display = "A different error occurred.")]
Variant2(#[fail(cause)] io::Error),
}
```
2 changes: 1 addition & 1 deletion book/src/error-errorkind.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum MyErrorKind {
// A plain enum with no data in any of its variants
//
// For example:
#[fail(display = "A contextual error message.")]
#[error(display = "A contextual error message.")]
OneVariant,
// ...
}
Expand Down
6 changes: 3 additions & 3 deletions book/src/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;

use failure::Error;
use failure::DefaultError;

// This is a new error type that you've created. It represents the ways a
// toolchain could be invalid.
Expand All @@ -29,11 +29,11 @@ use failure::Error;
// We don't do any other magic like creating new types.
#[derive(Debug, Fail)]
enum ToolchainError {
#[fail(display = "invalid toolchain name: {}", name)]
#[error(display = "invalid toolchain name: {}", name)]
InvalidToolchainName {
name: String,
},
#[fail(display = "unknown toolchain version: {}", version)]
#[error(display = "unknown toolchain version: {}", version)]
UnknownToolchainVersion {
version: String,
}
Expand Down
2 changes: 1 addition & 1 deletion book/src/use-error.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ functions:
use std::io;
use std::io::BufRead;

use failure::Error;
use failure::DefaultError;
use failure::err_msg;

fn my_function() -> Result<(), Error> {
Expand Down
6 changes: 3 additions & 3 deletions examples/bail_ensure.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#[macro_use]
extern crate failure;

use failure::Error;
use failure::DefaultError;

fn bailer() -> Result<(), Error> {
fn bailer() -> Result<(), DefaultError> {
// bail!("ruh roh");
bail!("ruh {}", "roh");
}

fn ensures() -> Result<(), Error> {
fn ensures() -> Result<(), DefaultError> {
ensure!(true, "true is false");
ensure!(false, "false is false");
Ok(())
Expand Down
25 changes: 13 additions & 12 deletions examples/error_as_cause.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#[macro_use]
extern crate failure;
// #[macro_use]
// extern crate failure;

use failure::{err_msg, Error, Fail};
// use failure::{err_msg, Error, Fail};

#[derive(Debug, Fail)]
#[fail(display = "my wrapping error")]
struct WrappingError(#[fail(cause)] Error);
// #[derive(Debug, Fail)]
// #[error(display = "my wrapping error")]
// struct WrappingError(#[fail(cause)] Error);

fn bad_function() -> Result<(), WrappingError> {
Err(WrappingError(err_msg("this went bad")))
}
// fn bad_function() -> Result<(), WrappingError> {
// Err(WrappingError(err_msg("this went bad")))
// }

fn main() {
for cause in Fail::iter_causes(&bad_function().unwrap_err()) {
println!("{}", cause);
}
// for cause in Fail::iter_causes(&bad_function().unwrap_err()) {
// println!("{}", cause);
// }
println!("Hello!");
}
10 changes: 5 additions & 5 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ extern crate failure;

use failure::Fail;

#[derive(Debug, Fail)]
#[fail(display = "my error")]
#[derive(Error, Debug)]
#[error(display = "my error")]
struct MyError;

#[derive(Debug, Fail)]
#[fail(display = "my wrapping error")]
struct WrappingError(#[fail(cause)] MyError);
#[derive(Error, Debug)]
#[error(display = "my wrapping error")]
struct WrappingError(#[error(cause)] MyError);

fn bad_function() -> Result<(), WrappingError> {
Err(WrappingError(MyError))
Expand Down
5 changes: 3 additions & 2 deletions failure_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ name = "failure_derive"
repository = "https://github.com/withoutboats/failure_derive"
homepage = "https://rust-lang-nursery.github.io/failure/"
documentation = "https://docs.rs/failure"
version = "0.1.5"
version = "0.2.0"
build = "build.rs"
edition = "2018"

[dependencies]
quote = "0.6.3"
Expand All @@ -16,7 +17,7 @@ synstructure = "0.10.0"
proc-macro2 = "0.4.8"

[dev-dependencies.failure]
version = "0.1.0"
version = "0.2.0"
path = ".."

[lib]
Expand Down
Loading