1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
use std::collections::HashMap;
use serde::Serialize;
#[cfg_attr(docsrs, doc(cfg(feature = "optional-builders")))]
#[cfg(feature = "optional-builders")]
use crate::common::builder::error::BuilderError;
use super::{
enums::Severity, App, Breadcrumb, Device, Exception, FeatureFlag, Request,
Session, SeverityReason, Thread, User,
};
/// Holds the structure for the `event` key of the BugSnag error-reporting API
/// payload.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::App;
/// use lemon_bugsnag_rs::error::payload::events::Severity;
///
/// let event = Event {
/// // Fill in fields as appropriate
/// app: Some(App::default()),
/// severity: Some(Severity::Error),
/// ..Event::default()
/// };
/// ```
#[derive(Debug, Default, Clone, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Event {
/// Holds information about the [application](App) that generated the event.
pub app: Option<App>,
/// A vector of [Breadcrumb]s providing additional information about the
/// code path which led to the event being reported.
pub breadcrumbs: Option<Vec<Breadcrumb>>,
/// Gives additional information about where the event occurred. For
/// example, this could be a filename and function name.
pub context: Option<String>,
/// Contains information about the [Device] hardware on which the event
/// occurred.
pub device: Option<Device>,
/// A vector of [Exception]s that are relevant to the event being reported.
pub exceptions: Vec<Exception>,
/// A vector of [FeatureFlag]s describing the state of various features
/// of the application which produced the event.
pub feature_flags: Option<Vec<FeatureFlag>>,
/// Custom identifier used to group events in the BugSnag API. Overrides
/// BugSnag's defaut grouping behavior.
pub grouping_hash: Option<String>,
/// A [HashMap] of key -> value pairs used to hold any additional
/// information about the event being reported that you would like to
/// record in BugSnag. The value should be a JSON string.
pub meta_data: Option<HashMap<String, String>>,
/// A vector of strings giving the names of packages that BugSnag should
/// consider part of the main application when processing stack traces.
pub project_packages: Option<Vec<String>>,
/// Information about the HTTP/S request that triggered the event. Only
/// relevant for web-base applications.
pub request: Option<Request>,
/// Details of the user [Session] that was in progress when the event
/// occurred.
pub session: Option<Session>,
/// Which [Severity] level applies to this event.
pub severity: Option<Severity>,
/// [Information](SeverityReason) about why the chosen [Severity] level
/// was applied to this event.
pub severity_reason: Option<SeverityReason>,
/// Information about the [Thread]s that were in use by the application
/// when the event occurred.
pub threads: Option<Vec<Thread>>,
/// False if the event was handled by the application, true otherwise.
pub unhandled: Option<bool>,
/// Information about the [User] who was affected by this event.
pub user: Option<User>,
}
#[cfg_attr(docsrs, doc(cfg(feature = "optional-builders")))]
#[cfg(feature = "optional-builders")]
impl Event {
/// Retrieve an [EventBuilder] for building an [Event].
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
///
/// let mut eb = Event::builder();
///
/// eb.set_unhandled(false)
/// .set_grouping_hash("1234");
/// // ... Set other fields as appropriate.
///
/// let event = eb.build();
/// ```
pub fn builder() -> EventBuilder {
EventBuilder::default()
}
}
/// Builder struct used to generate [Event]s.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::EventBuilder;
///
/// let mut eb = EventBuilder::default();
///
/// eb.set_context("Inside the EventBuilder doctest.");
/// // ... Cofigure other fields as needed.
///
/// let event = eb.build();
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "optional-builders")))]
#[cfg(feature = "optional-builders")]
#[derive(Default)]
pub struct EventBuilder {
/// Holds [Event] fields used by the builder.
error_event: Event,
/// Used to track whether the required Exceptions fields has
/// been configured before attempting to build and return
/// the [Exception].
exceptions: Option<Vec<Exception>>,
}
#[cfg_attr(docsrs, doc(cfg(feature = "optional-builders")))]
#[cfg(feature = "optional-builders")]
impl EventBuilder {
/// Set a vector of [Exception]s for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::Exception;
///
/// let mut exception = Exception::default();
/// exception.error_class = "doctest".to_string();
/// // Configure the rest of the Exception.
/// // ...
///
/// let mut eb = Event::builder();
/// eb.set_exceptions([exception].to_vec());
/// // With the convenience-intos feature disabled
/// // eb.set_exceptions([exception].to_vec());
/// ```
pub fn set_exceptions(
&mut self,
exceptions: impl Into<Option<Vec<Exception>>>,
) -> &mut Self {
self.exceptions = exceptions.into();
self
}
/// Set a vector of [Breadcrumb]s for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::Breadcrumb;
/// use lemon_bugsnag_rs::error::payload::events::enums::BreadcrumbType;
///
/// let mut breadcrumb = Breadcrumb {
/// timestamp: "doctest".to_string(),
/// name: "make-believe event breadcrumb".to_string(),
/// breadcrumb_type: BreadcrumbType::Navigation,
/// meta_data: None
/// };
///
/// let mut eb = Event::builder();
/// eb.set_breadcrumbs(breadcrumb);
/// // With the convenience-intos feature disabled
/// // eb.set_breadcrumbs([breadcrumb].to_vec());
/// ```
pub fn set_breadcrumbs(
&mut self,
breadcrumbs: impl Into<Option<Vec<Breadcrumb>>>,
) -> &mut Self {
self.error_event.breadcrumbs = breadcrumbs.into();
self
}
/// Set the [Request] information for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::Request;
///
/// let mut request = Request {
/// client_ip: Some("192.168.222.200".to_string()),
/// // Configure other fields as relevant
/// // ...
/// ..Request::default()
/// };
///
/// let mut eb = Event::builder();
/// eb.set_request(request);
/// ```
pub fn set_request(
&mut self,
request: impl Into<Option<Request>>,
) -> &mut Self {
self.error_event.request = request.into();
self
}
/// Set the [Thread] information for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::Thread;
///
/// let mut thread = Thread {
/// id: Some("Unique thread ID".to_string()),
/// name: Some("This is my name. There are many like it.".to_string()),
/// // Configure other fields as relevant
/// // ...
/// ..Thread::default()
/// };
///
/// let mut eb = Event::builder();
/// eb.set_threads(thread);
/// // With the convenience-intos feature disabled
/// // eb.set_threads([thread].to_vec());
/// ```
pub fn set_threads(
&mut self,
threads: impl Into<Option<Vec<Thread>>>,
) -> &mut Self {
self.error_event.threads = threads.into();
self
}
/// Set context information for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
///
/// let mut eb = Event::builder();
/// eb.set_context("This event happened in lemon_bugsnag_rs::error::payload::events::EventBuilder::set_context()");
/// ```
pub fn set_context<'a>(
&mut self,
context: impl Into<Option<&'a str>>,
) -> &mut Self {
self.error_event.context = context.into().map(|item| item.into());
self
}
/// Set the grouping hash for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
///
/// let mut eb = Event::builder();
/// eb.set_grouping_hash("My unique grouping identifier");
/// ```
pub fn set_grouping_hash<'a>(
&mut self,
grouping_hash: impl Into<Option<&'a str>>,
) -> &mut Self {
self.error_event.grouping_hash =
grouping_hash.into().map(|item| item.into());
self
}
/// Mark this event as either handled or unhandled.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
///
/// let mut eb = Event::builder();
/// // This event was handled by the application which is reporting it.
/// eb.set_unhandled(false);
/// // This event was not handled by the application which is reporting it.
/// // eb.set_unhandled(true);
/// ```
pub fn set_unhandled(
&mut self,
unhandled: impl Into<Option<bool>>,
) -> &mut Self {
self.error_event.unhandled = unhandled.into();
self
}
/// Set the [Severity] of this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::enums::Severity;
///
/// let mut eb = Event::builder();
/// eb.set_severity(Severity::Info);
/// ```
pub fn set_severity(
&mut self,
severity: impl Into<Option<Severity>>,
) -> &mut Self {
self.error_event.severity = severity.into();
self
}
/// Set the [SeverityReason] of this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::SeverityReason;
///
/// let severity_reason = SeverityReason::default();
/// // Configuration of severity_reason is ommitted as it includes
/// // multiple complex structs.
///
/// let mut eb = Event::builder();
/// eb.set_severity_reason(severity_reason);
/// ```
pub fn set_severity_reason(
&mut self,
severity_reason: impl Into<Option<SeverityReason>>,
) -> &mut Self {
self.error_event.severity_reason = severity_reason.into();
self
}
/// Set the list of packages included in your project.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
///
/// let mut eb = Event::builder();
/// eb.set_project_packages(
/// ["com.some.package",
/// "com.another.package"].to_vec()
/// );
/// ```
pub fn set_project_packages<'a>(
&mut self,
project_packages: impl Into<Option<Vec<&'a str>>>,
) -> &mut Self {
self.error_event.project_packages = project_packages
.into()
.map(|vec| vec.into_iter().map(|item| item.into()).collect());
self
}
/// Set the [User] information for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::User;
///
/// let user = User {
/// id: Some("8".to_string()),
/// name: Some("Mister Eight".to_string()),
/// email: Some("mr8@some.host".to_string())
/// };
///
/// let mut eb = Event::builder();
/// eb.set_user(user);
/// ```
pub fn set_user(&mut self, user: impl Into<Option<User>>) -> &mut Self {
self.error_event.user = user.into();
self
}
/// Set [App]lication information for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::App;
///
/// let app = App {
/// id: Some("my-app_build-2024-12-20".to_string()),
/// // Configure the rest of the App struct
/// // ...
/// ..App::default()
/// };
///
/// let mut eb = Event::builder();
/// eb.set_app(app);
/// ```
pub fn set_app(&mut self, app: impl Into<Option<App>>) -> &mut Self {
self.error_event.app = app.into();
self
}
/// Set the [Device] information for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::Device;
///
/// let device = Device {
/// id: Some("some_id".to_string()),
/// hostname: Some("my-device.host".to_string()),
/// // Configure the rest of the Device fields as required
/// // ...
/// ..Device::default()
/// };
///
/// let mut eb = Event::builder();
/// eb.set_device(device);
/// ```
pub fn set_device(
&mut self,
device: impl Into<Option<Device>>,
) -> &mut Self {
self.error_event.device = device.into();
self
}
/// Set the [Session] information for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::Session;
/// use chrono::{DateTime, Utc, SecondsFormat};
///
/// let timestamp = Utc::now();
/// let session = Session {
/// id: "session_id".to_string(),
/// started_at: timestamp,
/// handled: 2,
/// unhandled: 0
/// };
///
/// let mut eb = Event::builder();
/// eb.set_session(session);
/// ```
pub fn set_session(
&mut self,
session: impl Into<Option<Session>>,
) -> &mut Self {
self.error_event.session = session.into();
self
}
/// Set the feaature flags for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::FeatureFlag;
///
/// let mut eb = Event::builder();
/// eb.set_feature_flags(
/// [
/// FeatureFlag {
/// feature_flag: "overview dashboard".to_string(),
/// variant: None
/// },
/// FeatureFlag {
/// feature_flag: "popup alerts".to_string(),
/// variant: Some("require agreement".to_string())
/// }
///
/// ].to_vec()
/// );
/// ```
pub fn set_feature_flags(
&mut self,
feature_flags: impl Into<Option<Vec<FeatureFlag>>>,
) -> &mut Self {
self.error_event.feature_flags = feature_flags.into();
self
}
/// Set metadata for this event.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use std::collections::HashMap;
///
/// let mut metadata: HashMap<&str, &str> = HashMap::new();
/// metadata.insert("environment", r#"{"OS": "Linux", "RAM": "value2""#);
/// metadata.insert("environment", r#"{"key": "value", "key2": "value2""#);
///
/// let mut eb = Event::builder();
/// eb.set_grouping_hash("My unique grouping identifier");
/// ```
pub fn set_meta_data<'a>(
&mut self,
meta_data: impl Into<Option<HashMap<&'a str, &'a str>>>,
) -> &mut Self {
self.error_event.meta_data = meta_data.into().map(|item| {
item.into_iter()
.map(|(key, value)| (key.into(), value.into()))
.collect()
});
self
}
/// Build the Event struct.
///
/// ```
/// use lemon_bugsnag_rs::error::payload::events::Event;
/// use lemon_bugsnag_rs::error::payload::events::User;
///
/// let mut eb = Event::builder();
/// // Set fields as desired.
/// eb.set_user(
/// User {
/// id: Some("A1000".to_string()),
/// name: Some("U. Ser".to_string()),
/// email: Some("u.ser@local.mail".to_string())
/// }
/// ).set_project_packages(
/// [
/// "com.andgetme.package",
/// "com.andgetme.package2"
/// ].to_vec()
/// ) /* ... */;
///
/// // For the sake of bevity, we are ommitting the creation
/// // of real exceptions for this example.
/// eb.set_exceptions(Vec::new());
///
/// // In real code, you will want to handle the possibility
/// // of a failed unwrap().
/// let event = eb.build().unwrap();
/// ```
pub fn build(&mut self) -> Result<Event, BuilderError> {
let mut missing_fields: Vec<String> = Vec::new();
if self.exceptions.is_none() {
missing_fields.push("exceptions".to_string());
}
if missing_fields.len() > 0 {
Err(BuilderError::MissingField {
context: "EventBuilder".to_string(),
fields: missing_fields,
})
} else {
self.error_event.exceptions = self.exceptions.clone().unwrap();
Ok(self.error_event.clone())
}
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "convenience-intos")))]
#[cfg(feature = "convenience-intos")]
impl Into<Vec<Event>> for Event {
fn into(self) -> Vec<Event> {
[self].to_vec()
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "convenience-intos")))]
#[cfg(feature = "convenience-intos")]
impl Into<Option<Vec<Event>>> for Event {
fn into(self) -> Option<Vec<Event>> {
Some([self].into())
}
}
#[cfg(test)]
mod test {
#[cfg(feature = "convenience-intos")]
mod test_convenience_intos {
use crate::error::payload::events::Event;
#[test]
fn test_event_into_vec_event() {
let event = Event::default();
let vec_event: Vec<Event> = event.clone().into();
assert_eq!(vec_event, [event].to_vec());
}
#[test]
fn test_event_into_option_vec_event() {
let event = Event::default();
let vec_event: Option<Vec<Event>> = event.clone().into();
assert_eq!(vec_event, Some([event].into()));
}
}
/// There is debate about whether or not it is worthwhile to write tests
/// for getters and setters, but we found it helpful during development.
#[cfg(feature = "optional-builders")]
mod error_event_builders {
use crate::error::payload::events::{
device::Device,
enums::{BinaryArch, BreadcrumbType, SeverityReasonType},
App, Breadcrumb, Request, RuntimeVersion, Session, SeverityReason,
SeverityReasonAttribute, Thread, User,
};
use crate::error::payload::events::{
enums::{RuntimeType, Severity},
Exception, FeatureFlag, StackTrace, StackTraceCode,
};
use crate::test::test_error_utility::get_event_builder;
use crate::test::test_error_utility::{
extract_event_at_index, get_error_client_builder,
};
use chrono::Timelike;
use std::collections::HashMap;
#[test]
pub fn test_error_payload_events_builder_metadata() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let mut metadata = HashMap::new();
metadata.insert("key_one", "value 1");
metadata.insert("key_two", "value 2");
event_builder.set_meta_data(metadata.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(
event.meta_data.clone().unwrap(),
metadata
.into_iter()
.map(|(key, val)| { (key.into(), val.into()) })
.collect()
);
event_builder.set_meta_data(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.meta_data.is_none())
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_feature_flag() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let feature_flag_base = FeatureFlag {
feature_flag: "Feature Flag Test".into(),
variant: Some("Feature Flag Variant Test".into()),
};
event_builder
.set_feature_flags(Some([feature_flag_base.clone()].into()));
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
let feature_flags = event.feature_flags.clone().unwrap();
let feature_flag = feature_flags.get(0).unwrap();
assert_eq!(feature_flag, &feature_flag_base);
event_builder.set_feature_flags(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.feature_flags.is_none())
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_session() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_event_session = Session {
id: "2".to_string(),
started_at: chrono::Utc::now().with_minute(10).unwrap(),
handled: 32,
unhandled: 64,
};
event_builder.set_session(test_event_session.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(event.session.clone().unwrap(), test_event_session);
event_builder.set_session(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.session.is_none())
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_device() {
let runtime_version = RuntimeVersion {
android_api_level: "android_api_level".to_string().into(),
bottle: "bottle".to_string().into(),
celery: "Celery".to_string().into(),
clang_version: "clang_version".to_string().into(),
cocos2dx: "cocos2dx".to_string().into(),
delayed_job: "delayed_job".to_string().into(),
django: "django".to_string().into(),
dotnet: "dotnet".to_string().into(),
dotnet_api_compatibility: "dotnet_api_compatability"
.to_string()
.into(),
dotnet_clr: "dotnet_clr".to_string().into(),
dotnet_scripting_runtime: "dotnet_scripting_runtime"
.to_string()
.into(),
electron: "electron".to_string().into(),
event_machine: "event_machine".to_string().into(),
expo_app: "expo_app".to_string().into(),
expo_sdk: "expo_sdk".to_string().into(),
flask: "flask".to_string().into(),
gin: "gin".to_string().into(),
go: "go".to_string().into(),
java_type: "java_type".to_string().into(),
java_version: "java_version".to_string().into(),
jruby: "jruby".to_string().into(),
laravel: "laravel".to_string().into(),
lumen: "lumen".to_string().into(),
magento: "magento".to_string().into(),
mailman: "mailman".to_string().into(),
martini: "martini".to_string().into(),
negroni: "negroni".to_string().into(),
node: "node".to_string().into(),
os_build: "os_build".to_string().into(),
php: "php".to_string().into(),
python: "python".to_string().into(),
que: "que".to_string().into(),
rack: "rack".to_string().into(),
rails: "rails".to_string().into(),
rake: "rake".to_string().into(),
react_native: "react_native".to_string().into(),
react_native_js_engine: "react_native_js_engine"
.to_string()
.into(),
resque: "resque".to_string().into(),
revel: "revel".to_string().into(),
ruby: "ruby".to_string().into(),
shoryoken: "shoryoken".to_string().into(),
sidekiq: "sidekiq".to_string().into(),
silex: "silex".to_string().into(),
sinatra: "sinatra".to_string().into(),
spring_boot: "spring_boot".to_string().into(),
spring_framework: "spring_framework".to_string().into(),
swift: "swift".to_string().into(),
symfony: "symfony".to_string().into(),
tornado: "tornado".to_string().into(),
unity: "unity".to_string().into(),
unity_scripting_backend: "unity_scripting_backend"
.to_string()
.into(),
wordpress: "wordpress".to_string().into(),
};
let test_device = Device {
hostname: Some("hostname".into()),
id: Some("id".into()),
manufacturer: Some("manufacturer".into()),
model: Some("model".into()),
model_number: Some("mode number".into()),
os_name: Some("os name".into()),
os_version: Some("os version".into()),
free_memory: Some(32768),
total_memory: Some(65556),
free_disk: Some(2000000000),
browser_name: Some("Godzilla".into()),
browser_version: Some("2.2 Tokyo".into()),
jailbroken: Some(true),
orientation: Some("Vertical".into()),
time: Some("00:02:04UTC".into()),
cpu_abi: Some(["What even is this?".into()].into()),
runtime_versions: runtime_version.into(),
mac_catalyst_ios_version: Some("ios version".into()),
};
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
client_builder.set_events(Some(
[event_builder
.set_device(test_device.clone())
.build()
.unwrap()]
.into(),
));
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(test_device, event.device.clone().unwrap());
event_builder.set_device(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.device.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_app() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_app = App {
id: "1".to_string().into(),
version: "1.0".to_string().into(),
version_code: 1.into(),
bundle_version: "1.0".to_string().into(),
code_bundle_id: "20".to_string().into(),
build_uuid: "10".to_string().into(),
release_stage: "15".to_string().into(),
app_type: "rust".to_string().into(),
dsym_uuids: ["acx".into(), "poi".into()].to_vec().into(),
duration: 90000000.into(),
duration_in_foreground: 800000.into(),
in_foreground: true.into(),
is_launching: true.into(),
binary_arch: BinaryArch::Amd64.into(),
running_on_rosetta: false.into(),
};
event_builder.set_app(test_app.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(test_app, event.app.clone().unwrap());
event_builder.set_app(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.app.is_none())
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_user() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_user = User {
id: "1".to_string().into(),
name: "John Doe".to_string().into(),
email: "jdoe@email.com".to_string().into(),
};
event_builder.set_user(test_user.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(test_user, event.user.clone().unwrap());
event_builder.set_user(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.user.is_none())
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_project_packages() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let project_package = ["package 1", "package 2"].to_vec();
event_builder.set_project_packages(project_package.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(
event.project_packages.unwrap(),
project_package
.into_iter()
.map(|item| item.into())
.collect::<Vec<String>>()
);
event_builder.set_project_packages(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.project_packages.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_severity_reason() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let severity_reason_attribute = SeverityReasonAttribute {
error_class: "Car".to_string().into(),
error_type: "Sparkplug".to_string().into(),
exception_class: "Engine".to_string().into(),
framework: "Unibody".to_string().into(),
level: "Coupe".to_string().into(),
signal_type: "Left".to_string().into(),
violation_type: "Moving".to_string().into(),
};
let test_severity_reason = SeverityReason {
severity_reason_type: SeverityReasonType::AppHang.into(),
attributes: severity_reason_attribute.clone().into(),
unhandled_overridden: true.into(),
};
event_builder.set_severity_reason(test_severity_reason.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(event.severity_reason, test_severity_reason.into());
event_builder.set_severity_reason(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert_eq!(event.severity_reason, None);
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_severity() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_event_severity = Severity::Error;
event_builder.set_severity(test_event_severity.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(test_event_severity, event.severity.unwrap().clone());
event_builder.set_severity(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.severity.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_unhandled() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_event_unhandled = true;
event_builder.set_unhandled(test_event_unhandled);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(test_event_unhandled, event.unhandled.unwrap());
event_builder.set_unhandled(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.unhandled.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_grouping_hash() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_event_grouping_hash = "Group";
event_builder.set_grouping_hash(test_event_grouping_hash);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(test_event_grouping_hash, event.grouping_hash.unwrap());
event_builder.set_grouping_hash(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.grouping_hash.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_context() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_event_context = "Random Context";
event_builder.set_context(test_event_context);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(test_event_context, event.context.unwrap());
event_builder.set_context(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.context.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_threads() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let stack_trace_code: StackTraceCode = (&[
(10 as usize, "PRINT \"HELLO WORLD\""),
(20 as usize, "PRINT GOTO 10"),
][..])
.into();
let stack_trace = StackTrace {
file: "Filename".to_string(),
line_number: 12,
column_number: 24.into(),
method: "Function".to_string(),
in_project: true.into(),
code: stack_trace_code.clone().into(),
frame_address: "Frame Address".to_string().into(),
load_address: "Load Address".to_string().into(),
is_lr: true.into(),
is_pc: false.into(),
symbol_address: "Symbol Address".to_string().into(),
macho_file: "Macho File".to_string().into(),
macho_load_address: "Macho Load Address".to_string().into(),
macho_uuid: "Macho UUID".to_string().into(),
macho_vm_address: "Macho VM Adress".to_string().into(),
code_identifier: "Code Identifier".to_string().into(),
};
let test_event_threads: Vec<Thread> = [Thread {
id: "2".to_string().into(),
name: "Thread name".to_string().into(),
error_reporting_thread: true.into(),
stack_trace: vec![stack_trace].into(),
state: "State".to_string().into(),
runtime_type: RuntimeType::BrowserJs.into(),
}]
.into();
event_builder.set_threads(test_event_threads.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(event.threads.unwrap(), test_event_threads);
event_builder.set_threads(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.threads.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_request() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_headers: HashMap<String, String> =
[("Header-name".to_string(), "Header value".to_string())]
.into();
let test_request = Request {
client_ip: "Client IP".to_string().into(),
headers: test_headers.into(),
http_method: "Http Method".to_string().into(),
url: "Url".to_string().into(),
referer: "".to_string().into(),
};
event_builder.set_request(test_request.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!(event.request.unwrap(), test_request);
event_builder.set_request(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.request.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_breadcrumbs() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let test_metadata: HashMap<String, String> = [
("Metadata key 1".to_string(), "Metadata value 1".to_string()),
("Metadata key 2".to_string(), "Metadata value 2".to_string()),
]
.into();
let test_breadcrumb = Breadcrumb {
timestamp: "01:02:03".to_string(),
name: "Breadcrumb".to_string(),
breadcrumb_type: BreadcrumbType::Request,
meta_data: test_metadata.into(),
};
event_builder.set_breadcrumbs([test_breadcrumb.clone()].to_vec());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let mut event = extract_event_at_index(&client_builder, 0);
assert_eq!([test_breadcrumb].to_vec(), event.breadcrumbs.unwrap());
event_builder.set_breadcrumbs(None);
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
event = extract_event_at_index(&client_builder, 0);
assert!(event.breadcrumbs.is_none());
}
#[cfg(feature = "optional-builders")]
#[test]
pub fn test_error_payload_events_builder_exceptions() {
let mut client_builder = get_error_client_builder();
let mut event_builder = get_event_builder();
let mut test_code_map: HashMap<usize, String> = HashMap::new();
test_code_map.insert(1, "Error".into());
test_code_map.insert(2, "Another Error".to_string());
let test_stack_trace_code: StackTraceCode =
test_code_map.clone().into();
let test_stack_trace = StackTrace {
file: "Filename".to_string(),
line_number: 12,
column_number: 24.into(),
method: "Function".to_string(),
in_project: true.into(),
code: test_stack_trace_code.clone().into(),
frame_address: "Frame Address".to_string().into(),
load_address: "Load Address".to_string().into(),
is_lr: true.into(),
is_pc: false.into(),
symbol_address: "Symbol Address".to_string().into(),
macho_file: "Macho File".to_string().into(),
macho_load_address: "Macho Load Address".to_string().into(),
macho_uuid: "Macho UUID".to_string().into(),
macho_vm_address: "Macho VM Adress".to_string().into(),
code_identifier: "Code Identifier".to_string().into(),
};
let exception = Exception {
error_class: "Error Class".to_string(),
message: "Message".to_string().into(),
stack_trace: [test_stack_trace.into()].into(),
runtime_type: RuntimeType::BrowserJs.into(),
};
event_builder.set_exceptions(exception.clone());
client_builder
.set_events([event_builder.build().unwrap()].to_vec());
let event = extract_event_at_index(&client_builder, 0);
assert_eq!([exception].to_vec(), event.exceptions);
}
}
}