-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathErrors.swift
96 lines (85 loc) · 3.45 KB
/
Errors.swift
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Errors.swift
//
//
// Created by Brandon Sneed on 10/20/22.
//
import Foundation
public indirect enum AnalyticsError: Error {
case storageUnableToCreate(String)
case storageUnableToWrite(String)
case storageUnableToRename(String)
case storageUnableToOpen(String)
case storageUnableToClose(String)
case storageInvalid(String)
case storageUnknown(Error)
case networkUnexpectedHTTPCode(URL?, Int)
case networkServerLimited(URL?, Int)
case networkServerRejected(URL?, Int)
case networkUnknown(URL?, Error)
case networkInvalidData
case jsonUnableToSerialize(Error)
case jsonUnableToDeserialize(Error)
case jsonUnknown(Error)
case pluginError(Error)
case enrichmentError(String)
case settingsFail(AnalyticsError)
case batchUploadFail(AnalyticsError)
}
extension Analytics {
/// Tries to convert known error types to AnalyticsError.
static internal func translate(error: Error) -> Error {
if let e = error as? OutputFileStream.OutputStreamError {
switch e {
case .invalidPath(let path):
return AnalyticsError.storageInvalid(path)
case .unableToCreate(let path):
return AnalyticsError.storageUnableToCreate(path)
case .unableToOpen(let path):
return AnalyticsError.storageUnableToOpen(path)
case .unableToWrite(let path):
return AnalyticsError.storageUnableToWrite(path)
case .unableToClose(let path):
return AnalyticsError.storageUnableToClose(path)
}
}
if let e = error as? JSON.JSONError {
switch e {
case .incorrectType:
return AnalyticsError.jsonUnableToDeserialize(e)
case .nonJSONType:
return AnalyticsError.jsonUnableToDeserialize(e)
case .unknown:
return AnalyticsError.jsonUnknown(e)
}
}
return error
}
/// Reports an internal error to the user-defined error handler.
public func reportInternalError(_ error: Error, fatal: Bool = false) {
let translatedError = Self.translate(error: error)
configuration.values.errorHandler?(translatedError)
Self.segmentLog(message: "An internal error occurred: \(translatedError)", kind: .error)
if fatal {
exceptionFailure("A critical error occurred: \(translatedError)")
}
Telemetry.shared.error(metric: Telemetry.INVOKE_ERROR_METRIC, log: Thread.callStackSymbols.joined(separator: "\n")) {
(_ it: inout [String: String]) in
it["error"] = "\(translatedError)"
it["writekey"] = configuration.values.writeKey
}
}
static public func reportInternalError(_ error: Error, fatal: Bool = false) {
// we don't have an instance of analytics to call to get our error handler,
// but we can at least report the message to the console.
let translatedError = Self.translate(error: error)
Self.segmentLog(message: "An internal error occurred: \(translatedError)", kind: .error)
if fatal {
exceptionFailure("A critical error occurred: \(translatedError)")
}
Telemetry.shared.error(metric: Telemetry.INVOKE_ERROR_METRIC, log: Thread.callStackSymbols.joined(separator: "\n")) {
(_ it: inout [String: String]) in
it["error"] = "\(translatedError)"
}
}
}