1
1
import Sentry
2
2
import SwiftUI
3
3
4
- struct Options {
5
- static var dsnHash : String ?
6
- static var cacheDirPath : String ?
7
- }
8
-
9
4
@main
10
5
struct SwiftUITestSampleApp : App {
11
- init ( ) {
12
- SentrySDK . start { options in
13
- options. debug = true
14
- options. dsn = " https://6cc9bae94def43cab8444a99e0031c28@o447951.ingest.sentry.io/5428557 "
15
- Options . dsnHash = options. parsedDsn? . getHash ( )
16
- Options . cacheDirPath = options. cacheDirectoryPath
17
- }
18
- }
19
6
20
7
var body : some Scene {
21
8
WindowGroup {
@@ -25,38 +12,81 @@ struct SwiftUITestSampleApp: App {
25
12
}
26
13
27
14
struct ContentView : View {
15
+ @State private var errorMessage : String ?
16
+
28
17
var body : some View {
29
18
VStack {
30
19
Text ( " Welcome! " )
31
- Button ( " Close SDK" ) {
32
- SentrySDK . close ( )
20
+ Button ( " Start SDK" ) {
21
+ startSDK ( )
33
22
}
34
23
Button ( " Write Corrupted Envelope " ) {
35
- guard let dsnHash = Options . dsnHash else {
36
- fatalError ( " dsnHash can not be nil! " )
37
- }
38
-
39
- guard let cachePath = Options . cacheDirPath else {
40
- fatalError ( " cacheDirPath can not be nil! " )
41
- }
42
-
43
- let envelopePath = " \( cachePath) /io.sentry/ \( dsnHash) /envelopes/corrupted-envelope.json "
44
- let corruptedEnvelopeData = Data ( """
45
- { " event_id " : " 1990b5bc31904b7395fd07feb72daf1c " , " sdk " :{ " name " : " sentry.cocoa " , " version " : " 8.33.0 " }}
46
- { " type " : " test " , " length " :50}
47
- """ . utf8)
48
-
49
24
do {
50
- try corruptedEnvelopeData. write ( to: URL ( fileURLWithPath: envelopePath) )
51
- print ( " Corrupted envelope saved to: " + envelopePath)
25
+ errorMessage = nil // Clear any previous error
26
+ try writeCorruptedEnvelope ( )
27
+ } catch let error as WriteCorruptedEnvelopeError {
28
+ errorMessage = error. description
52
29
} catch {
53
- fatalError ( " Error while writing corrupted envelope to: " + envelopePath )
30
+ errorMessage = " Unknown error: \( error ) "
54
31
}
55
32
}
33
+
34
+ if let errorMessage = errorMessage {
35
+ Text ( " \( errorMessage) " )
36
+ . accessibilityIdentifier ( " errorMessage " )
37
+ }
56
38
}
57
39
}
58
40
}
59
41
42
+ private var sentryOptions : Options = {
43
+ let options = Options ( )
44
+ options. dsn = " https://6cc9bae94def43cab8444a99e0031c28@o447951.ingest.sentry.io/5428557 "
45
+ options. debug = true
46
+ return options
47
+ } ( )
48
+
49
+ private func startSDK( ) {
50
+ SentrySDK . start ( options: sentryOptions)
51
+ }
52
+
53
+ enum WriteCorruptedEnvelopeError : Error {
54
+ case dsn
55
+ case write( String )
56
+ }
57
+
58
+ extension WriteCorruptedEnvelopeError : CustomStringConvertible {
59
+ var description : String {
60
+ switch self {
61
+ case . dsn:
62
+ return " DSN hash is not available "
63
+ case . write( let s) :
64
+ return s
65
+ }
66
+ }
67
+ }
68
+
69
+ private func writeCorruptedEnvelope( ) throws {
70
+ guard let dsnHash = sentryOptions. parsedDsn? . getHash ( ) else {
71
+ throw WriteCorruptedEnvelopeError . dsn
72
+ }
73
+
74
+ let envelopeFolder = " \( sentryOptions. cacheDirectoryPath) /io.sentry/ \( dsnHash) /envelopes "
75
+ let envelopePath = " \( envelopeFolder) /corrupted-envelope.json "
76
+ let corruptedEnvelopeData = Data ( """
77
+ { " event_id " : " 1990b5bc31904b7395fd07feb72daf1c " , " sdk " :{ " name " : " sentry.cocoa " , " version " : " 8.33.0 " }}
78
+ { " type " : " test " , " length " :50}
79
+ """ . utf8)
80
+
81
+ do {
82
+ let fileManager = FileManager . default
83
+ try fileManager. createDirectory ( atPath: envelopeFolder, withIntermediateDirectories: true )
84
+ try corruptedEnvelopeData. write ( to: URL ( fileURLWithPath: envelopePath) , options: . atomic)
85
+ } catch {
86
+ throw WriteCorruptedEnvelopeError . write ( " Error while writing corrupted envelope to: \( envelopePath) error: \( error) " )
87
+ }
88
+ }
89
+
60
90
struct ContentView_Previews : PreviewProvider {
61
91
static var previews : some View {
62
92
ContentView ( )
0 commit comments