-
Notifications
You must be signed in to change notification settings - Fork 363
Error handling improvements #907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a73212f
0e10bdd
fa3347f
17886da
6c13af2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "basic-error-error-crates-integration" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
eyre = "0.6.12" | ||
lambda_runtime = { path = "../../lambda-runtime", features = ["anyhow", "eyre", "miette"] } | ||
miette = "7.2.0" | ||
serde = "1" | ||
tokio = { version = "1", features = ["macros"] } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use lambda_runtime::{run, service_fn, Diagnostic, IntoDiagnostic, Error, LambdaEvent}; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
enum ErrorType { | ||
Anyhow, | ||
Eyre, | ||
Miette, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Request { | ||
error_type: ErrorType, | ||
} | ||
|
||
fn anyhow_error() -> anyhow::Result<()> { | ||
anyhow::bail!("This is an error message from Anyhow"); | ||
} | ||
|
||
fn eyre_error() -> eyre::Result<()> { | ||
eyre::bail!("This is an error message from Eyre"); | ||
} | ||
|
||
fn miette_error() -> miette::Result<()> { | ||
miette::bail!("This is an error message from Miette"); | ||
} | ||
|
||
/// Transform an anyhow::Error, eyre::Report, or miette::Report into a lambda_runtime::Diagnostic. | ||
/// It does it by enabling the feature `anyhow`, `eyre` or `miette` in the runtime dependency, | ||
/// and importing the `IntoDiagnostic` trait, which enables | ||
/// the implementation of `into_diagnostic` for `anyhow::Error`, `eyre::Report`, and `miette::Report`. | ||
async fn function_handler(event: LambdaEvent<Request>) -> Result<(), Diagnostic> { | ||
match event.payload.error_type { | ||
ErrorType::Anyhow => anyhow_error().map_err(|e| e.into_diagnostic()), | ||
ErrorType::Eyre => eyre_error().map_err(|e| e.into_diagnostic()), | ||
ErrorType::Miette => miette_error().map_err(|e| e.into_diagnostic()), | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
run(service_fn(function_handler)).await | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "basic-error-diagnostic" | ||
name = "basic-error-thiserror" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,16 @@ readme = "../README.md" | |
default = ["tracing"] | ||
tracing = ["lambda_runtime_api_client/tracing"] | ||
opentelemetry = ["opentelemetry-semantic-conventions"] | ||
anyhow = ["dep:anyhow"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there is no standard way of documenting features yet (rust-lang/rfcs#3485), shall we add The features are described in the ReadMe, but that's likely to be missed on the first read. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea. I've documented them all since I was there. |
||
eyre = ["dep:eyre"] | ||
miette = ["dep:miette"] | ||
|
||
[dependencies] | ||
anyhow = { version = "1.0.86", optional = true } | ||
async-stream = "0.3" | ||
base64 = { workspace = true } | ||
bytes = { workspace = true } | ||
eyre = { version = "0.6.12", optional = true } | ||
futures = { workspace = true } | ||
http = { workspace = true } | ||
http-body = { workspace = true } | ||
|
@@ -35,6 +40,7 @@ hyper-util = { workspace = true, features = [ | |
"tokio", | ||
] } | ||
lambda_runtime_api_client = { version = "0.11.1", path = "../lambda-runtime-api-client", default-features = false } | ||
miette = { version = "7.2.0", optional = true } | ||
opentelemetry-semantic-conventions = { version = "0.14", optional = true } | ||
pin-project = "1" | ||
serde = { version = "1", features = ["derive", "rc"] } | ||
|
Uh oh!
There was an error while loading. Please reload this page.