Update docs

This commit is contained in:
Sunli 2020-10-02 15:34:20 +08:00
parent 87bcf055ac
commit 6ed1cd1b47
2 changed files with 19 additions and 21 deletions

View File

@ -16,9 +16,9 @@ field `Option<serde_json::Value>` which - if given some valid `serde_json::Map`
A resolver looks like this:
```rust
async fn parse_with_extensions(&self) -> Result<i32, Error> {
async fn parse_with_extensions(&self) -> Result<i32> {
let my_extension = json!({ "details": "CAN_NOT_FETCH" });
Err(Error("MyMessage", Some(my_extension)))
Err(Error::new("MyMessage").extend_with(|| my_extension))
}
```
@ -79,15 +79,14 @@ pub enum MyError {
impl ErrorExtensions for MyError {
// lets define our base extensions
fn extend(&self) -> Error {
let extensions = match self {
MyError::NotFound => json!({"code": "NOT_FOUND"}),
MyError::ServerError(reason) => json!({ "reason": reason }),
MyError::ErrorWithoutExtensions => {
json!("This will be ignored since it does not represent an object.")
}
};
Error(format!("{}", self), Some(extensions))
Error::new(format!("{}", self)).extend_with(|err|
match self {
MyError::NotFound => json!({"code": "NOT_FOUND"}),
MyError::ServerError(reason) => json!({ "reason": reason }),
MyError::ErrorWithoutExtensions => {
json!("This will be ignored since it does not represent an object.")
}
})
}
}
```

View File

@ -20,7 +20,7 @@ Resolver函数类似这样
```rust
async fn parse_with_extensions(&self) -> Result<i32, Error> {
let my_extension = json!({ "details": "CAN_NOT_FETCH" });
Err(Error("MyMessage", Some(my_extension)))
Err(Error::new("MyMessage").extend_with(|_| my_extension))
}
```
@ -83,15 +83,14 @@ pub enum MyError {
impl ErrorExtensions for MyError {
// lets define our base extensions
fn extend(&self) -> Error {
let extensions = match self {
MyError::NotFound => json!({"code": "NOT_FOUND"}),
MyError::ServerError(reason) => json!({ "reason": reason }),
MyError::ErrorWithoutExtensions => {
json!("This will be ignored since it does not represent an object.")
}
};
Error(format!("{}", self), Some(extensions))
Error::new(format!("{}", self)).extend_with(|err|
match self {
MyError::NotFound => json!({"code": "NOT_FOUND"}),
MyError::ServerError(reason) => json!({ "reason": reason }),
MyError::ErrorWithoutExtensions => {
json!("This will be ignored since it does not represent an object.")
}
})
}
}
```