Merge pull request #2446 from NicolasDorier/fweihnq

Give better error for greenfield API
This commit is contained in:
Nicolas Dorier 2021-04-20 18:25:10 +09:00 committed by GitHub
commit b6bd7cce6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 18 deletions

View file

@ -23,5 +23,9 @@ namespace BTCPayServer.Controllers.GreenField
{ {
return controller.BadRequest(new GreenfieldAPIError(errorCode, errorMessage)); return controller.BadRequest(new GreenfieldAPIError(errorCode, errorMessage));
} }
public static IActionResult CreateAPIError(this ControllerBase controller, int httpCode, string errorCode, string errorMessage)
{
return controller.StatusCode(httpCode, new GreenfieldAPIError(errorCode, errorMessage));
}
} }
} }

View file

@ -52,7 +52,7 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return StoreNotFound();
} }
var invoices = var invoices =
@ -74,13 +74,13 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return InvoiceNotFound();
} }
var invoice = await _invoiceRepository.GetInvoice(invoiceId, true); var invoice = await _invoiceRepository.GetInvoice(invoiceId, true);
if (invoice?.StoreId != store.Id) if (invoice?.StoreId != store.Id)
{ {
return NotFound(); return InvoiceNotFound();
} }
return Ok(ToModel(invoice)); return Ok(ToModel(invoice));
@ -94,9 +94,13 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return InvoiceNotFound();
}
var invoice = await _invoiceRepository.GetInvoice(invoiceId, true);
if (invoice?.StoreId != store.Id)
{
return InvoiceNotFound();
} }
await _invoiceRepository.ToggleInvoiceArchival(invoiceId, true, storeId); await _invoiceRepository.ToggleInvoiceArchival(invoiceId, true, storeId);
return Ok(); return Ok();
} }
@ -109,7 +113,7 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return InvoiceNotFound();
} }
var result = await _invoiceRepository.UpdateInvoiceMetadata(invoiceId, storeId, request.Metadata); var result = await _invoiceRepository.UpdateInvoiceMetadata(invoiceId, storeId, request.Metadata);
@ -118,7 +122,7 @@ namespace BTCPayServer.Controllers.GreenField
return Ok(ToModel(result)); return Ok(ToModel(result));
} }
return NotFound(); return InvoiceNotFound();
} }
[Authorize(Policy = Policies.CanCreateInvoice, [Authorize(Policy = Policies.CanCreateInvoice,
@ -129,7 +133,7 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return StoreNotFound();
} }
if (request.Amount < 0.0m) if (request.Amount < 0.0m)
@ -206,13 +210,13 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return InvoiceNotFound();
} }
var invoice = await _invoiceRepository.GetInvoice(invoiceId, true); var invoice = await _invoiceRepository.GetInvoice(invoiceId, true);
if (invoice.StoreId != store.Id) if (invoice.StoreId != store.Id)
{ {
return NotFound(); return InvoiceNotFound();
} }
if (!await _invoiceRepository.MarkInvoiceStatus(invoice.Id, request.Status)) if (!await _invoiceRepository.MarkInvoiceStatus(invoice.Id, request.Status))
@ -235,13 +239,13 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return InvoiceNotFound();
} }
var invoice = await _invoiceRepository.GetInvoice(invoiceId, true); var invoice = await _invoiceRepository.GetInvoice(invoiceId, true);
if (invoice.StoreId != store.Id) if (invoice.StoreId != store.Id)
{ {
return NotFound(); return InvoiceNotFound();
} }
if (!invoice.Archived) if (!invoice.Archived)
@ -265,13 +269,13 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return InvoiceNotFound();
} }
var invoice = await _invoiceRepository.GetInvoice(invoiceId, true); var invoice = await _invoiceRepository.GetInvoice(invoiceId, true);
if (invoice?.StoreId != store.Id) if (invoice?.StoreId != store.Id)
{ {
return NotFound(); return InvoiceNotFound();
} }
return Ok(ToPaymentMethodModels(invoice)); return Ok(ToPaymentMethodModels(invoice));
@ -285,13 +289,13 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData(); var store = HttpContext.GetStoreData();
if (store == null) if (store == null)
{ {
return NotFound(); return InvoiceNotFound();
} }
var invoice = await _invoiceRepository.GetInvoice(invoiceId, true); var invoice = await _invoiceRepository.GetInvoice(invoiceId, true);
if (invoice?.StoreId != store.Id) if (invoice?.StoreId != store.Id)
{ {
return NotFound(); return InvoiceNotFound();
} }
if (PaymentMethodId.TryParse(paymentMethod, out var paymentMethodId)) if (PaymentMethodId.TryParse(paymentMethod, out var paymentMethodId))
@ -300,7 +304,17 @@ namespace BTCPayServer.Controllers.GreenField
_paymentMethodHandlerDictionary, store, invoice, paymentMethodId); _paymentMethodHandlerDictionary, store, invoice, paymentMethodId);
return Ok(); return Ok();
} }
return BadRequest(); ModelState.AddModelError(nameof(paymentMethod), "Invalid payment method");
return this.CreateValidationError(ModelState);
}
private IActionResult InvoiceNotFound()
{
return this.CreateAPIError(404, "invoice-not-found", "The invoice was not found");
}
private IActionResult StoreNotFound()
{
return this.CreateAPIError(404, "store-not-found", "The store was not found");
} }
private InvoicePaymentMethodDataModel[] ToPaymentMethodModels(InvoiceEntity entity) private InvoicePaymentMethodDataModel[] ToPaymentMethodModels(InvoiceEntity entity)