|
| 1 | +// |
| 2 | +// ErrorController.swift |
| 3 | +// PlayerUI |
| 4 | +// |
| 5 | +// Created by Player Team |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import JavaScriptCore |
| 10 | + |
| 11 | +/** |
| 12 | + Severity levels for errors |
| 13 | + */ |
| 14 | +public enum ErrorSeverity: String { |
| 15 | + /// Cannot continue, flow must end |
| 16 | + case fatal |
| 17 | + /// Standard error, may allow recovery |
| 18 | + case error |
| 19 | + /// Non-blocking, logged for telemetry |
| 20 | + case warning |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + Known error types for Player |
| 25 | + */ |
| 26 | +public struct ErrorTypes { |
| 27 | + public static let expression = "expression" |
| 28 | + public static let binding = "binding" |
| 29 | + public static let view = "view" |
| 30 | + public static let asset = "asset" |
| 31 | + public static let navigation = "navigation" |
| 32 | + public static let validation = "validation" |
| 33 | + public static let data = "data" |
| 34 | + public static let schema = "schema" |
| 35 | + public static let network = "network" |
| 36 | + public static let plugin = "plugin" |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + Represents a Player error with metadata |
| 41 | + */ |
| 42 | +public class PlayerErrorInfo: CreatedFromJSValue { |
| 43 | + /// Typealias for associated type |
| 44 | + public typealias T = PlayerErrorInfo |
| 45 | + |
| 46 | + /// The JSValue that backs this wrapper |
| 47 | + private let value: JSValue |
| 48 | + |
| 49 | + /// The error message |
| 50 | + public var message: String { |
| 51 | + value.objectForKeyedSubscript("error")?.objectForKeyedSubscript("message")?.toString() ?? "" |
| 52 | + } |
| 53 | + |
| 54 | + /// The error name |
| 55 | + public var name: String { |
| 56 | + value.objectForKeyedSubscript("error")?.objectForKeyedSubscript("name")?.toString() ?? "" |
| 57 | + } |
| 58 | + |
| 59 | + /// Error category |
| 60 | + public var errorType: String { |
| 61 | + value.objectForKeyedSubscript("errorType")?.toString() ?? "" |
| 62 | + } |
| 63 | + |
| 64 | + /// Impact level |
| 65 | + public var severity: ErrorSeverity? { |
| 66 | + guard let severityString = value.objectForKeyedSubscript("severity")?.toString() else { |
| 67 | + return nil |
| 68 | + } |
| 69 | + return ErrorSeverity(rawValue: severityString) |
| 70 | + } |
| 71 | + |
| 72 | + /// Additional metadata |
| 73 | + public var metadata: [String: Any]? { |
| 74 | + guard let metadataValue = value.objectForKeyedSubscript("metadata"), |
| 75 | + !metadataValue.isUndefined, |
| 76 | + !metadataValue.isNull else { |
| 77 | + return nil |
| 78 | + } |
| 79 | + return metadataValue.toObject() as? [String: Any] |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + Creates an instance from a JSValue, used for generic construction |
| 84 | + - parameters: |
| 85 | + - value: The JSValue to construct from |
| 86 | + */ |
| 87 | + public static func createInstance(value: JSValue) -> PlayerErrorInfo { |
| 88 | + return PlayerErrorInfo(value) |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + Construct a PlayerErrorInfo from a JSValue |
| 93 | + - parameters: |
| 94 | + - value: The JSValue that is the error object |
| 95 | + */ |
| 96 | + public init(_ value: JSValue) { |
| 97 | + self.value = value |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + A wrapper around the JS ErrorController in the core player |
| 103 | + */ |
| 104 | +public class ErrorController: CreatedFromJSValue { |
| 105 | + /// Typealias for associated type |
| 106 | + public typealias T = ErrorController |
| 107 | + |
| 108 | + /** |
| 109 | + Creates an instance from a JSValue, used for generic construction |
| 110 | + - parameters: |
| 111 | + - value: The JSValue to construct from |
| 112 | + */ |
| 113 | + public static func createInstance(value: JSValue) -> ErrorController { ErrorController(value) } |
| 114 | + |
| 115 | + /// The JSValue that backs this wrapper |
| 116 | + private let value: JSValue |
| 117 | + |
| 118 | + /// The hooks that can be tapped into |
| 119 | + public let hooks: ErrorControllerHooks |
| 120 | + |
| 121 | + /** |
| 122 | + Construct an ErrorController from a JSValue |
| 123 | + - parameters: |
| 124 | + - value: The JSValue that is the ErrorController |
| 125 | + */ |
| 126 | + public init(_ value: JSValue) { |
| 127 | + self.value = value |
| 128 | + hooks = ErrorControllerHooks( |
| 129 | + onError: BailHook<PlayerErrorInfo>(baseValue: value, name: "onError") |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + Capture an error with metadata |
| 135 | + - parameters: |
| 136 | + - error: The native Error object |
| 137 | + - errorType: Error category (use ErrorTypes constants) |
| 138 | + - severity: Impact level |
| 139 | + - metadata: Additional metadata dictionary |
| 140 | + - returns: The captured error as a JSValue |
| 141 | + */ |
| 142 | + @discardableResult |
| 143 | + public func captureError( |
| 144 | + error: Error, |
| 145 | + errorType: String, |
| 146 | + severity: ErrorSeverity? = nil, |
| 147 | + metadata: [String: Any]? = nil |
| 148 | + ) -> JSValue? { |
| 149 | + var args: [Any] = [ |
| 150 | + [ |
| 151 | + "message": error.localizedDescription, |
| 152 | + "name": String(describing: type(of: error)) |
| 153 | + ] as [String: Any], |
| 154 | + errorType |
| 155 | + ] |
| 156 | + |
| 157 | + if let severity = severity { |
| 158 | + args.append(severity.rawValue) |
| 159 | + } else { |
| 160 | + args.append(JSValue(undefinedIn: value.context) as Any) |
| 161 | + } |
| 162 | + |
| 163 | + if let metadata = metadata { |
| 164 | + args.append(metadata) |
| 165 | + } |
| 166 | + |
| 167 | + return value.invokeMethod("captureError", withArguments: args) |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + Get the most recent error |
| 172 | + - returns: The current error as a JSValue if one exists |
| 173 | + */ |
| 174 | + public func getCurrentError() -> JSValue? { |
| 175 | + return value.invokeMethod("getCurrentError", withArguments: []) |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + Get the complete error history |
| 180 | + - returns: JSValue representing the array of errors |
| 181 | + */ |
| 182 | + public func getErrors() -> JSValue? { |
| 183 | + return value.invokeMethod("getErrors", withArguments: []) |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + Clear all errors (history + current + data model) |
| 188 | + */ |
| 189 | + public func clearErrors() { |
| 190 | + value.invokeMethod("clearErrors", withArguments: []) |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + Clear only current error and remove from data model, preserve history |
| 195 | + */ |
| 196 | + public func clearCurrentError() { |
| 197 | + value.invokeMethod("clearCurrentError", withArguments: []) |
| 198 | + } |
| 199 | +} |
0 commit comments