diff --git a/ios/src_library/React Native/RNIError/RNIBaseError+Helpers.swift b/ios/src_library/React Native/RNIError/RNIBaseError+Helpers.swift new file mode 100644 index 00000000..190537e0 --- /dev/null +++ b/ios/src_library/React Native/RNIError/RNIBaseError+Helpers.swift @@ -0,0 +1,99 @@ +// +// RNIBaseError+Helpers.swift +// react-native-ios-modal +// +// Created by Dominic Go on 5/12/23. +// + +import Foundation + +extension RNIBaseError { + + public var errorMessage: String { + var message = "message: \(self.message ?? "N/A")"; + + #if DEBUG + message += " - debugMessage: \(self.debugMessage ?? "N/A")"; + #endif + + if let fileID = self.fileID { + message += "- fileID: \(fileID)"; + }; + + if let functionName = self.functionName { + message += "- functionName: \(functionName)"; + }; + + if let lineNumber = self.lineNumber { + message += "- lineNumber: \(lineNumber)"; + }; + + return message; + }; + + public init( + code: ErrorCode, + message: String? = nil, + debugMessage: String? = nil, + debugData: Dictionary? = nil, + fileID: String? = #fileID, + functionName: String? = #function, + lineNumber: Int? = #line + ) { + self.init( + code: code, + message: message, + debugMessage: debugMessage + ); + + self.fileID = fileID; + self.functionName = functionName; + self.lineNumber = lineNumber; + }; + + public mutating func setDebugValues( + fileID: String = #fileID, + functionName: String = #function, + lineNumber: Int = #line + ) { + self.fileID = fileID; + self.functionName = functionName; + self.lineNumber = lineNumber; + }; + + public mutating func addDebugData(_ nextDebugData: Dictionary){ + guard let prevDebugData = self.debugData else { + self.debugData = nextDebugData; + return; + }; + + self.debugData = prevDebugData.merging(nextDebugData) { (_, new) in new }; + }; +}; + +extension RNIBaseError where Self: RNIDictionarySynthesizable { + + public var asNSError: NSError? { + + let errorCode = self.code.errorCode ?? + RNIGenericErrorCode.unspecified.errorCode; + + guard let errorCode = errorCode else { return nil }; + + return NSError( + domain: Self.domain, + code: errorCode, + userInfo: self.synthesizedJSDictionary + ); + }; + + public func invokePromiseRejectBlock( + _ block: @escaping RCTPromiseRejectBlock + ) { + block( + /* code */ self.code.description, + /* message */ self.errorMessage, + /* error */ self.asNSError + ); + }; +}; diff --git a/ios/src_library/React Native/RNIError/RNIBaseError.swift b/ios/src_library/React Native/RNIError/RNIBaseError.swift index abd35d2e..2fe9aa3f 100644 --- a/ios/src_library/React Native/RNIError/RNIBaseError.swift +++ b/ios/src_library/React Native/RNIError/RNIBaseError.swift @@ -10,52 +10,25 @@ import Foundation /// TODO - Move to `react-native-ios-utilities` /// * Replace older impl. of `RNIError` with this version -public class RNIBaseError: Error where E.RawValue == String { +public protocol RNIBaseError: Error where ErrorCode == any RNIErrorCode { - public var code: E; - public let domain: String; + associatedtype ErrorCode; - public let message: String?; - public let debug: String?; + static var domain: String { get }; - public init( - code: E, - domain: String, - message: String? = nil, - debug: String? = nil - ) { - self.code = code; - self.domain = domain; - self.message = message; - self.debug = debug; - }; + var code: ErrorCode { get }; + var message: String? { get }; - public func createJSONString() -> String? { - let encoder = JSONEncoder(); - - guard let data = try? encoder.encode(self), - let jsonString = String(data: data, encoding: .utf8) - else { return nil }; - - return jsonString; - }; -}; - -// ---------------- -// MARK:- Encodable -// ---------------- - -extension RNIBaseError: Encodable { - enum CodingKeys: String, CodingKey { - case code, domain, message, debug; - }; + var debugMessage: String? { get }; + var debugData: Dictionary? { get set } + + var fileID : String? { get set }; + var functionName: String? { get set }; + var lineNumber : Int? { get set }; - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self); - - try container.encode(self.code.rawValue, forKey: .code); - try container.encode(self.domain, forKey: .domain); - try container.encode(self.message, forKey: .message); - try container.encode(self.debug, forKey: .debug); - }; + init( + code: ErrorCode, + message: String?, + debugMessage: String? + ); }; diff --git a/ios/src_library/React Native/RNIError/RNIError.swift b/ios/src_library/React Native/RNIError/RNIError.swift new file mode 100644 index 00000000..c25c222c --- /dev/null +++ b/ios/src_library/React Native/RNIError/RNIError.swift @@ -0,0 +1,18 @@ +// +// RNIError.swift +// react-native-ios-modal +// +// Created by Dominic Go on 5/12/23. +// + +import Foundation + +public typealias RNIError = + RNIBaseError & RNIDictionarySynthesizable; + +public typealias RNIErrorCode = + CustomStringConvertible + & CaseIterable + & Equatable + & RNIErrorCodeDefaultable + & RNIErrorCodeSynthesizable; diff --git a/ios/src_library/React Native/RNIError/RNIErrorCodeDefaultable.swift b/ios/src_library/React Native/RNIError/RNIErrorCodeDefaultable.swift new file mode 100644 index 00000000..9a732311 --- /dev/null +++ b/ios/src_library/React Native/RNIError/RNIErrorCodeDefaultable.swift @@ -0,0 +1,70 @@ +// +// RNIGenericErrorDefaultable.swift +// react-native-ios-utilities +// +// Created by Dominic Go on 8/28/22. +// + +import Foundation + +public protocol RNIErrorCodeDefaultable { + + static var runtimeError : Self { get }; + static var libraryError : Self { get }; + static var reactError : Self { get }; + static var unknownError : Self { get }; + static var invalidArgument: Self { get }; + static var outOfBounds : Self { get }; + static var invalidReactTag: Self { get }; + static var nilValue : Self { get }; +}; + +// MARK: - Default +// --------------- + +public extension RNIErrorCodeDefaultable { + + static var runtimeError: Self { + Self.runtimeError + }; + + static var libraryError: Self { + Self.libraryError + }; + + static var reactError: Self { + Self.reactError + }; + + static var unknownError: Self { + Self.unknownError + }; + + static var invalidArgument: Self { + Self.invalidArgument + }; + + static var outOfBounds: Self { + Self.outOfBounds + }; + + static var invalidReactTag: Self { + Self.invalidReactTag + }; + + static var nilValue: Self { + Self.nilValue + }; +}; + +// MARK: - Helpers +// --------------- + +extension RNIErrorCodeDefaultable where Self: RawRepresentable { + + public var description: String { + self.rawValue; + }; +}; + + diff --git a/ios/src_library/React Native/RNIError/RNIErrorCodeSynthesizable.swift b/ios/src_library/React Native/RNIError/RNIErrorCodeSynthesizable.swift new file mode 100644 index 00000000..adece050 --- /dev/null +++ b/ios/src_library/React Native/RNIError/RNIErrorCodeSynthesizable.swift @@ -0,0 +1,24 @@ +// +// RNIErrorCodeSynthesizable.swift +// react-native-ios-modal +// +// Created by Dominic Go on 5/12/23. +// + +import Foundation + +public protocol RNIErrorCodeSynthesizable { + var errorCode: Int? { get }; +} + +extension RNIErrorCodeSynthesizable where Self: CaseIterable & Equatable { + + public var errorCode: Int? { + let match = Self.allCases.enumerated().first { _, value in + value == self + } + + guard let offset = match?.offset else { return nil }; + return offset * -1; + }; +}; diff --git a/ios/src_library/React Native/RNIError/RNIGenericError.swift b/ios/src_library/React Native/RNIError/RNIGenericError.swift deleted file mode 100644 index af4895f3..00000000 --- a/ios/src_library/React Native/RNIError/RNIGenericError.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// RNIGenericError.swift -// react-native-ios-utilities -// -// Created by Dominic Go on 4/21/22. -// - -import Foundation - - -public class RNIGenericError: RNIBaseError { - - init( - code: RNIGenericErrorCode, - message: String? = nil, - debug: String? = nil - ) { - super.init( - code: code, - domain: "react-native-ios-utilities", - message: message, - debug: debug - ); - }; -}; diff --git a/ios/src_library/React Native/RNIError/RNIGenericErrorCode.swift b/ios/src_library/React Native/RNIError/RNIGenericErrorCode.swift index a4adcced..728942a7 100644 --- a/ios/src_library/React Native/RNIError/RNIGenericErrorCode.swift +++ b/ios/src_library/React Native/RNIError/RNIGenericErrorCode.swift @@ -1,16 +1,15 @@ // // RNIGenericErrorCode.swift -// react-native-ios-utilities +// react-native-ios-modal // -// Created by Dominic Go on 4/21/22. +// Created by Dominic Go on 5/12/23. // import Foundation -public enum RNIGenericErrorCode: - String, Codable, CaseIterable, RNIGenericErrorDefaultable { +enum RNIGenericErrorCode: + String, CaseIterable, RNIErrorCodeDefaultable, RNIErrorCodeSynthesizable { - // placeholder case unspecified; }; diff --git a/ios/src_library/React Native/RNIError/RNIGenericErrorDefaultable+Default.swift b/ios/src_library/React Native/RNIError/RNIGenericErrorDefaultable+Default.swift deleted file mode 100644 index 6bc7ceea..00000000 --- a/ios/src_library/React Native/RNIError/RNIGenericErrorDefaultable+Default.swift +++ /dev/null @@ -1,42 +0,0 @@ -// -// RNIGenericErrorDefaultable+Default.swift -// react-native-ios-modal -// -// Created by Dominic Go on 5/10/23. -// - -import Foundation - -public extension RNIGenericErrorDefaultable { - static var runtimeError: Self { - Self.runtimeError - }; - - static var libraryError: Self { - Self.libraryError - }; - - static var reactError: Self { - Self.reactError - }; - - static var unknownError: Self { - Self.unknownError - }; - - static var invalidArgument: Self { - Self.invalidArgument - }; - - static var outOfBounds: Self { - Self.outOfBounds - }; - - static var invalidReactTag: Self { - Self.invalidReactTag - }; - - static var nilValue: Self { - Self.nilValue - }; -}; diff --git a/ios/src_library/React Native/RNIError/RNIGenericErrorDefaultable.swift b/ios/src_library/React Native/RNIError/RNIGenericErrorDefaultable.swift deleted file mode 100644 index 4c684e1a..00000000 --- a/ios/src_library/React Native/RNIError/RNIGenericErrorDefaultable.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// RNIGenericErrorDefaultable.swift -// react-native-ios-utilities -// -// Created by Dominic Go on 8/28/22. -// - -import Foundation - - -public protocol RNIGenericErrorDefaultable { - static var runtimeError : Self { get }; - static var libraryError : Self { get }; - static var reactError : Self { get }; - static var unknownError : Self { get }; - static var invalidArgument: Self { get }; - static var outOfBounds : Self { get }; - static var invalidReactTag: Self { get }; - static var nilValue : Self { get }; -};