Skip to content

Commit

Permalink
Recommend to implement From.
Browse files Browse the repository at this point in the history
Signed-off-by: David Calavera <[email protected]>
  • Loading branch information
calavera committed Jul 8, 2024
1 parent a73212f commit 0e10bdd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ The Rust Runtime for Lambda uses a struct called `Diagnostic` to represent funct

### Implement your own Diagnostic

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:
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:

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

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

impl Into<Diagnostic> for ErrorResponse {
fn into(self) -> Diagnostic {
impl From<ErrorResponse> for Diagnostic {
fn from(error: ErrorResponse) -> Diagnostic {
Diagnostic {
error_type: "MyErrorType".into(),
error_message: self.0.to_string(),
error_message: error.0.to_string(),
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions lambda-runtime/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use crate::{deserializer::DeserializeError, Error};
/// [`error_type`][`Diagnostic::error_type`] is derived from the type name of
/// the original error with [`std::any::type_name`] as a fallback, which may
/// not be reliable for conditional error handling.
/// You can define your own error container that implements `Into<Diagnostic>`
/// if you need to handle errors based on error types.
///
/// To get more descriptive [`error_type`][`Diagnostic::error_type`] fields, you can implement `From` for your error type.
/// That gives you full control on what the `error_type` is.
///
/// Example:
/// ```
Expand All @@ -24,11 +25,11 @@ use crate::{deserializer::DeserializeError, Error};
/// #[derive(Debug)]
/// struct ErrorResponse(&'static str);
///
/// impl Into<Diagnostic> for ErrorResponse {
/// fn into(self) -> Diagnostic {
/// impl From<ErrorResponse> for Diagnostic {
/// fn from(error: ErrorResponse) -> Diagnostic {
/// Diagnostic {
/// error_type: "MyError".into(),
/// error_message: self.0.to_string(),
/// error_message: error.0.to_string(),
/// }
/// }
/// }
Expand Down

0 comments on commit 0e10bdd

Please sign in to comment.