Reporting errors
Once Flare runs, as set up in the quick start, a lot gets reported without any extra code: panics, the error that main returns, and the server errors of Actix Web. This page covers those, and what to do with the errors your code catches itself.
What Flare reports on its own
Panics. Flare installs a panic hook when it starts, and reports every panic on every thread with its message, the name of the thread and a stack trace. In Flare, a panic shows up with the exception class Panic and is marked as unhandled.
Because a program can end right after a panic, the hook waits until the report is sent, for at most the shutdown_timeout of 2 seconds. Then it hands over to the panic hook that was there before, so Rust still prints the panic message as usual. This also works with panic = "abort" in your profile.
Panics inside a tokio task or an axum handler are reported too. Tokio catches those, so your server keeps running, but you still hear about them.
Errors returned from main. With #[flareapp::main], an error that main returns is reported before the program exits. Rust still prints the error and exits with code 1, just like before:
Rust
#[flareapp::main]
fn main() -> Result<(), Box<dyn std::error::Error>> {
std::fs::read_to_string("orders.csv")?;
Ok(())
}
The attribute recognizes the error by the name of the return type, so the type has to be named Result, like io::Result<()> or anyhow::Result<()>. A type alias with another name, like AppResult, isn't reported. Use Result in the signature of main, or report the error yourself with report_error before you return it.
Server errors in Actix Web
With the Actix Web middleware, an error that a handler returns is reported when it ends in a 5xx response. The report gets the request data, like every report made during a request.
Actix Web hides the type of the error by the time the middleware sees it. Flare falls back to the Debug output of the error for the exception class, which gives you the name of the struct or enum variant. The stack trace points at the middleware, not at the line in your handler that made the error. If you want that line, report the error yourself from inside the handler with report_error.
To turn these reports off:
Rust
App::new()
.wrap(flareapp::actix::middleware().report_server_errors(false))
Reporting an error you handled
An error your code handles never reaches Flare on its own. When you still want to know about it, pass it to flareapp::report_error:
Rust
if let Err(error) = std::fs::read_to_string("orders.csv") {
flareapp::report_error(&error);
}
report_error takes anything that implements std::error::Error. Flare uses the Rust type of the error as the exception class, such as my_app::PaymentFailed, and its Display output as the message. The name comes from the compiler. For a std::io::Error it is std::io::error::Error before Rust 1.99 and core::io::error::Error from 1.99. A Rust upgrade can therefore start a new error in Flare. The causes of the error, from source(), aren't sent: they have no stack trace of their own.
A Box<dyn Error> doesn't implement std::error::Error itself, so pass error.as_ref(). The type behind the box is only known at runtime, so Flare takes the exception class from the name at the start of the Debug output. With a derived Debug, that's the name of the struct or enum variant. When the output doesn't start with a name, the class is Error:
Rust
let error: Box<dyn std::error::Error> = load_orders().unwrap_err();
flareapp::report_error(error.as_ref());
report_error returns the tracking id of the report, which you can show to your users so they can quote it when they contact you. It returns None when Flare is disabled, when a client hook dropped the report, or when the queue was full:
Rust
if let Some(id) = flareapp::report_error(&error) {
println!("Something went wrong. Error id: {id}");
}
To add context to this one report only, use report_error_with. The closure gets the report before it's sent:
Rust
use serde_json::json;
flareapp::report_error_with(&error, |report| {
report.set_context("payment", json!({ "provider": "mollie", "order_id": 7 }));
});
For context that belongs on every report, see adding custom context.
Sending a message without an error
Sometimes there's no error, but something still deserves your attention. Send it as a message, with a level:
Rust
use flareapp::Level;
flareapp::report_message("Stock for TSHIRT-M is low", Level::Warning);
The levels are Debug, Info, Notice, Warning, Error, Critical, Alert and Emergency. Messages show up under the Log exception class in Flare, so they're easy to tell apart from real errors. Flare groups them on their level and message, not on the line of code that sent them. Like report_error, report_message returns the tracking id of the report.
anyhow errors
anyhow::Error doesn't implement std::error::Error, so report_error can't take it. Turn on the anyhow feature and use flareapp::anyhow::report instead:
Terminal
cargo add flareapp --features anyhow
Rust
use anyhow::Context;
fn load_config() -> anyhow::Result<String> {
std::fs::read_to_string("config.toml").context("loading config")
}
if let Err(error) = load_config() {
flareapp::anyhow::report(&error);
}
The message is the whole context chain, like loading config: No such file or directory (os error 2). Behind an anyhow::Error, Rust no longer knows the type of the root cause, so Flare takes the exception class from the first word of its Debug output. With a derived Debug, that's the name of the struct or enum variant, like PaymentFailed, without its module. A std::io::Error keeps its full type name, and so does an error whose Debug output starts with a path, like reqwest::Error. An error made with anyhow!("...") has no type behind it, so it gets the class anyhow::Error.
For the stack trace, Flare uses the one anyhow captured where the error was created. anyhow only captures one when RUST_BACKTRACE or RUST_LIB_BACKTRACE is set, though. Without either, the stack trace starts where you reported the error.
flareapp::anyhow::report_with changes the report before it's sent, the same way report_error_with does. With the anyhow feature on, an anyhow::Error that main returns is reported the same way.
What a report contains
Every report carries:
- The exception class, the message and the stack trace.
- The environment, the name and version of your crate, and the git commit. See application info.
- The Rust version, the host name, the CPU architecture, the type of operating system and the command line of your program. With the
host-detailsfeature, also the IP address of the host and the name and version of its operating system. - The user, the context, the glows and the log breadcrumbs of the current scope.
- The entry point: the request, job or command the error happened in.
- During a request, with the axum or Actix Web middleware: the method, the URL, the route, the IP address of the client, the user agent and the request headers. With axum, the client IP is only there when you serve your app with
into_make_service_with_connect_info. Behind a proxy, it's the IP address of the proxy. Flare replaces the values of theAuthorization,Cookie,Set-Cookie,API-KEY,X-CSRF-TOKENandX-XSRF-TOKENheaders with<CENSORED:string>before the report leaves your app.
When the report happens inside a traced span, the trace shows it as an exception row. See what a trace shows.
Stack traces
The SDK resolves the stack trace inside your app, on its background thread. It marks the frames of your own crate as application frames, so you can tell them apart from the frames of the standard library and your dependencies.
A few things work differently from what you might know from other languages:
- Release builds need debug info. Without it, frames have function names but no files or lines. See release builds and debug files.
- There are no code snippets. Your deployed app doesn't have its source files, so Flare shows the file and line of each frame, but not the code around it.
- Inlined functions disappear. An optimized build loses the frame of a function that the compiler inlined, such as the body of an
async fn. When that happens, Flare puts the place where you made the report on top, with its file and line but without a function name. - Grouping uses line numbers. Flare groups Rust errors on the exception class, plus the file and line of the top application frame. A deploy that moves that line can start a new error in Flare.
When a report fails to send
Error reports go out one at a time from a background thread, so your app never waits on the network. A request that fails without an answer, like a connection error or a timeout, or with a 429, 502, 503 or 504 response, is tried again up to 3 times, with a longer pause before each new try. Any other failure drops the report. Set FLARE_DEBUG=true to see a line for every request on stderr, and the answer when one fails.
Every report goes into a queue, and the background thread sends it as soon as it can. For a panic, your code also waits until the report is sent, because the program might end after it. When main returns, #[flareapp::main] sends what's left. If your program ends some other way, call flareapp::flush first. It waits at most as long as you allow, and returns false when that wasn't enough:
Rust
use std::time::Duration;
flareapp::flush(Duration::from_secs(2));
std::process::exit(1);
Read more
- Client hooks: change or drop a report before it's sent.
- Adding custom context and identifying users: add what Flare can't know on its own.
- Release builds and debug files: readable stack traces from stripped binaries.