use std::ops::{Deref, DerefMut};
#[cfg(feature = "ureq")]
use ureq::AgentBuilder;
use super::payload::events::enums::Severity;
use super::payload::events::App;
use super::payload::events::Event;
use super::payload::events::Exception;
use super::payload::events::StackTrace;
use super::payload::notifier::Notifier;
use crate::common::builder::client_builder::ClientBuilderInternal;
#[cfg(feature = "reqwest")]
use super::reqwest::builder::decider::ReqwestErrorDecider;
#[cfg(feature = "ureq")]
use super::ureq::builder::blocking::ClientBuilderUreqErrorSync;
#[derive(Clone, Debug, PartialEq)]
pub struct ErrorClientBuilder {
pub(crate) builder_internal: ClientBuilderInternal,
pub(crate) notifier: Option<Notifier>,
pub(crate) events: Option<Vec<Event>>,
}
impl ErrorClientBuilder {
pub fn notifier(&self) -> Option<&Notifier> {
self.notifier.as_ref()
}
pub fn set_notifier(
&mut self,
notifier: impl Into<Option<Notifier>>,
) -> &mut Self {
self.notifier = notifier.into();
self
}
pub fn events(&self) -> Option<Vec<Event>> {
self.events.clone()
}
pub fn set_events(
&mut self,
events: impl Into<Option<Vec<Event>>>,
) -> &mut Self {
self.events = events.into();
self
}
pub fn configure<'a>(
&mut self,
api_key: impl Into<String>,
error_class: impl Into<String>,
error_message: impl Into<String>,
notifier_name: impl Into<String>,
notifier_version: impl Into<String>,
app_type: impl Into<Option<&'a str>>,
release_stage: impl Into<Option<&'a str>>,
) -> &mut Self {
let (notifier_name, notifier_version): (String, String) =
(notifier_name.into(), notifier_version.into());
let app_type_option = app_type.into().map(|item| item.to_string());
let release_stage_option =
release_stage.into().map(|item| item.to_string());
self.api_key = Some(api_key.into());
self.notifier = Some(Notifier {
name: notifier_name.clone(),
version: notifier_version.clone(),
url: "".into(),
dependencies: None,
});
let app_name = notifier_name;
let app_version = notifier_version;
let events: Vec<Event> = [Event {
exceptions: [Exception {
error_class: error_class.into(),
message: Some(error_message.into()),
stack_trace: StackTrace::grab(),
runtime_type: None,
}]
.into(),
breadcrumbs: None,
request: None,
threads: None,
context: None,
grouping_hash: None,
unhandled: None,
severity: Some(Severity::Error),
severity_reason: None,
project_packages: None,
user: None,
app: {
if app_type_option.is_some() || release_stage_option.is_some() {
Some(App {
id: Some(app_name),
version: Some(app_version),
version_code: None,
bundle_version: None,
code_bundle_id: None,
build_uuid: None,
release_stage: release_stage_option,
app_type: app_type_option,
dsym_uuids: None,
duration: None,
duration_in_foreground: None,
in_foreground: None,
is_launching: None,
binary_arch: None,
running_on_rosetta: None,
})
} else {
None
}
},
device: None,
session: None,
feature_flags: None,
meta_data: None,
}]
.into();
match self.events.as_mut() {
Some(current_events) => current_events.extend(events),
None => self.events = Some(events.into()),
}
self
}
}
impl ErrorClientBuilder {
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
#[cfg(feature = "reqwest")]
pub fn reqwest(&self) -> ReqwestErrorDecider {
ReqwestErrorDecider { builder: self.clone() }
}
#[cfg_attr(docsrs, doc(cfg(feature = "ureq")))]
#[cfg(feature = "ureq")]
pub fn ureq(&self) -> ClientBuilderUreqErrorSync {
ClientBuilderUreqErrorSync {
client_builder: AgentBuilder::new(),
options: self.options.clone(),
api_key: self.api_key.clone(),
payload_version: self
.payload_version_override
.clone()
.unwrap_or(self.payload_version.clone()),
notifier: self.notifier.clone(),
events: self.events.clone(),
}
}
}
impl Deref for ErrorClientBuilder {
type Target = ClientBuilderInternal;
fn deref(&self) -> &Self::Target {
&self.builder_internal
}
}
impl DerefMut for ErrorClientBuilder {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.builder_internal
}
}