Skip to content

Commit

Permalink
Change WalletError from String to Int (trustwallet#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
hewigovens authored and vikmeup committed Jun 15, 2018
1 parent 57de048 commit 82c0655
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
32 changes: 29 additions & 3 deletions Sources/Shared/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,36 @@

import Foundation

public enum WalletError: String, LocalizedError {
public enum WalletError: Int {
/// Unknown Error
case unknown = -1

/// No Error occurred
case none = 0

/// Error generated when the user cancells the sign request.
case cancelled
case cancelled = 1

/// Error generated when the request is invalid.
case invalidRequest
case invalidRequest = 2

/// Error generated when current wallet is watch only
case watchOnly = 3
}

extension WalletError: LocalizedError {
public var errorDescription: String? {
switch self {
case .unknown:
return "Unknown Error"
case .none:
return "No Error"
case .cancelled:
return "User cancelled"
case .invalidRequest:
return "Signing request is invalid"
case .watchOnly:
return "Wallet is watch only"
}
}
}
4 changes: 3 additions & 1 deletion Sources/TrustSDK/Commands/SignMessageCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public class SignMessageCommand: Command {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false), components.host == name else {
return false
}
if let errorString = components.queryItems?.first(where: { $0.name == "error" })?.value, let error = WalletError(rawValue: errorString) {
if let value = components.queryItems?.first(where: { $0.name == "error" })?.value,
let errorCode = Int(value),
let error = WalletError(rawValue: errorCode) {
completion(.failure(error))
return true
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/TrustSDK/Commands/SignTransactionCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public final class SignTransactionCommand: Command {
return false
}

if let errorString = components.queryItems?.first(where: { $0.name == "error" })?.value, let error = WalletError(rawValue: errorString) {
if let value = components.queryItems?.first(where: { $0.name == "error" })?.value,
let errorCode = Int(value),
let error = WalletError(rawValue: errorCode) {
completion(.failure(error))
return true
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TrustWalletSDK/TrustWalletSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public final class TrustWalletSDK {

private func callbackWithFailure(url: URL, error: WalletError) {
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.append(queryItem: URLQueryItem(name: "error", value: error.rawValue))
components.append(queryItem: URLQueryItem(name: "error", value: "\(error.rawValue)"))
UIApplication.shared.open(components.url!, options: [:], completionHandler: nil)
}
}

0 comments on commit 82c0655

Please sign in to comment.