Skip to content

Commit 418eef4

Browse files
committed
Auto merge of #3981 - Turbo87:sentry, r=JohnTitor
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.
2 parents 70cae90 + d19d28c commit 418eef4

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/sentry.rs

Lines changed: 18 additions & 15 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,22 +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 mut opts = ClientOptions::from(dsn);
20-
opts.environment = Some(
21-
dotenv::var("SENTRY_ENV_API")
22-
.map(Cow::Owned)
23-
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"),
24-
);
15+
.expect("SENTRY_DSN_API is not a valid Sentry DSN value");
2516

26-
opts.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+
});
2722

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

0 commit comments

Comments
 (0)