Update docs

This commit is contained in:
Sunli 2020-10-02 15:34:20 +08:00
parent 150de7bfdd
commit 323dbf8a87
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: A resolver looks like this:
```rust ```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" }); 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 { impl ErrorExtensions for MyError {
// lets define our base extensions // lets define our base extensions
fn extend(&self) -> Error { fn extend(&self) -> Error {
let extensions = match self { Error::new(format!("{}", self)).extend_with(|err|
MyError::NotFound => json!({"code": "NOT_FOUND"}), match self {
MyError::ServerError(reason) => json!({ "reason": reason }), MyError::NotFound => json!({"code": "NOT_FOUND"}),
MyError::ErrorWithoutExtensions => { MyError::ServerError(reason) => json!({ "reason": reason }),
json!("This will be ignored since it does not represent an object.") MyError::ErrorWithoutExtensions => {
} json!("This will be ignored since it does not represent an object.")
}; }
})
Error(format!("{}", self), Some(extensions))
} }
} }
``` ```

View File

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