Skip to content

Better handle errors where conversion occurs. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Segment.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
46A018D425E6C9C200F9CCD8 /* LinuxUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018D325E6C9C200F9CCD8 /* LinuxUtils.swift */; };
46A018DA25E97FDF00F9CCD8 /* AppleUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018D925E97FDF00F9CCD8 /* AppleUtils.swift */; };
46A018EE25E9A74F00F9CCD8 /* VendorSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46A018ED25E9A74F00F9CCD8 /* VendorSystem.swift */; };
46E382E5264C738100BA2502 /* LateInit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E382E4264C738100BA2502 /* LateInit.swift */; };
46E382E72654429A00BA2502 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E382E62654429A00BA2502 /* Utils.swift */; };
46FE4C9225A3EE90003A7362 /* Lifecycle_.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FE4C9125A3EE90003A7362 /* Lifecycle_.swift */; };
46FE4C9725A3F35E003A7362 /* macOSLifecycleMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FE4C9625A3F35E003A7362 /* macOSLifecycleMonitor.swift */; };
46FE4C9C25A3F41C003A7362 /* LinuxLifecycleMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FE4C9B25A3F41C003A7362 /* LinuxLifecycleMonitor.swift */; };
Expand Down Expand Up @@ -139,7 +139,7 @@
46A018D325E6C9C200F9CCD8 /* LinuxUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxUtils.swift; sourceTree = "<group>"; };
46A018D925E97FDF00F9CCD8 /* AppleUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleUtils.swift; sourceTree = "<group>"; };
46A018ED25E9A74F00F9CCD8 /* VendorSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VendorSystem.swift; sourceTree = "<group>"; };
46E382E4264C738100BA2502 /* LateInit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LateInit.swift; sourceTree = "<group>"; };
46E382E62654429A00BA2502 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
46FE4C9125A3EE90003A7362 /* Lifecycle_.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lifecycle_.swift; sourceTree = "<group>"; };
46FE4C9625A3F35E003A7362 /* macOSLifecycleMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = macOSLifecycleMonitor.swift; sourceTree = "<group>"; };
46FE4C9B25A3F41C003A7362 /* LinuxLifecycleMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxLifecycleMonitor.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -324,7 +324,7 @@
46FE4CDF25A53FAD003A7362 /* Storage.swift */,
4621080B2605332D00EBC4A8 /* KeyPath.swift */,
46022770261F7A4800A9E913 /* Atomic.swift */,
46E382E4264C738100BA2502 /* LateInit.swift */,
46E382E62654429A00BA2502 /* Utils.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand Down Expand Up @@ -559,11 +559,11 @@
96C33AB1258961F500F3D538 /* Settings.swift in Sources */,
969A534325B10324009227D9 /* iOSAppBackground.swift in Sources */,
46FE4C9225A3EE90003A7362 /* Lifecycle_.swift in Sources */,
46E382E72654429A00BA2502 /* Utils.swift in Sources */,
A31A16B225781CB400C9CDDF /* JSON.swift in Sources */,
46022771261F7A4800A9E913 /* Atomic.swift in Sources */,
A3471FBE256487F000965480 /* Configuration.swift in Sources */,
OBJ_23 /* Analytics.swift in Sources */,
46E382E5264C738100BA2502 /* LateInit.swift in Sources */,
A31A16342576B7AF00C9CDDF /* Types.swift in Sources */,
46A018DA25E97FDF00F9CCD8 /* AppleUtils.swift in Sources */,
A31A16E12579779600C9CDDF /* Version.swift in Sources */,
Expand Down
16 changes: 10 additions & 6 deletions Sources/Segment/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ extension Analytics {
}

} catch {
assertionFailure("\(error)")
exceptionFailure("\(error)")
}
}

public func track(name: String, properties: [String: Any]?) {
var props: JSON? = nil
if let properties = properties {
props = try? JSON(properties)
do {
props = try JSON(properties)
} catch {
exceptionFailure("\(error)")
}
}
let event = TrackEvent(event: name, properties: props)
process(incomingEvent: event)
Expand Down Expand Up @@ -64,7 +68,7 @@ extension Analytics {
let event = IdentifyEvent(userId: userId, traits: jsonTraits)
process(incomingEvent: event)
} catch {
assertionFailure("\(error)")
exceptionFailure("\(error)")
}
}

Expand All @@ -78,7 +82,7 @@ extension Analytics {
let event = IdentifyEvent(traits: jsonTraits)
process(incomingEvent: event)
} catch {
assertionFailure("\(error)")
exceptionFailure("\(error)")
}
}

Expand Down Expand Up @@ -109,7 +113,7 @@ extension Analytics {
process(incomingEvent: event)
}
} catch {
assertionFailure("\(error)")
exceptionFailure("\(error)")
}
}

Expand All @@ -133,7 +137,7 @@ extension Analytics {
process(incomingEvent: event)
}
} catch {
assertionFailure("\(error)")
exceptionFailure("\(error)")
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/Segment/Plugins/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public class Context: PlatformPlugin {
context.merge(eventContext) { (_, new) in new }
}

workingEvent.context = try? JSON(context)
do {
workingEvent.context = try JSON(context)
} catch {
exceptionFailure("Unable to convert context to JSON: \(error)")
}

return workingEvent
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Segment/Plugins/DeviceToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public class DeviceToken: PlatformPlugin {
guard var workingEvent = event else { return event }
if var context = workingEvent.context?.dictionaryValue, let token = token {
context[keyPath: "device.token"] = token
workingEvent.context = try? JSON(context)
do {
workingEvent.context = try JSON(context)
} catch {
exceptionFailure("Unable to convert context to JSON: \(error)")
}
}
return workingEvent
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/Segment/Plugins/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ public enum MetricType: Int {

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

if let jsonEncoded = try? JSON(with: metric) {
metrics?.append(jsonEncoded)
} else {
exceptionFailure("Unable to encode metric `\(name)` to JSON.")
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Segment/Utilities/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class HTTPClient {
urlRequest.httpMethod = "POST"

guard let session = try? configuredSession(for: writeKey) else {
exceptionFailure("Unable to create a HTTPClient session!")
completion(false)
return nil
}
Expand Down
29 changes: 0 additions & 29 deletions Sources/Segment/Utilities/LateInit.swift

This file was deleted.

26 changes: 26 additions & 0 deletions Sources/Segment/Utilities/Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Utils.swift
// Segment
//
// Created by Brandon Sneed on 5/18/21.
//

import Foundation

internal var isUnitTesting: Bool = {
let env = ProcessInfo().environment
let value = (env["XCTestConfigurationFilePath"] != nil)
return value
}()

internal let _SegmentException = "SegmentException"
internal func exceptionFailure(_ message: String) {
let args: [CVarArg] = []
if isUnitTesting {
NSException.raise(NSExceptionName(_SegmentException), format: message, arguments: getVaList(args))
Copy link
Contributor

@migs647 migs647 May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure we want to use NSExceptions? They aren't friendly with recovery, and apple doesn't recommend usage unless you fail / exit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's why I used them. I want a hard stop when unit testing or during development.

Copy link
Contributor

@migs647 migs647 May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, if that is the intention then we're good :)

} else {
#if DEBUG
NSException.raise(NSExceptionName(_SegmentException), format: message, arguments: getVaList(args))
#endif
}
}