8
8
import Foundation
9
9
import NewRelic
10
10
11
- struct UtilOption {
11
+ struct UtilOption : Equatable {
12
+ static func == ( lhs: UtilOption , rhs: UtilOption ) -> Bool {
13
+ lhs. title == rhs. title
14
+ }
15
+
12
16
let title : String
13
17
let handler : ( ( ) -> Void )
14
18
}
@@ -38,6 +42,13 @@ class UtilViewModel {
38
42
options. append ( UtilOption ( title: " Set Attributes " , handler: { [ self ] in setAttributes ( ) } ) )
39
43
options. append ( UtilOption ( title: " Remove Attributes " , handler: { [ self ] in removeAttributes ( ) } ) )
40
44
options. append ( UtilOption ( title: " Crash Now! " , handler: { [ self ] in crash ( ) } ) )
45
+
46
+ // crash types
47
+ options. append ( UtilOption ( title: " Unhandled Exception Now! " , handler: { [ self ] in generateUncaughtException ( ) } ) )
48
+ options. append ( UtilOption ( title: " Stack Overflow Now! " , handler: { [ self ] in generateStackOverflow ( ) } ) )
49
+ options. append ( UtilOption ( title: " Fatal App Hang Now! " , handler: { [ self ] in generateFatalAppHang ( ) } ) )
50
+ options. append ( UtilOption ( title: " Raise NSException Now! " , handler: { [ self ] in throwNSException ( ) } ) )
51
+
41
52
options. append ( UtilOption ( title: " Record Error " , handler: { [ self ] in makeError ( ) } ) )
42
53
options. append ( UtilOption ( title: " Record Handled Exception " , handler: { triggerException. testing ( ) } ) )
43
54
@@ -62,11 +73,41 @@ class UtilViewModel {
62
73
options. append ( UtilOption ( title: " Shut down New Relic Agent " , handler: { [ self ] in shutDown ( ) } ) )
63
74
}
64
75
76
+ // Crash Types
65
77
func crash( ) {
66
78
// This will cause a crash to test the crash uploader, crash files may not get recorded if the debugger is running.
67
79
NewRelic . crashNow ( " New Relic intentionally crashed to test Utils " )
68
80
}
69
-
81
+
82
+ func generateUncaughtException( ) {
83
+ let someJson : Dictionary = [ " foo " : self ]
84
+ do {
85
+ let data = try JSONSerialization . data ( withJSONObject: someJson, options: . prettyPrinted)
86
+ print ( " Received data: %@ " , data)
87
+ } catch {
88
+
89
+ }
90
+ }
91
+
92
+ func generateStackOverflow( ) {
93
+ let items = [ " Hello world " ]
94
+ // Use if statement to remove warning about calling self through any path
95
+ if ( items [ 0 ] == " Hello world " ) {
96
+ generateStackOverflow ( )
97
+ }
98
+ print ( " items: %@ " , items)
99
+ }
100
+
101
+ func generateFatalAppHang( ) {
102
+ Thread . sleep ( forTimeInterval: 3 )
103
+ _exit ( 1 )
104
+ }
105
+
106
+ func throwNSException( ) {
107
+ NSException ( name: . internalInconsistencyException, reason: " example internalInconsistencyException " , userInfo: nil ) . raise ( )
108
+ }
109
+ // End test crashes.
110
+
70
111
func removeAttributes( ) {
71
112
if ( NewRelic . removeAllAttributes ( ) ) {
72
113
attributes = " "
0 commit comments