Skip to content

Commit 8bdf2b3

Browse files
bsneedBrandon Sneed
andauthored
Better handle errors where conversion occurs. (#11)
Co-authored-by: Brandon Sneed <[email protected]>
1 parent 18a7522 commit 8bdf2b3

File tree

8 files changed

+57
-42
lines changed

8 files changed

+57
-42
lines changed

Segment.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
46A018D425E6C9C200F9CCD8 /* LinuxUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018D325E6C9C200F9CCD8 /* LinuxUtils.swift */; };
3535
46A018DA25E97FDF00F9CCD8 /* AppleUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018D925E97FDF00F9CCD8 /* AppleUtils.swift */; };
3636
46A018EE25E9A74F00F9CCD8 /* VendorSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018ED25E9A74F00F9CCD8 /* VendorSystem.swift */; };
37-
46E382E5264C738100BA2502 /* LateInit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E382E4264C738100BA2502 /* LateInit.swift */; };
37+
46E382E72654429A00BA2502 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E382E62654429A00BA2502 /* Utils.swift */; };
3838
46FE4C9225A3EE90003A7362 /* Lifecycle_.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FE4C9125A3EE90003A7362 /* Lifecycle_.swift */; };
3939
46FE4C9725A3F35E003A7362 /* macOSLifecycleMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FE4C9625A3F35E003A7362 /* macOSLifecycleMonitor.swift */; };
4040
46FE4C9C25A3F41C003A7362 /* LinuxLifecycleMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FE4C9B25A3F41C003A7362 /* LinuxLifecycleMonitor.swift */; };
@@ -139,7 +139,7 @@
139139
46A018D325E6C9C200F9CCD8 /* LinuxUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxUtils.swift; sourceTree = "<group>"; };
140140
46A018D925E97FDF00F9CCD8 /* AppleUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleUtils.swift; sourceTree = "<group>"; };
141141
46A018ED25E9A74F00F9CCD8 /* VendorSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VendorSystem.swift; sourceTree = "<group>"; };
142-
46E382E4264C738100BA2502 /* LateInit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LateInit.swift; sourceTree = "<group>"; };
142+
46E382E62654429A00BA2502 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
143143
46FE4C9125A3EE90003A7362 /* Lifecycle_.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lifecycle_.swift; sourceTree = "<group>"; };
144144
46FE4C9625A3F35E003A7362 /* macOSLifecycleMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = macOSLifecycleMonitor.swift; sourceTree = "<group>"; };
145145
46FE4C9B25A3F41C003A7362 /* LinuxLifecycleMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxLifecycleMonitor.swift; sourceTree = "<group>"; };
@@ -324,7 +324,7 @@
324324
46FE4CDF25A53FAD003A7362 /* Storage.swift */,
325325
4621080B2605332D00EBC4A8 /* KeyPath.swift */,
326326
46022770261F7A4800A9E913 /* Atomic.swift */,
327-
46E382E4264C738100BA2502 /* LateInit.swift */,
327+
46E382E62654429A00BA2502 /* Utils.swift */,
328328
);
329329
path = Utilities;
330330
sourceTree = "<group>";
@@ -559,11 +559,11 @@
559559
96C33AB1258961F500F3D538 /* Settings.swift in Sources */,
560560
969A534325B10324009227D9 /* iOSAppBackground.swift in Sources */,
561561
46FE4C9225A3EE90003A7362 /* Lifecycle_.swift in Sources */,
562+
46E382E72654429A00BA2502 /* Utils.swift in Sources */,
562563
A31A16B225781CB400C9CDDF /* JSON.swift in Sources */,
563564
46022771261F7A4800A9E913 /* Atomic.swift in Sources */,
564565
A3471FBE256487F000965480 /* Configuration.swift in Sources */,
565566
OBJ_23 /* Analytics.swift in Sources */,
566-
46E382E5264C738100BA2502 /* LateInit.swift in Sources */,
567567
A31A16342576B7AF00C9CDDF /* Types.swift in Sources */,
568568
46A018DA25E97FDF00F9CCD8 /* AppleUtils.swift in Sources */,
569569
A31A16E12579779600C9CDDF /* Version.swift in Sources */,

Sources/Segment/Events.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ extension Analytics {
2727
}
2828

2929
} catch {
30-
assertionFailure("\(error)")
30+
exceptionFailure("\(error)")
3131
}
3232
}
3333

3434
public func track(name: String, properties: [String: Any]?) {
3535
var props: JSON? = nil
3636
if let properties = properties {
37-
props = try? JSON(properties)
37+
do {
38+
props = try JSON(properties)
39+
} catch {
40+
exceptionFailure("\(error)")
41+
}
3842
}
3943
let event = TrackEvent(event: name, properties: props)
4044
process(incomingEvent: event)
@@ -64,7 +68,7 @@ extension Analytics {
6468
let event = IdentifyEvent(userId: userId, traits: jsonTraits)
6569
process(incomingEvent: event)
6670
} catch {
67-
assertionFailure("\(error)")
71+
exceptionFailure("\(error)")
6872
}
6973
}
7074

@@ -78,7 +82,7 @@ extension Analytics {
7882
let event = IdentifyEvent(traits: jsonTraits)
7983
process(incomingEvent: event)
8084
} catch {
81-
assertionFailure("\(error)")
85+
exceptionFailure("\(error)")
8286
}
8387
}
8488

@@ -109,7 +113,7 @@ extension Analytics {
109113
process(incomingEvent: event)
110114
}
111115
} catch {
112-
assertionFailure("\(error)")
116+
exceptionFailure("\(error)")
113117
}
114118
}
115119

@@ -133,7 +137,7 @@ extension Analytics {
133137
process(incomingEvent: event)
134138
}
135139
} catch {
136-
assertionFailure("\(error)")
140+
exceptionFailure("\(error)")
137141
}
138142
}
139143

Sources/Segment/Plugins/Context.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public class Context: PlatformPlugin {
3434
context.merge(eventContext) { (_, new) in new }
3535
}
3636

37-
workingEvent.context = try? JSON(context)
37+
do {
38+
workingEvent.context = try JSON(context)
39+
} catch {
40+
exceptionFailure("Unable to convert context to JSON: \(error)")
41+
}
3842

3943
return workingEvent
4044
}

Sources/Segment/Plugins/DeviceToken.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public class DeviceToken: PlatformPlugin {
2424
guard var workingEvent = event else { return event }
2525
if var context = workingEvent.context?.dictionaryValue, let token = token {
2626
context[keyPath: "device.token"] = token
27-
workingEvent.context = try? JSON(context)
27+
do {
28+
workingEvent.context = try JSON(context)
29+
} catch {
30+
exceptionFailure("Unable to convert context to JSON: \(error)")
31+
}
2832
}
2933
return workingEvent
3034
}

Sources/Segment/Plugins/Metrics.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ public enum MetricType: Int {
3131

3232
public extension RawEvent {
3333
mutating func addMetric(_ type: MetricType, name: String, value: Double, tags: [String]?, timestamp: Date) {
34-
guard let metric = try? JSON(with: Metric(eventName: "\(Self.self)", metricName: name, value: value, tags: tags, type: type, timestamp: Date())) else { return }
34+
guard let metric = try? JSON(with: Metric(eventName: "\(Self.self)", metricName: name, value: value, tags: tags, type: type, timestamp: Date())) else {
35+
exceptionFailure("Unable to add metric `\(name)`")
36+
return
37+
}
3538
if self.metrics == nil {
3639
metrics = [JSON]()
3740
}
3841

3942
if let jsonEncoded = try? JSON(with: metric) {
4043
metrics?.append(jsonEncoded)
44+
} else {
45+
exceptionFailure("Unable to encode metric `\(name)` to JSON.")
4146
}
4247
}
4348
}

Sources/Segment/Utilities/HTTPClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class HTTPClient {
6969
urlRequest.httpMethod = "POST"
7070

7171
guard let session = try? configuredSession(for: writeKey) else {
72+
exceptionFailure("Unable to create a HTTPClient session!")
7273
completion(false)
7374
return nil
7475
}

Sources/Segment/Utilities/LateInit.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Utils.swift
3+
// Segment
4+
//
5+
// Created by Brandon Sneed on 5/18/21.
6+
//
7+
8+
import Foundation
9+
10+
internal var isUnitTesting: Bool = {
11+
let env = ProcessInfo().environment
12+
let value = (env["XCTestConfigurationFilePath"] != nil)
13+
return value
14+
}()
15+
16+
internal let _SegmentException = "SegmentException"
17+
internal func exceptionFailure(_ message: String) {
18+
let args: [CVarArg] = []
19+
if isUnitTesting {
20+
NSException.raise(NSExceptionName(_SegmentException), format: message, arguments: getVaList(args))
21+
} else {
22+
#if DEBUG
23+
NSException.raise(NSExceptionName(_SegmentException), format: message, arguments: getVaList(args))
24+
#endif
25+
}
26+
}

0 commit comments

Comments
 (0)