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).
This commit is contained in:
Dennis Reimann 2020-11-23 06:40:13 +01:00 committed by GitHub
parent fa517417ed
commit ff87319a74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 6 deletions

View file

@ -263,13 +263,29 @@ namespace BTCPayServer.Controllers.GreenField
{ {
return this.CreateValidationError(ModelState); 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 return this.CreateAPIError("invoice-error", ex.Message);
}, }
CancellationToken.None); throw;
return Ok(ToModel(invoice)); }
} }
private LightningInvoiceData ToModel(LightningInvoice invoice) private LightningInvoiceData ToModel(LightningInvoice invoice)

View file

@ -433,6 +433,16 @@
} }
} }
}, },
"400": {
"description": "Wellknown error codes are: `invoice-error`",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"503": { "503": {
"description": "Unable to access the lightning node" "description": "Unable to access the lightning node"
}, },

View file

@ -509,6 +509,16 @@
} }
} }
}, },
"400": {
"description": "Wellknown error codes are: `invoice-error`",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"503": { "503": {
"description": "Unable to access the lightning node" "description": "Unable to access the lightning node"
}, },