actix-web

Learn about using Sentry with Actix Web.

The sentry-actix crate adds a middleware for actix-web that captures errors and report them to Sentry.

To use this middleware, configure Sentry then add it to your actix web app as a middleware. Because actix is generally working with non sendable objects and is highly concurrent, this middleware creates a new hub per request. As a result, many of the Sentry integrations, such as breadcrumbs, do not work unless you bind the actix hub.

Note: Macros like #[tokio::main] and #[actix_web::main] are not supported. The Sentry client must be initialized before the async runtime is started so that all threads are correctly connected to the Hub.

In your Cargo.toml:

Cargo.toml
Copied
[dependencies]
actix-web = "4.3.1"
sentry = "0.32.3"
sentry-actix = "0.32.3"

And your Rust code:

main.rs
Copied
use std::io;

use actix_web::{get, App, Error, HttpRequest, HttpServer};

#[get("/")]
async fn failing(_req: HttpRequest) -> Result<String, Error> {
    Err(io::Error::new(io::ErrorKind::Other, "An error happens here").into())
}

fn main() -> io::Result<()> {
    let _guard = sentry::init((
        "https://examplePublicKey@o0.ingest.sentry.io/0",
        sentry::ClientOptions {
            release: sentry::release_name!(),
            ..Default::default()
        },
    ));
    std::env::set_var("RUST_BACKTRACE", "1");

    actix_web::rt::System::new().block_on(async {
        HttpServer::new(|| {
            App::new()
                .wrap(sentry_actix::Sentry::new())
                .service(failing)
        })
        .bind("127.0.0.1:3001")?
        .run()
        .await
    })?;

    Ok(())
}

When using the actix integration, a new per-request Hub will be created from the main Hub, and will be set automatically as the current Hub (Hub::current()). No manual intervention in needed.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").