-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathStatsTracker+ErrorTracking.swift
More file actions
55 lines (51 loc) · 1.89 KB
/
Copy pathStatsTracker+ErrorTracking.swift
File metadata and controls
55 lines (51 loc) · 1.89 KB
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
import Foundation
extension StatsTracker {
/// Convenience method to track errors with automatic type detection
/// - Parameters:
/// - error: The error to track
/// - screen: The screen where the error occurred
func trackError(_ error: Error, screen: String) {
let errorType: String
let errorCode = (error as NSError).code
// Determine error type based on the error instance
switch error {
case let urlError as URLError:
errorType = urlErrorType(urlError)
case is DecodingError:
errorType = "parsing"
case is CancellationError:
return
default:
// Check for common error domains
let nsError = error as NSError
switch nsError.domain {
case NSCocoaErrorDomain:
errorType = "cocoa_\(errorCode)"
case NSURLErrorDomain:
errorType = "url_\(errorCode)"
default:
errorType = "unknown"
}
}
send(.errorEncountered, properties: [
"error_type": errorType,
"error_code": "\(errorCode)",
"screen": screen
])
}
/// Determine specific network error type
private func urlErrorType(_ error: URLError) -> String {
switch error.code {
case .notConnectedToInternet: "network_offline"
case .timedOut: "network_timeout"
case .cannotFindHost, .cannotConnectToHost: "network_host_unreachable"
case .networkConnectionLost: "network_connection_lost"
case .dnsLookupFailed: "network_dns_failed"
case .httpTooManyRedirects: "network_too_many_redirects"
case .resourceUnavailable: "network_resource_unavailable"
case .dataNotAllowed: "network_data_not_allowed"
case .secureConnectionFailed: "network_ssl_failed"
default: "other"
}
}
}