From bf349a01101c928ea3333f2bb84bb3dd8e4928ae Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Thu, 2 Jun 2022 12:01:55 +0200
Subject: [PATCH] Replace unknown Bindings errors with fatalError

---
 .../AppFeature/PasswordStorage+Keychain.swift |  2 +-
 Sources/ElixxirDAppsSDK/ClientCreator.swift   |  2 +-
 Sources/ElixxirDAppsSDK/ClientLoader.swift    |  6 ++++--
 Sources/ElixxirDAppsSDK/ClientStorage.swift   |  2 +-
 Sources/ElixxirDAppsSDK/Errors.swift          | 19 -------------------
 Sources/ElixxirDAppsSDK/NDFDownloader.swift   |  2 +-
 .../ElixxirDAppsSDK/PasswordGenerator.swift   | 11 +++++------
 Sources/ElixxirDAppsSDK/PasswordStorage.swift |  4 ++++
 8 files changed, 17 insertions(+), 31 deletions(-)
 delete mode 100644 Sources/ElixxirDAppsSDK/Errors.swift

diff --git a/Example/example-app/Sources/AppFeature/PasswordStorage+Keychain.swift b/Example/example-app/Sources/AppFeature/PasswordStorage+Keychain.swift
index 7af6bc4f..5eb1a61d 100644
--- a/Example/example-app/Sources/AppFeature/PasswordStorage+Keychain.swift
+++ b/Example/example-app/Sources/AppFeature/PasswordStorage+Keychain.swift
@@ -8,7 +8,7 @@ extension PasswordStorage {
     )
     return PasswordStorage(
       save: { password in keychain[data: "password"] = password},
-      load: { try keychain[data: "password"] ?? { throw PasswordStorageMissingPasswordError() }() }
+      load: { try keychain[data: "password"] ?? { throw MissingPasswordError() }() }
     )
   }()
 }
diff --git a/Sources/ElixxirDAppsSDK/ClientCreator.swift b/Sources/ElixxirDAppsSDK/ClientCreator.swift
index e048227e..9e698645 100644
--- a/Sources/ElixxirDAppsSDK/ClientCreator.swift
+++ b/Sources/ElixxirDAppsSDK/ClientCreator.swift
@@ -17,7 +17,7 @@ extension ClientCreator {
       throw error
     }
     if !created {
-      throw BindingsNewClientUnknownError()
+      fatalError("BindingsNewClient returned `false` without providing error")
     }
   }
 }
diff --git a/Sources/ElixxirDAppsSDK/ClientLoader.swift b/Sources/ElixxirDAppsSDK/ClientLoader.swift
index 632f0107..7c1ca48a 100644
--- a/Sources/ElixxirDAppsSDK/ClientLoader.swift
+++ b/Sources/ElixxirDAppsSDK/ClientLoader.swift
@@ -12,9 +12,11 @@ extension ClientLoader {
   public static let live = ClientLoader { directoryURL, password in
     var error: NSError?
     let bindingsClient = BindingsLogin(directoryURL.path, password, &error)
-    if let error = error { throw error }
+    if let error = error {
+      throw error
+    }
     guard let bindingsClient = bindingsClient else {
-      throw BindingsLoginUnknownError()
+      fatalError("BindingsLogin returned `nil` without providing error")
     }
     return Client.live(bindingsClient: bindingsClient)
   }
diff --git a/Sources/ElixxirDAppsSDK/ClientStorage.swift b/Sources/ElixxirDAppsSDK/ClientStorage.swift
index 8471fa78..7c18828b 100644
--- a/Sources/ElixxirDAppsSDK/ClientStorage.swift
+++ b/Sources/ElixxirDAppsSDK/ClientStorage.swift
@@ -30,7 +30,7 @@ extension ClientStorage {
       },
       createClient: {
         let ndf = try downloadNDF(environment)
-        let password = try generatePassword()
+        let password = generatePassword()
         try passwordStorage.save(password)
         try? fileManager.removeItem(at: directoryURL)
         try? fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true)
diff --git a/Sources/ElixxirDAppsSDK/Errors.swift b/Sources/ElixxirDAppsSDK/Errors.swift
deleted file mode 100644
index d28f5baf..00000000
--- a/Sources/ElixxirDAppsSDK/Errors.swift
+++ /dev/null
@@ -1,19 +0,0 @@
-public struct BindingsDownloadAndVerifySignedNdfWithUrlUnknownError: Error, Equatable {
-  public init() {}
-}
-
-public struct BindingsGenerateSecretUnknownError: Error, Equatable {
-  public init() {}
-}
-
-public struct PasswordStorageMissingPasswordError: Error, Equatable {
-  public init() {}
-}
-
-public struct BindingsNewClientUnknownError: Error, Equatable {
-  public init() {}
-}
-
-public struct BindingsLoginUnknownError: Error, Equatable {
-  public init() {}
-}
diff --git a/Sources/ElixxirDAppsSDK/NDFDownloader.swift b/Sources/ElixxirDAppsSDK/NDFDownloader.swift
index c0552b99..fed33850 100644
--- a/Sources/ElixxirDAppsSDK/NDFDownloader.swift
+++ b/Sources/ElixxirDAppsSDK/NDFDownloader.swift
@@ -20,7 +20,7 @@ extension NDFDownloader {
       throw error
     }
     guard let data = data else {
-      throw BindingsDownloadAndVerifySignedNdfWithUrlUnknownError()
+      fatalError("BindingsDownloadAndVerifySignedNdfWithUrl returned `nil` without providing error")
     }
     return data
   }
diff --git a/Sources/ElixxirDAppsSDK/PasswordGenerator.swift b/Sources/ElixxirDAppsSDK/PasswordGenerator.swift
index 251ea873..949dfdb2 100644
--- a/Sources/ElixxirDAppsSDK/PasswordGenerator.swift
+++ b/Sources/ElixxirDAppsSDK/PasswordGenerator.swift
@@ -1,17 +1,17 @@
 import Bindings
 
 public struct PasswordGenerator {
-  public var run: () throws -> Data
+  public var run: () -> Data
 
-  public func callAsFunction() throws -> Data {
-    try run()
+  public func callAsFunction() -> Data {
+    run()
   }
 }
 
 extension PasswordGenerator {
   public static let live = PasswordGenerator {
     guard let secret = BindingsGenerateSecret(32) else {
-      throw BindingsGenerateSecretUnknownError()
+      fatalError("BindingsGenerateSecret returned `nil`")
     }
     return secret
   }
@@ -20,8 +20,7 @@ extension PasswordGenerator {
 #if DEBUG
 extension PasswordGenerator {
   public static let failing = PasswordGenerator {
-    struct NotImplemented: Error {}
-    throw NotImplemented()
+    Data()
   }
 }
 #endif
diff --git a/Sources/ElixxirDAppsSDK/PasswordStorage.swift b/Sources/ElixxirDAppsSDK/PasswordStorage.swift
index 82ae04dc..89df9af8 100644
--- a/Sources/ElixxirDAppsSDK/PasswordStorage.swift
+++ b/Sources/ElixxirDAppsSDK/PasswordStorage.swift
@@ -1,6 +1,10 @@
 import Foundation
 
 public struct PasswordStorage {
+  public struct MissingPasswordError: Error, Equatable {
+    public init() {}
+  }
+
   public init(
     save: @escaping (Data) throws -> Void,
     load: @escaping () throws -> Data
-- 
GitLab