Skip to content

Commit eb97b25

Browse files
committed
Recommend to implement From.
1 parent a73212f commit eb97b25

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ The Rust Runtime for Lambda uses a struct called `Diagnostic` to represent funct
8686

8787
### Implement your own Diagnostic
8888

89-
To get more descriptive `error_type` fields, you can implement `Into<Diagnostic>` for your error type. That gives you full control on what the `error_type` is:
89+
To get more descriptive `error_type` fields, you can implement `From` for your error type. That gives you full control on what the `error_type` is:
9090

9191
```rust
9292
use lambda_runtime::{Diagnostic, Error, LambdaEvent};
9393

9494
#[derive(Debug)]
9595
struct ErrorResponse(&'static str);
9696

97-
impl Into<Diagnostic> for ErrorResponse {
98-
fn into(self) -> Diagnostic {
97+
impl From<ErrorResponse> for Diagnostic {
98+
fn into(error: ErrorResponse) -> Diagnostic {
9999
Diagnostic {
100100
error_type: "MyErrorType".into(),
101-
error_message: self.0.to_string(),
101+
error_message: error.0.to_string(),
102102
}
103103
}
104104
}

lambda-runtime/src/diagnostic.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ use crate::{deserializer::DeserializeError, Error};
1414
/// [`error_type`][`Diagnostic::error_type`] is derived from the type name of
1515
/// the original error with [`std::any::type_name`] as a fallback, which may
1616
/// not be reliable for conditional error handling.
17-
/// You can define your own error container that implements `Into<Diagnostic>`
18-
/// if you need to handle errors based on error types.
17+
///
18+
/// To get more descriptive [`error_type`][`Diagnostic::error_type`] fields, you can implement `From` for your error type.
19+
/// That gives you full control on what the `error_type` is.
1920
///
2021
/// Example:
2122
/// ```
@@ -24,11 +25,11 @@ use crate::{deserializer::DeserializeError, Error};
2425
/// #[derive(Debug)]
2526
/// struct ErrorResponse(&'static str);
2627
///
27-
/// impl Into<Diagnostic> for ErrorResponse {
28-
/// fn into(self) -> Diagnostic {
28+
/// impl From<ErrorResponse> for Diagnostic {
29+
/// fn into(error: ErrorResponse) -> Diagnostic {
2930
/// Diagnostic {
3031
/// error_type: "MyError".into(),
31-
/// error_message: self.0.to_string(),
32+
/// error_message: error.0.to_string(),
3233
/// }
3334
/// }
3435
/// }

0 commit comments

Comments
 (0)