diff --git a/docs/en/src/error_extensions.md b/docs/en/src/error_extensions.md index 69677a70..61eb57df 100644 --- a/docs/en/src/error_extensions.md +++ b/docs/en/src/error_extensions.md @@ -16,9 +16,9 @@ field `Option` which - if given some valid `serde_json::Map` A resolver looks like this: ```rust -async fn parse_with_extensions(&self) -> Result { +async fn parse_with_extensions(&self) -> Result { 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.") + } + }) } } ``` diff --git a/docs/zh-CN/src/error_extensions.md b/docs/zh-CN/src/error_extensions.md index edc69cb9..dc8c37c5 100644 --- a/docs/zh-CN/src/error_extensions.md +++ b/docs/zh-CN/src/error_extensions.md @@ -20,7 +20,7 @@ Resolver函数类似这样: ```rust async fn parse_with_extensions(&self) -> Result { 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.") + } + }) } } ```