Skip to content

Commit

Permalink
🛠 Refactor: Re-write RNIError
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-03-27-23-55-09` Refactor: Re-write `RNIModalView` error creation and handling.
  • Loading branch information
dominicstop committed May 12, 2023
1 parent a6b905e commit c769f6f
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 135 deletions.
99 changes: 99 additions & 0 deletions ios/src_library/React Native/RNIError/RNIBaseError+Helpers.swift
Original file line number Diff line number Diff line change
@@ -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<String, Any>? = 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<String, Any>){
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
);
};
};
59 changes: 16 additions & 43 deletions ios/src_library/React Native/RNIError/RNIBaseError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,25 @@ import Foundation
/// TODO - Move to `react-native-ios-utilities`
/// * Replace older impl. of `RNIError` with this version

public class RNIBaseError<E: RawRepresentable>: 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<String, Any>? { 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?
);
};
18 changes: 18 additions & 0 deletions ios/src_library/React Native/RNIError/RNIError.swift
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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<String> {

public var description: String {
self.rawValue;
};
};


Original file line number Diff line number Diff line change
@@ -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;
};
};
25 changes: 0 additions & 25 deletions ios/src_library/React Native/RNIError/RNIGenericError.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
};

This file was deleted.

This file was deleted.

0 comments on commit c769f6f

Please sign in to comment.