Skip to content

Commit

Permalink
cleanup errors
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed May 15, 2024
1 parent 5610a08 commit ff75355
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 65 deletions.
72 changes: 14 additions & 58 deletions Sources/TorusUtils/Helpers/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,55 @@ import Foundation

public enum TorusUtilError: Error, Equatable {
case configurationError
case apiRequestFailed
case errInResponse(Any)
case encodingFailed(String? = nil)
case decodingFailed(String? = nil)
case encodingFailed(String = "")
case decodingFailed(String = "")
case commitmentRequestFailed
case importShareFailed
case invalidImportShares
case decryptionFailed
case thresholdError
case promiseFulfilled
case privateKeyDeriveFailed
case timeout
case missingNodePubKeyError
case unableToDerive
case interpolationFailed
case nodesUnavailable
case invalidKeySize
case invalidPubKeySize
case invalid
case runtime(_ msg: String)
case retrieveOrImportShareError
case metadataNonceMissing
case gatingError(_ msg: String? = nil)
case empty
case gatingError(_ msg: String = "")
case invalidInput
}

extension TorusUtilError: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .configurationError:
return "SDK Configuration incorrect. Network is probably incorrect"
case .apiRequestFailed:
return "API request failed or No response from the node"
case let .decodingFailed(response):
return "JSON Decoding error \(response ?? "")"
case let .errInResponse(str):
return "API response error \(str)"
return "JSON Decoding error" + response
case .decryptionFailed:
return "Decryption Failed"
case .commitmentRequestFailed:
return "commitment request failed"
case .importShareFailed:
return "import share failed"
case .invalidImportShares:
return "invalid import shares"
case .thresholdError:
return "Threshold error"
case .promiseFulfilled:
return "Promise fulfilled"
case .privateKeyDeriveFailed:
return "could not derive private key"
case .timeout:
return "Timeout"
case .missingNodePubKeyError:
return "Missing node pub key for node index"
case .unableToDerive:
return "could not derive private key"
case .interpolationFailed:
return "lagrange interpolation failed"
case .nodesUnavailable:
return "One or more nodes unavailable"
case .empty:
return ""
case .invalidInput:
return "Input was found to be invalid"
case let .runtime(msg):
return msg
case .invalidKeySize:
return "Invalid key size. Expected 32 bytes"
case .invalidPubKeySize:
return "Invalid key size. Expected 64 bytes"
case .invalid:
return "Invalid"
case let .encodingFailed(msg):
return "Could not encode data \(msg ?? "")"
return "Could not encode data" + msg
case .retrieveOrImportShareError:
return "retrieve or import share failed"
case .metadataNonceMissing:
return "Unable to fetch metadata nonce"
case let .gatingError(msg):
return "could not process request \(msg ?? "")"
return "could not process request" + msg
}
}

Expand All @@ -97,38 +67,24 @@ extension TorusUtilError: LocalizedError {
switch self {
case .configurationError:
return "SDK Configuration incorrect. Network is probably incorrect"
case .apiRequestFailed:
return "API request failed or No response from the node"
case let .decodingFailed(response):
return "JSON Decoding error \(response ?? "")"
case let .errInResponse(str):
return "API response error \(str)"
return "JSON Decoding error" + response
case .decryptionFailed:
return "Decryption Failed"
case .commitmentRequestFailed:
return "commitment request failed"
case .thresholdError:
return "Threshold error"
case .promiseFulfilled:
return "Promise fulfilled"
case .timeout:
return "Timeout"
case .unableToDerive:
return "could not derive private key"
case .interpolationFailed:
return "lagrange interpolation failed"
case .nodesUnavailable:
return "One or more nodes unavailable"
case .empty:
return ""
case .invalidInput:
return "Input was found to be invalid"
case let .runtime(msg):
return msg
case .invalidKeySize:
return "Invalid key size. Expected 32 bytes"
case let .encodingFailed(msg):
return "Could not encode data \(msg ?? "")"
return "Could not encode data " + msg
case let .gatingError(msg):
return "\(msg ?? "")"
return msg
default:
return "default Error msg"
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TorusUtils/Helpers/KeyUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class KeyUtils {
}

public static func keccak256Data(_ input: String) throws -> String {
guard let data = input.data(using: .utf8) else { throw TorusUtilError.empty }
guard let data = input.data(using: .utf8) else { throw TorusUtilError.invalidInput }
return try keccak256(data: data).toHexString()
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/TorusUtils/Point.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ internal class Point: Decodable {
if let xCoord = BigInt(x, radix: 16) {
self.x = xCoord
} else {
throw TorusUtilError.unableToDerive
throw TorusUtilError.invalidInput
}

if let yCoord = BigInt(y, radix: 16) {
self.y = yCoord
} else {
throw TorusUtilError.unableToDerive
throw TorusUtilError.invalidInput
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/TorusUtils/Share.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ internal class Share: Codable {
if let si = BigInt(shareIndex, radix: 16) {
self.shareIndex = si
} else {
throw TorusUtilError.unableToDerive
throw TorusUtilError.invalidInput
}

if let s = BigInt(share, radix: 16) {
self.share = s
} else {
throw TorusUtilError.unableToDerive
throw TorusUtilError.invalidInput
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/TorusUtils/TorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public class TorusUtils {
}

if oAuthPubKey == nil || finalPubKey == nil {
throw TorusUtilError.unableToDerive
throw TorusUtilError.privateKeyDeriveFailed
}

let (oAuthPubKeyX, oAuthPubKeyY) = try KeyUtils.getPublicKeyCoords(pubKey: oAuthPubKey!)
Expand Down Expand Up @@ -337,7 +337,7 @@ public class TorusUtils {
let oAuthAddress = try KeyUtils.generateAddressFromPubKey(publicKeyX: X, publicKeyY: Y)

if typeOfUser == .v2 && finalPubKey == nil {
throw TorusUtilError.unableToDerive
throw TorusUtilError.privateKeyDeriveFailed
}

let (finalPubKeyX, finalPubKeyY) = try KeyUtils.getPublicKeyCoords(pubKey: finalPubKey!)
Expand Down

0 comments on commit ff75355

Please sign in to comment.