Skip to content

Commit d19d28c

Browse files
committed
sentry: Simplify init() function
`sentry::init()` already expects an `Option<Dsn>` so there is no reason for us to only call it if the env var exists. This reduces the nesting and simplfies the big `.map()` implementation.
1 parent d684b01 commit d19d28c

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/sentry.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use sentry::{ClientInitGuard, ClientOptions, IntoDsn};
2-
use std::borrow::Cow;
32

43
/// Initializes the Sentry SDK from the environment variables.
54
///
@@ -9,28 +8,26 @@ use std::borrow::Cow;
98
///
109
/// `HEROKU_SLUG_COMMIT`, if present, will be used as the `release` property
1110
/// on all events.
12-
#[must_use]
13-
pub fn init() -> Option<ClientInitGuard> {
14-
dotenv::var("SENTRY_DSN_API")
11+
pub fn init() -> ClientInitGuard {
12+
let dsn = dotenv::var("SENTRY_DSN_API")
1513
.ok()
1614
.into_dsn()
17-
.expect("SENTRY_DSN_API is not a valid Sentry DSN value")
18-
.map(|dsn| {
19-
let environment = Some(
20-
dotenv::var("SENTRY_ENV_API")
21-
.map(Cow::Owned)
22-
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"),
23-
);
15+
.expect("SENTRY_DSN_API is not a valid Sentry DSN value");
2416

25-
let release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into);
17+
let environment = dsn.as_ref().map(|_| {
18+
dotenv::var("SENTRY_ENV_API")
19+
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API")
20+
.into()
21+
});
2622

27-
let opts = ClientOptions {
28-
dsn: Some(dsn),
29-
environment,
30-
release,
31-
..Default::default()
32-
};
23+
let release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into);
3324

34-
sentry::init(opts)
35-
})
25+
let opts = ClientOptions {
26+
dsn,
27+
environment,
28+
release,
29+
..Default::default()
30+
};
31+
32+
sentry::init(opts)
3633
}

0 commit comments

Comments
 (0)