Throw lnd exception if any issue with lnd

This commit is contained in:
nicolas.dorier
2018-07-14 02:56:36 +09:00
parent d907031ec7
commit 1ac17e96c3
2 changed files with 55 additions and 7 deletions

View File

@@ -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<LnrpcInvoice>(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<LnrpcInvoice>(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<LndError>(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();
}

View File

@@ -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)