From 1ac17e96c31c1ef4d42012d10c70bf0e1ed7f05e Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sat, 14 Jul 2018 02:56:36 +0900 Subject: [PATCH] Throw lnd exception if any issue with lnd --- .../Lightning/Lnd/LndInvoiceClient.cs | 27 ++++++++++---- .../Lightning/Lnd/LndSwaggerClient.partial.cs | 35 +++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/BTCPayServer/Payments/Lightning/Lnd/LndInvoiceClient.cs b/BTCPayServer/Payments/Lightning/Lnd/LndInvoiceClient.cs index 568bd9afd..e2115d0a5 100644 --- a/BTCPayServer/Payments/Lightning/Lnd/LndInvoiceClient.cs +++ b/BTCPayServer/Payments/Lightning/Lnd/LndInvoiceClient.cs @@ -64,11 +64,24 @@ namespace BTCPayServer.Payments.Lightning.Lnd while (!_Cts.IsCancellationRequested) { string line = await _Reader.ReadLineAsync().WithCancellation(_Cts.Token); - if (line != null && line.StartsWith("{\"result\":", StringComparison.OrdinalIgnoreCase)) + if (line != null) { - var invoiceString = JObject.Parse(line)["result"].ToString(); - LnrpcInvoice parsedInvoice = _Parent.Deserialize(invoiceString); - await _Invoices.Writer.WriteAsync(ConvertLndInvoice(parsedInvoice), _Cts.Token); + if (line.StartsWith("{\"result\":", StringComparison.OrdinalIgnoreCase)) + { + var invoiceString = JObject.Parse(line)["result"].ToString(); + LnrpcInvoice parsedInvoice = _Parent.Deserialize(invoiceString); + await _Invoices.Writer.WriteAsync(ConvertLndInvoice(parsedInvoice), _Cts.Token); + } + else if (line.StartsWith("{\"error\":", StringComparison.OrdinalIgnoreCase)) + { + var errorString = JObject.Parse(line)["error"].ToString(); + var error = _Parent.Deserialize(errorString); + throw new LndException(error); + } + else + { + throw new LndException("Unknown result from LND: " + line); + } } } } @@ -76,7 +89,7 @@ namespace BTCPayServer.Payments.Lightning.Lnd { } - catch(Exception ex) + catch (Exception ex) { _Invoices.Writer.TryComplete(ex); } @@ -95,7 +108,7 @@ namespace BTCPayServer.Payments.Lightning.Lnd { throw new TaskCanceledException(); } - catch(ChannelClosedException ex) + catch (ChannelClosedException ex) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); throw; @@ -119,7 +132,7 @@ namespace BTCPayServer.Payments.Lightning.Lnd _Response = null; _Client?.Dispose(); _Client = null; - if(waitLoop) + if (waitLoop) _ListenLoop?.Wait(); _Invoices.Writer.TryComplete(); } diff --git a/BTCPayServer/Payments/Lightning/Lnd/LndSwaggerClient.partial.cs b/BTCPayServer/Payments/Lightning/Lnd/LndSwaggerClient.partial.cs index 7ec18359f..dad41b34a 100644 --- a/BTCPayServer/Payments/Lightning/Lnd/LndSwaggerClient.partial.cs +++ b/BTCPayServer/Payments/Lightning/Lnd/LndSwaggerClient.partial.cs @@ -16,6 +16,41 @@ using Newtonsoft.Json.Linq; namespace BTCPayServer.Payments.Lightning.Lnd { + public class LndException : Exception + { + public LndException(string message) : base(message) + { + + } + public LndException(LndError error) : base(error.Message) + { + if (error == null) + throw new ArgumentNullException(nameof(error)); + _Error = error; + } + + + private readonly LndError _Error; + public LndError Error + { + get + { + return _Error; + } + } + } + // {"grpc_code":2,"http_code":500,"message":"rpc error: code = Unknown desc = expected 1 macaroon, got 0","http_status":"Internal Server Error"} + public class LndError + { + [JsonProperty("grpc_code")] + public int GRPCCode { get; set; } + [JsonProperty("http_code")] + public int HttpCode { get; set; } + [JsonProperty("message")] + public string Message { get; set; } + [JsonProperty("http_status")] + public string HttpStatus { get; set; } + } public partial class LndSwaggerClient { public LndSwaggerClient(LndRestSettings settings)