Skip to content
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

Add anyhow::Error example #342

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"./examples/tauri/src-tauri",
"./examples/legacy",
"./examples/binario",
"./examples/anyhow",
]

[workspace.dependencies]
Expand Down
14 changes: 14 additions & 0 deletions examples/anyhow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "example-anyhow"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies]
rspc = { path = "../../rspc", features = ["typescript"] }
specta.workspace = true
anyhow = "1.0.95"
thiserror = "2.0.11"

[lints]
workspace = true
5 changes: 5 additions & 0 deletions examples/anyhow/bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file was generated by [rspc](https://github.com/specta-rs/rspc). Do not edit this file manually.

export type Procedures = {
anyhow: { kind: "query", input: any, output: any, error: any },
}
45 changes: 45 additions & 0 deletions examples/anyhow/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use rspc::{Procedure, Router};
use specta::Type;

////////////////////////////////////////////////////////////////////////////////////////////////////

fn main() {
let (_procedures, _types) = Router::new()
.procedure(
//
"anyhow",
Procedure::builder().query(anyhow_procedure),
)
.build()
.expect("router should be built");
}

// Some procedure that needs `anyhow::Error` to be compatible with `rspc`.
async fn anyhow_procedure(_ctx: (), _input: ()) -> Result<String, AnyhowError> {
let response = fallible()?; // `?` converts `anyhow::Error` into `AnyhowError`.
Ok(response)
}

fn fallible() -> Result<String, anyhow::Error> {
anyhow::bail!("oh no!")
}

////////////////////////////////////////////////////////////////////////////////////////////////////

// Make `anyhow::Error` work where `std::error::Error + Send + 'static` is expected.
// NB: Define this only once; afterwards, you can import and use it anywhere.
// See: https://github.com/dtolnay/anyhow/issues/153#issuecomment-833718851
#[derive(Debug, thiserror::Error, Type)]
#[error(transparent)]
struct AnyhowError(
#[from]
#[serde(skip)]
anyhow::Error,
);

impl rspc::Error for AnyhowError {
fn into_procedure_error(self) -> rspc::ProcedureError {
let message = format!("something bad happened: {}", self);
rspc::ResolverError::new(message, Some(self)).into()
}
}