Greenfield: Improve error message, do not use internal lightning node on store's lightning API

This commit is contained in:
nicolas.dorier
2021-12-16 12:32:13 +09:00
parent bbddd72780
commit 4f7eeea14e
12 changed files with 149 additions and 64 deletions

View File

@@ -20,15 +20,8 @@ namespace BTCPayServer.Controllers.GreenField
{
public void OnException(ExceptionContext context)
{
if (context.Exception is NBitcoin.JsonConverters.JsonObjectException jsonObject)
{
context.Result = new ObjectResult(new GreenfieldValidationError(jsonObject.Path, jsonObject.Message));
}
else
{
context.Result = new StatusCodeResult(503);
}
context.ExceptionHandled = true;
context.Result = new ObjectResult(new GreenfieldAPIError("ligthning-node-unavailable", $"The lightning node is unavailable ({context.Exception.GetType().Name}: {context.Exception.Message})")) { StatusCode = 503 };
// Do not mark handled, it is possible filters above have better errors
}
}
public abstract class LightningNodeApiController : Controller
@@ -49,10 +42,6 @@ namespace BTCPayServer.Controllers.GreenField
public virtual async Task<IActionResult> GetInfo(string cryptoCode)
{
var lightningClient = await GetLightningClient(cryptoCode, true);
if (lightningClient == null)
{
return NotFound();
}
var info = await lightningClient.GetInfo();
return Ok(new LightningNodeInformationData()
{
@@ -64,11 +53,6 @@ namespace BTCPayServer.Controllers.GreenField
public virtual async Task<IActionResult> ConnectToNode(string cryptoCode, ConnectToNodeRequest request)
{
var lightningClient = await GetLightningClient(cryptoCode, true);
if (lightningClient == null)
{
return NotFound();
}
if (request?.NodeURI is null)
{
ModelState.AddModelError(nameof(request.NodeURI), "A valid node info was not provided to connect to");
@@ -94,10 +78,6 @@ namespace BTCPayServer.Controllers.GreenField
public virtual async Task<IActionResult> GetChannels(string cryptoCode)
{
var lightningClient = await GetLightningClient(cryptoCode, true);
if (lightningClient == null)
{
return NotFound();
}
var channels = await lightningClient.ListChannels();
return Ok(channels.Select(channel => new LightningChannelData()
@@ -198,10 +178,6 @@ namespace BTCPayServer.Controllers.GreenField
{
var lightningClient = await GetLightningClient(cryptoCode, true);
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
if (lightningClient == null || network == null)
{
return NotFound();
}
if (lightningInvoice?.BOLT11 is null ||
!BOLT11PaymentRequest.TryParse(lightningInvoice.BOLT11, out _, network.NBitcoinNetwork))
@@ -248,12 +224,6 @@ namespace BTCPayServer.Controllers.GreenField
public virtual async Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request)
{
var lightningClient = await GetLightningClient(cryptoCode, false);
if (lightningClient == null)
{
return NotFound();
}
if (request.Amount < LightMoney.Zero)
{
ModelState.AddModelError(nameof(request.Amount), "Amount should be more or equals to 0");
@@ -285,6 +255,23 @@ namespace BTCPayServer.Controllers.GreenField
}
}
protected JsonHttpException ErrorLightningNodeNotConfiguredForStore()
{
return new JsonHttpException(this.CreateAPIError(404, "lightning-not-configured", "The lightning node is not set up"));
}
protected JsonHttpException ErrorInternalLightningNodeNotConfigured()
{
return new JsonHttpException(this.CreateAPIError(404, "lightning-not-configured", "The internal lightning node is not set up"));
}
protected JsonHttpException ErrorCryptoCodeNotFound()
{
return new JsonHttpException(this.CreateAPIError(404, "unknown-cryptocode", "This crypto code isn't set up in this BTCPay Server instance"));
}
protected JsonHttpException ErrorShouldBeAdminForInternalNode()
{
return new JsonHttpException(this.CreateAPIError(403, "insufficient-api-permissions", "The user should be admin to use the internal lightning node"));
}
private LightningInvoiceData ToModel(LightningInvoice invoice)
{
return new LightningInvoiceData()