From f2696d54a0a0a489126307633d4ed17248c2cf55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alja=C5=BE=20Mur=20Er=C5=BEen?= Date: Mon, 8 Apr 2024 18:43:49 +0200 Subject: [PATCH 1/2] Remove cyclic dev-dependencies --- edgedb-derive/Cargo.toml | 3 +- edgedb-errors/Cargo.toml | 8 +---- edgedb-errors/src/lib.rs | 74 +------------------------------------- edgedb-tokio/src/lib.rs | 76 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 82 deletions(-) diff --git a/edgedb-derive/Cargo.toml b/edgedb-derive/Cargo.toml index 005ec097..713fd219 100644 --- a/edgedb-derive/Cargo.toml +++ b/edgedb-derive/Cargo.toml @@ -18,8 +18,7 @@ trybuild = "1.0.19" [dev-dependencies] bytes = "1.0.1" edgedb-protocol = {path="../edgedb-protocol"} -edgedb-tokio = {path="../edgedb-tokio"} -serde = "1.0" +serde = {version="1.0", features=["derive"]} serde_json = "1.0" [lib] diff --git a/edgedb-errors/Cargo.toml b/edgedb-errors/Cargo.toml index d05f904e..0ca97c76 100644 --- a/edgedb-errors/Cargo.toml +++ b/edgedb-errors/Cargo.toml @@ -11,12 +11,6 @@ readme = "README.md" [dependencies] bytes = "1.0.1" -miette = { version="5.3.0", optional=true } - -[dev-dependencies] -tokio = "1.0" -anyhow = "1.0" -edgedb-tokio = { path="../edgedb-tokio" } +miette = { version = "5.3.0", optional = true } [lib] - diff --git a/edgedb-errors/src/lib.rs b/edgedb-errors/src/lib.rs index d31b660c..93531259 100644 --- a/edgedb-errors/src/lib.rs +++ b/edgedb-errors/src/lib.rs @@ -73,79 +73,7 @@ Special care for errors must be taken in transactions. Generally: # Nice Error Reporting -We use [miette] crate for including snippets in your error reporting code. - -To make it work, first you need enable `fancy` feature in your top-level -crate's `Cargo.toml`: -```toml -[dependencies] -miette = { version="5.3.0", features=["fancy"] } -edgedb-tokio = { version="*", features=["miette-errors"] } -``` - -Then if you use `miette` all the way through your application, it just -works: -```rust,no_run -#[tokio::main] -async fn main() -> miette::Result<()> { - let conn = edgedb_tokio::create_client().await?; - conn.query::("SELECT 1+2)", &()).await?; - Ok(()) -} -``` - -However, if you use some boxed error container (e.g. [anyhow]), you -might need to downcast error for printing: -```rust,no_run -async fn do_something() -> anyhow::Result<()> { - let conn = edgedb_tokio::create_client().await?; - conn.query::("SELECT 1+2)", &()).await?; - Ok(()) -} - -#[tokio::main] -async fn main() { - match do_something().await { - Ok(res) => res, - Err(e) => { - e.downcast::() - .map(|e| eprintln!("{:?}", miette::Report::new(e))) - .unwrap_or_else(|e| eprintln!("{:#}", e)); - std::process::exit(1); - } - } -} -``` - -In some cases, where parts of your code use `miette::Result` or -`miette::Report` before converting to the boxed (anyhow) container, you -might want a little bit more complex downcasting: - -```rust,no_run -# async fn do_something() -> anyhow::Result<()> { unimplemented!() } -#[tokio::main] -async fn main() { - match do_something().await { - Ok(res) => res, - Err(e) => { - e.downcast::() - .map(|e| eprintln!("{:?}", miette::Report::new(e))) - .or_else(|e| e.downcast::() - .map(|e| eprintln!("{:?}", e))) - .unwrap_or_else(|e| eprintln!("{:#}", e)); - std::process::exit(1); - } - } -} -``` - -Note that last two examples do hide error contexts from anyhow and do not -pretty print if `source()` of the error is `edgedb_errors::Error` but not -the top-level one. We leave those more complex cases as an excersize to the -reader. - -[miette]: https://crates.io/crates/miette -[anyhow]: https://crates.io/crates/anyhow +Refer to documentation in the [edgedb-tokio](https://docs.rs/edgedb-tokio) crate. */ mod error; mod traits; diff --git a/edgedb-tokio/src/lib.rs b/edgedb-tokio/src/lib.rs index 95349d7f..09d83732 100644 --- a/edgedb-tokio/src/lib.rs +++ b/edgedb-tokio/src/lib.rs @@ -28,6 +28,82 @@ async fn main() -> anyhow::Result<()> { } ``` More [examples on github](https://github.com/edgedb/edgedb-rust/tree/master/edgedb-tokio/examples) + +# Nice Error Reporting + +We use [miette] crate for including snippets in your error reporting code. + +To make it work, first you need enable `fancy` feature in your top-level +crate's `Cargo.toml`: +```toml +[dependencies] +miette = { version="5.3.0", features=["fancy"] } +edgedb-tokio = { version="*", features=["miette-errors"] } +``` + +Then if you use `miette` all the way through your application, it just +works: +```rust,no_run +#[tokio::main] +async fn main() -> miette::Result<()> { + let conn = edgedb_tokio::create_client().await?; + conn.query::("SELECT 1+2)", &()).await?; + Ok(()) +} +``` + +However, if you use some boxed error container (e.g. [anyhow]), you +might need to downcast error for printing: +```rust,no_run +async fn do_something() -> anyhow::Result<()> { + let conn = edgedb_tokio::create_client().await?; + conn.query::("SELECT 1+2)", &()).await?; + Ok(()) +} + +#[tokio::main] +async fn main() { + match do_something().await { + Ok(res) => res, + Err(e) => { + e.downcast::() + .map(|e| eprintln!("{:?}", miette::Report::new(e))) + .unwrap_or_else(|e| eprintln!("{:#}", e)); + std::process::exit(1); + } + } +} +``` + +In some cases, where parts of your code use `miette::Result` or +`miette::Report` before converting to the boxed (anyhow) container, you +might want a little bit more complex downcasting: + +```rust,no_run +# async fn do_something() -> anyhow::Result<()> { unimplemented!() } +#[tokio::main] +async fn main() { + match do_something().await { + Ok(res) => res, + Err(e) => { + e.downcast::() + .map(|e| eprintln!("{:?}", miette::Report::new(e))) + .or_else(|e| e.downcast::() + .map(|e| eprintln!("{:?}", e))) + .unwrap_or_else(|e| eprintln!("{:#}", e)); + std::process::exit(1); + } + } +} +``` + +Note that last two examples do hide error contexts from anyhow and do not +pretty print if `source()` of the error is `edgedb_errors::Error` but not +the top-level one. We leave those more complex cases as an excersize to the +reader. + +[miette]: https://crates.io/crates/miette +[anyhow]: https://crates.io/crates/anyhow */ #![cfg_attr(not(feature="unstable"), From 2f27b62f813ec9787d155678b8226c5aa17efbd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alja=C5=BE=20Mur=20Er=C5=BEen?= Date: Mon, 8 Apr 2024 18:54:48 +0200 Subject: [PATCH 2/2] fix --- edgedb-tokio/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/edgedb-tokio/Cargo.toml b/edgedb-tokio/Cargo.toml index b25070df..ff620f30 100644 --- a/edgedb-tokio/Cargo.toml +++ b/edgedb-tokio/Cargo.toml @@ -51,6 +51,8 @@ env_logger = "0.10" thiserror = "1.0.30" test-log = "0.2.8" futures-util = "0.3.21" +miette = {version = "5.3.0", features = ["fancy"]} +edgedb-errors = {path = "../edgedb-errors", features = ["miette"]} [features] default = ["derive", "env"]