lnlink

iOS app for connecting to lightning nodes
git clone git://jb55.com/lnlink
Log | Files | Refs | Submodules | README

commit 67480a5a27d454a8b76cc38379fa978805a10d35
parent dce24dd0f721a807e39281ce122d0001782141c6
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 25 Feb 2022 11:52:49 -0800

better error messages

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mlightninglink/PayView.swift | 6++++--
Mlightninglink/RPC.swift | 22++++++++++++++++++++--
2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/lightninglink/PayView.swift b/lightninglink/PayView.swift @@ -50,6 +50,7 @@ struct PayView: View { Text("\(render_amount(self.amount))") .font(.title) Text("\(self.error ?? "")") + .foregroundColor(Color.red) Spacer() HStack { Button("Cancel") { @@ -64,7 +65,7 @@ struct PayView: View { switch res { case .left(let err): - self.error = err + self.error = "Error: \(err)" case .right(let pay): print(pay) @@ -113,7 +114,8 @@ func confirm_payment(bolt11: String, lnlink: LNLink) -> Either<String, Pay> { switch res { case .failure(let req_err): // handle error - return .left(req_err.description) + let errmsg = req_err.decoded?.message ?? req_err.description + return .left(errmsg) case .success(let pay): return .right(pay) diff --git a/lightninglink/RPC.swift b/lightninglink/RPC.swift @@ -8,12 +8,20 @@ import Foundation -public typealias RequestRes<T> = Result<T, RequestError> +public typealias RequestRes<T> = Result<T, RequestError<RpcErrorData>> public struct ResultWrapper<T: Decodable>: Decodable { public var result: T } +public struct ErrorWrapper<T: Decodable>: Decodable { + public var error: T +} + +public struct RpcErrorData: Decodable { + public var message: String +} + public struct Output: Decodable { public var txid: String public var output: Int @@ -86,9 +94,10 @@ public enum RequestErrorType: Error { case unknown(String) } -public struct RequestError: Error, CustomStringConvertible { +public struct RequestError<E: Decodable>: Error, CustomStringConvertible { public var response: HTTPURLResponse? public var respData: Data = Data() + public var decoded: E? public var errorType: RequestErrorType init(errorType: RequestErrorType) { @@ -98,6 +107,7 @@ public struct RequestError: Error, CustomStringConvertible { init(respData: Data, errorType: RequestErrorType) { self.respData = respData self.errorType = errorType + self.decoded = maybe_decode_error_json(respData) } public var description: String { @@ -271,3 +281,11 @@ public func rpc_listfunds(ln: LNSocket, token: String) -> RequestRes<ListFunds> let params: Array<String> = [] 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? { + do { + return try JSONDecoder().decode(ErrorWrapper<T>.self, from: dat).error + } catch { + return nil + } +}