From ff87319a741fda1d321187d5b6a6c37666d958f9 Mon Sep 17 00:00:00 2001 From: Dennis Reimann Date: Mon, 23 Nov 2020 06:40:13 +0100 Subject: [PATCH] API: Handle lightning invoice creation errors (#2070) Catches LightningClient exceptions and responds with a detailed API error (400) instead of a generic server failure (503). --- .../GreenField/LightningNodeApiController.cs | 28 +++++++++++++++---- .../swagger.template.lightning.internal.json | 10 +++++++ .../v1/swagger.template.lightning.store.json | 10 +++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/BTCPayServer/Controllers/GreenField/LightningNodeApiController.cs b/BTCPayServer/Controllers/GreenField/LightningNodeApiController.cs index d8eb1079b..5dcfef4bd 100644 --- a/BTCPayServer/Controllers/GreenField/LightningNodeApiController.cs +++ b/BTCPayServer/Controllers/GreenField/LightningNodeApiController.cs @@ -263,13 +263,29 @@ namespace BTCPayServer.Controllers.GreenField { return this.CreateValidationError(ModelState); } - var invoice = await lightningClient.CreateInvoice( - new CreateInvoiceParams(request.Amount, request.Description, request.Expiry) + + try + { + var invoice = await lightningClient.CreateInvoice( + new CreateInvoiceParams(request.Amount, request.Description, request.Expiry) + { + PrivateRouteHints = request.PrivateRouteHints + }, + CancellationToken.None); + return Ok(ToModel(invoice)); + } + catch (Exception ex) + { + var nameSpace = ex.GetType().Namespace; + var isLnClientError = nameSpace != null && + nameSpace.StartsWith("BTCPayServer.Lightning", + StringComparison.OrdinalIgnoreCase); + if (isLnClientError) { - PrivateRouteHints = request.PrivateRouteHints - }, - CancellationToken.None); - return Ok(ToModel(invoice)); + return this.CreateAPIError("invoice-error", ex.Message); + } + throw; + } } private LightningInvoiceData ToModel(LightningInvoice invoice) diff --git a/BTCPayServer/wwwroot/swagger/v1/swagger.template.lightning.internal.json b/BTCPayServer/wwwroot/swagger/v1/swagger.template.lightning.internal.json index 8d9b0bce0..0650e111b 100644 --- a/BTCPayServer/wwwroot/swagger/v1/swagger.template.lightning.internal.json +++ b/BTCPayServer/wwwroot/swagger/v1/swagger.template.lightning.internal.json @@ -433,6 +433,16 @@ } } }, + "400": { + "description": "Wellknown error codes are: `invoice-error`", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, "503": { "description": "Unable to access the lightning node" }, diff --git a/BTCPayServer/wwwroot/swagger/v1/swagger.template.lightning.store.json b/BTCPayServer/wwwroot/swagger/v1/swagger.template.lightning.store.json index b166fd982..be94d7104 100644 --- a/BTCPayServer/wwwroot/swagger/v1/swagger.template.lightning.store.json +++ b/BTCPayServer/wwwroot/swagger/v1/swagger.template.lightning.store.json @@ -509,6 +509,16 @@ } } }, + "400": { + "description": "Wellknown error codes are: `invoice-error`", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProblemDetails" + } + } + } + }, "503": { "description": "Unable to access the lightning node" },