commit 763731077be0450f43abd81e732a0c63c8061dd2
parent 87539b63d04368ba60ca6afd881e02e32a780923
Author: William Casarin <jb55@jb55.com>
Date: Sat, 26 Feb 2022 21:12:05 -0800
improved errors messages, layout tweaks
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
4 files changed, 29 insertions(+), 17 deletions(-)
diff --git a/lightninglink/RPC.swift b/lightninglink/RPC.swift
@@ -18,8 +18,12 @@ public struct ErrorWrapper<T: Decodable>: Decodable {
public var error: T
}
-public struct RpcErrorData: Decodable {
+public struct RpcErrorData: Decodable, CustomStringConvertible {
public var message: String
+
+ public var description: String {
+ return message
+ }
}
public struct Output: Decodable {
@@ -94,10 +98,10 @@ public enum RequestErrorType: Error {
case unknown(String)
}
-public struct RequestError<E: Decodable>: Error, CustomStringConvertible {
+public struct RequestError<E: CustomStringConvertible & Decodable>: Error, CustomStringConvertible {
public var response: HTTPURLResponse?
public var respData: Data = Data()
- public var decoded: E?
+ public var decoded: Either<String, E>?
public var errorType: RequestErrorType
init(errorType: RequestErrorType) {
@@ -111,6 +115,15 @@ public struct RequestError<E: Decodable>: Error, CustomStringConvertible {
}
public var description: String {
+ if let decoded = self.decoded {
+ switch decoded {
+ case .left(let err):
+ return err
+ case .right(let err):
+ return err.description
+ }
+ }
+
let strData = String(decoding: respData, as: UTF8.self)
guard let resp = response else {
@@ -277,10 +290,14 @@ public func rpc_listfunds(ln: LNSocket, token: String) -> RequestRes<ListFunds>
return performRpc(ln: ln, operation: "listfunds", authToken: token, timeout_ms: default_timeout, params: params)
}
-public func maybe_decode_error_json<T: Decodable>(_ dat: Data) -> T? {
+public func maybe_decode_error_json<T: Decodable>(_ dat: Data) -> Either<String, T>? {
do {
- return try JSONDecoder().decode(ErrorWrapper<T>.self, from: dat).error
+ return .right(try JSONDecoder().decode(ErrorWrapper<T>.self, from: dat).error)
} catch {
- return nil
+ do {
+ return .left(try JSONDecoder().decode(ErrorWrapper<String>.self, from: dat).error)
+ } catch {
+ return nil
+ }
}
}
diff --git a/lightninglink/Views/PayView.swift b/lightninglink/Views/PayView.swift
@@ -114,7 +114,7 @@ func confirm_payment(bolt11: String, lnlink: LNLink) -> Either<String, Pay> {
switch res {
case .failure(let req_err):
// handle error
- let errmsg = req_err.decoded?.message ?? req_err.description
+ let errmsg = req_err.description
return .left(errmsg)
case .success(let pay):
diff --git a/lightninglink/Views/SetupView.swift b/lightninglink/Views/SetupView.swift
@@ -60,7 +60,7 @@ struct SetupView: View {
self.error = "Connected but could not retrieve data. Commando plugin missing?"
case .auth_invalid(let str):
self.state = .initial
- self.error = "Auth issue: \(str)"
+ self.error = str
case .success(let info, let funds):
save_lnlink(lnlink: lnlink)
self.lnlink = lnlink
@@ -95,8 +95,8 @@ struct SetupView: View {
Spacer()
Link("What the heck is LNLink?", destination: URL(string:"https://jb55.com/lnlink/qr")!)
- .padding()
}
+ .padding()
.sheet(item: $active_sheet) { active_sheet in
switch active_sheet {
case .qr:
@@ -207,12 +207,7 @@ func validate_connection(lnlink: LNLink, completion: @escaping (SetupResult) ->
break
}
- guard let decoded = rpc_err.decoded else {
- completion(.auth_invalid(rpc_err.description))
- return
- }
-
- completion(.auth_invalid(decoded.message))
+ completion(.auth_invalid(rpc_err.description))
case .success(let getinfo):
let funds_res = rpc_listfunds(ln: ln, token: lnlink.token)
diff --git a/lightninglink/lightninglinkApp.swift b/lightninglink/lightninglinkApp.swift
@@ -25,12 +25,12 @@ func fetch_dashboard(lnlink: LNLink) -> Either<String, Dashboard> {
let res = rpc_getinfo(ln: ln, token: lnlink.token)
switch res {
case .failure(let res_err):
- return .left(res_err.decoded?.message ?? res_err.errorType.localizedDescription.debugDescription )
+ return .left(res_err.description )
case .success(let info):
let res2 = rpc_listfunds(ln: ln, token: lnlink.token)
switch res2 {
case .failure(let err):
- return .left(err.decoded?.message ?? err.description)
+ return .left(err.description)
case .success(let funds):
return .right(Dashboard(info: info, funds: funds))
}