# Client hooks


The Rust SDK has one hook, `before_send`, and it runs right before a report leaves your app. Use it to drop errors you don't care about, to strip sensitive data, or to add something that every report should carry.

## `before_send`

```rust
before_send: Fn(&mut Report) -> bool
```

The hook gets every report Flare builds: panics, errors returned from `main`, `report_error`, `report_message`, `anyhow` reports, and the `error!` lines you report through the [logging integrations](/docs/rust/logs/introduction#reporting-error-lines-as-errors). Return `false` and the report is dropped. Return `true` and it's sent, with whatever changes you made.

By the time the hook runs, Flare has already added the user, context and glows of the current [scope](/docs/rust/data-collection/scopes). The hook can read what the table below lists, but not the user, the context or the stack trace. Context you set in the hook wins over context with the same key from the scope.

Set the hook in a config function, and pass that function to the attribute:

```rust
// src/main.rs
fn flare_config() -> flareapp::Config {
    flareapp::config!().before_send(|report| {
        if report.exception_class() == "my_app::NotFound" {
            return false;
        }

        report.set_context("region", "eu-west-1");

        true
    })
}

#[flareapp::main(config = flare_config)]
fn main() {}
```

Start the function with `flareapp::config!()`, not `Config::new()`. The macro fills in the name and version of your crate, the git commit and the folder of your project, and `Config::new()` can't know any of those.

Not using the attribute? Pass the same config to `flareapp::init`:

```rust
let _flare = flareapp::init(flare_config());
```

## Reading and changing the report

| Method | What it does |
|---|---|
| `id()` | The tracking id of the report. |
| `exception_class()` | The exception class, like `my_app::PaymentFailed` or `Panic`. |
| `message()` | The message. |
| `handled()` | `true` when your code handled the error, `false` for a panic. |
| `set_exception_class(class)` | Changes the exception class. |
| `set_handled(handled)` | Marks the error as handled or unhandled. |
| `set_context(key, value)` | Adds a piece of custom context to this report. |
| `set_attribute(key, value)` | Adds an attribute to this report, like `thread.name`. |

Flare groups Rust errors on the exception class together with the file and line of the top application frame. Changing the class with `set_exception_class` therefore also changes which error a report ends up under. That makes it a handy way to merge reports that belong together, or to split the ones that don't.

## Dropping noisy errors

Some errors are part of normal operation and only fill up your error list. Match them on their class or message and return `false`:

```rust
fn flare_config() -> flareapp::Config {
    flareapp::config!().before_send(|report| {
        let is_closed_connection = report.exception_class().ends_with("io::error::Error")
            && report.message().contains("Broken pipe");

        !is_closed_connection
    })
}
```

The example matches the end of the class, because the compiler names this type `std::io::error::Error` before Rust 1.99 and `core::io::error::Error` from 1.99.

A dropped report never counts against your quota. `report_error` and `report_message` return `None` for it, because there's no report to look up in Flare.

## Removing sensitive data

Context you set on the report wins over context with the same key from the scope. So to blank out a piece of context that some part of your app adds:

```rust
use serde_json::Value;

fn flare_config() -> flareapp::Config {
    flareapp::config!().before_send(|report| {
        report.set_context("payment_card", Value::Null);

        true
    })
}
```

The SDK already censors the values of the `Authorization`, `Cookie`, `Set-Cookie`, `API-KEY`, `X-CSRF-TOKEN` and `X-XSRF-TOKEN` request headers. If you don't want to send the IP address of the client at all, turn it off on the config:

```rust
fn flare_config() -> flareapp::Config {
    flareapp::config!().censor_client_ips(true)
}
```

This also censors the headers that proxies use to pass on the IP address of the client, like `Forwarded`, `X-Forwarded-For`, `X-Real-IP` and `CF-Connecting-IP`. Behind a proxy, the client IP that Flare reads from the connection is the address of the proxy, and the real one is in those headers.

## Keep the hook fast

`before_send` runs for every report, on the thread that makes the report. For a panic, that's the thread that panicked. Keep it to quick checks and small changes. The hook must also be `Send + Sync`, so it can't hold on to a plain `RefCell` or `Rc`.

## Read more

- [Reporting errors](/docs/rust/errors/reporting-errors): every kind of report the hook can see.
- [Configuration](/docs/rust/reference/configuration): the other settings of the config you pass the hook to.
