Skip to content

Commit 42ff5d1

Browse files
authored
ref: Deprecate public fields on Integrations (#267)
1 parent cd549de commit 42ff5d1

File tree

8 files changed

+67
-65
lines changed

8 files changed

+67
-65
lines changed

sentry-anyhow/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ all-features = true
1717
[dependencies]
1818
sentry-core = { version = "0.20.1", path = "../sentry-core" }
1919
anyhow = "1.0.30"
20+
21+
[dev-dependencies]
22+
sentry = { version = "0.20.1", path = "../sentry", default-features = false, features = ["test"] }

sentry-anyhow/src/lib.rs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@
33
//! # Example
44
//!
55
//! ```no_run
6-
//! # fn function_that_might_fail() -> anyhow::Result<()> { Ok(()) }
7-
//! use sentry_anyhow::capture_anyhow;
8-
//! # fn test() -> anyhow::Result<()> {
9-
//! let result = match function_that_might_fail() {
10-
//! Ok(result) => result,
11-
//! Err(err) => {
12-
//! capture_anyhow(&err);
13-
//! return Err(err);
14-
//! }
15-
//! };
16-
//! # Ok(()) }
6+
//! use sentry_anyhow::{capture_anyhow, AnyhowIntegration};
7+
//!
8+
//! fn function_that_might_fail() -> anyhow::Result<()> {
9+
//! Err(anyhow::anyhow!("some kind of error"))
10+
//! }
11+
//!
12+
//! let _sentry =
13+
//! sentry::init(sentry::ClientOptions::new().add_integration(AnyhowIntegration));
14+
//!
15+
//! if let Err(err) = function_that_might_fail() {
16+
//! capture_anyhow(&err);
17+
//! }
1718
//! ```
1819
1920
#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
2021
#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]
2122
#![warn(missing_docs)]
2223
#![deny(unsafe_code)]
2324

24-
use std::error::Error;
25-
use std::fmt;
26-
2725
use sentry_core::types::Uuid;
2826
use sentry_core::{ClientOptions, Hub, Integration};
2927

@@ -32,7 +30,7 @@ use sentry_core::{ClientOptions, Hub, Integration};
3230
pub struct AnyhowIntegration;
3331

3432
impl AnyhowIntegration {
35-
/// Creates a new Failure Integration.
33+
/// Creates a new anyhow Integration.
3634
pub fn new() -> Self {
3735
Self::default()
3836
}
@@ -57,36 +55,13 @@ pub fn capture_anyhow(e: &anyhow::Error) -> Uuid {
5755

5856
/// Hub extension methods for working with `anyhow`.
5957
pub trait AnyhowHubExt {
60-
/// Captures an `anyhow::Error` on a specific hub.
58+
/// Captures an [`anyhow::Error`] on a specific hub.
6159
fn capture_anyhow(&self, e: &anyhow::Error) -> Uuid;
6260
}
6361

6462
impl AnyhowHubExt for Hub {
6563
fn capture_anyhow(&self, e: &anyhow::Error) -> Uuid {
66-
self.capture_error(&AnyhowError(e))
67-
}
68-
}
69-
70-
// `anyhow::Error` itself does not impl `std::error::Error`, because it would
71-
// be incoherent. This can be worked around by wrapping it in a newtype
72-
// which impls `std::error::Error`.
73-
// Code adopted from: https://github.com/dtolnay/anyhow/issues/63#issuecomment-590983511
74-
struct AnyhowError<'a>(&'a anyhow::Error);
75-
76-
impl fmt::Debug for AnyhowError<'_> {
77-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
78-
self.0.fmt(fmt)
79-
}
80-
}
81-
82-
impl fmt::Display for AnyhowError<'_> {
83-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
84-
self.0.fmt(fmt)
85-
}
86-
}
87-
88-
impl Error for AnyhowError<'_> {
89-
fn source(&self) -> Option<&(dyn Error + 'static)> {
90-
self.0.source()
64+
let e: &dyn std::error::Error = e.as_ref();
65+
self.capture_error(e)
9166
}
9267
}

sentry-contexts/src/integration.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ use crate::utils::{device_context, os_context, rust_context, server_name};
1313
/// [Contexts Interface]: https://develop.sentry.dev/sdk/event-payloads/contexts/
1414
#[derive(Debug)]
1515
pub struct ContextIntegration {
16-
/// Add `os` context, enabled by default.
17-
pub add_os: bool,
18-
/// Add `rust` context, enabled by default.
19-
pub add_rust: bool,
20-
/// Add `device` context, enabled by default.
21-
pub add_device: bool,
16+
add_os: bool,
17+
add_rust: bool,
18+
add_device: bool,
2219
}
2320

2421
impl Default for ContextIntegration {
@@ -36,6 +33,23 @@ impl ContextIntegration {
3633
pub fn new() -> Self {
3734
Self::default()
3835
}
36+
37+
/// Add `os` context, enabled by default.
38+
pub fn add_os(mut self, add_os: bool) -> Self {
39+
self.add_os = add_os;
40+
self
41+
}
42+
/// Add `rust` context, enabled by default.
43+
pub fn add_rust(mut self, add_rust: bool) -> Self {
44+
self.add_rust = add_rust;
45+
self
46+
}
47+
48+
/// Add `device` context, enabled by default.
49+
pub fn add_device(mut self, add_device: bool) -> Self {
50+
self.add_device = add_device;
51+
self
52+
}
3953
}
4054

4155
impl Integration for ContextIntegration {

sentry-contexts/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
//! # Examples
1010
//!
1111
//! ```
12-
//! let integration = sentry_contexts::ContextIntegration {
13-
//! add_os: false,
14-
//! ..Default::default()
15-
//! };
16-
//! let _sentry = sentry::init(sentry::ClientOptions::default().add_integration(integration));
12+
//! let integration = sentry_contexts::ContextIntegration::new().add_os(false);
13+
//! let _sentry = sentry::init(sentry::ClientOptions::new().add_integration(integration));
1714
//! ```
1815
//!
1916
//! [Contexts Interface]: https://develop.sentry.dev/sdk/event-payloads/contexts/

sentry-core/src/hub.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use std::thread;
99
use std::time::Duration;
1010

1111
use crate::protocol::{Breadcrumb, Event, Level, SessionStatus};
12-
use crate::session::Session;
1312
use crate::types::Uuid;
1413
use crate::{event_from_error, Integration, IntoBreadcrumbs, Scope, ScopeGuard};
1514
#[cfg(feature = "client")]
16-
use crate::{scope::Stack, Client, Envelope};
15+
use crate::{scope::Stack, session::Session, Client, Envelope};
1716

1817
#[cfg(feature = "client")]
1918
lazy_static::lazy_static! {

sentry-core/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ mod hub;
6363
mod integration;
6464
mod intodsn;
6565
mod scope;
66-
mod session;
6766
mod transport;
6867

6968
// public api or exports from this crate
@@ -82,6 +81,8 @@ pub use crate::transport::{Transport, TransportFactory};
8281
#[cfg(feature = "client")]
8382
mod client;
8483
#[cfg(feature = "client")]
84+
mod session;
85+
#[cfg(feature = "client")]
8586
pub use crate::client::Client;
8687

8788
// test utilities

sentry-debug-images/src/integration.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ use sentry_core::{ClientOptions, Integration};
55

66
/// The Sentry Debug Images Integration.
77
pub struct DebugImagesIntegration {
8-
/// A custom filter for which Events should get debug images.
9-
pub filter: Box<dyn Fn(&Event<'static>) -> bool + Send + Sync>,
8+
filter: Box<dyn Fn(&Event<'_>) -> bool + Send + Sync>,
109
}
1110

1211
impl DebugImagesIntegration {
1312
/// Creates a new Debug Images Integration.
1413
pub fn new() -> Self {
1514
Self::default()
1615
}
16+
17+
/// Sets a custom filter function.
18+
///
19+
/// The filter specified which [`Event`]s should get debug images.
20+
pub fn filter<F>(mut self, filter: F) -> Self
21+
where
22+
F: Fn(&Event<'_>) -> bool + Send + Sync + 'static,
23+
{
24+
self.filter = Box::new(filter);
25+
self
26+
}
1727
}
1828

1929
impl Default for DebugImagesIntegration {

sentry-debug-images/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
//! The Sentry Debug Images Integration.
22
//!
3-
//! The `DebugImagesIntegration` adds metadata about the loaded shared libraries
4-
//! to Sentry `Event`s.
3+
//! The [`DebugImagesIntegration`] adds metadata about the loaded shared
4+
//! libraries to Sentry [`Event`]s.
5+
//!
6+
//! This Integration only works on Unix-like OSs right now. Support for Windows
7+
//! will be added in the future.
58
//!
69
//! # Configuration
710
//!
8-
//! The integration by default attaches this information to all Events, but a
9-
//! custom filter can be defined as well.
11+
//! The integration by default attaches this information to all [`Event`]s, but
12+
//! a custom filter can be defined as well.
1013
//!
1114
//! ```
1215
//! use sentry_core::Level;
13-
//! let integration = sentry_debug_images::DebugImagesIntegration {
14-
//! filter: Box::new(|event| event.level >= Level::Warning),
15-
//! ..Default::default()
16-
//! };
16+
//! let integration = sentry_debug_images::DebugImagesIntegration::new()
17+
//! .filter(|event| event.level >= Level::Warning);
1718
//! ```
19+
//!
20+
//! [`Event`]: sentry_core::protocol::Event
1821
1922
#![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")]
2023
#![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")]

0 commit comments

Comments
 (0)