Merge pull request #3279 from NicolasDorier/quofwe

[Greenfield] if some json property are invalid, throw nice error instead of an exception (Fix #2795)
This commit is contained in:
Nicolas Dorier
2022-01-10 23:56:57 +09:00
committed by GitHub
5 changed files with 21 additions and 4 deletions

View File

@@ -18,7 +18,6 @@ namespace BTCPayServer.Client
private readonly string _username;
private readonly string _password;
private readonly HttpClient _httpClient;
public Uri Host => _btcpayHost;
public string APIKey => _apiKey;
@@ -84,6 +83,13 @@ namespace BTCPayServer.Client
using var resp = await _httpClient.SendAsync(CreateHttpRequest(path, queryPayload, method), cancellationToken);
return await HandleResponse<T>(resp);
}
public async Task<T> SendHttpRequest<T>(string path,
object bodyPayload = null,
HttpMethod method = null, CancellationToken cancellationToken = default)
{
using var resp = await _httpClient.SendAsync(CreateHttpRequest(path: path, bodyPayload: bodyPayload, method: method), cancellationToken);
return await HandleResponse<T>(resp);
}
protected virtual HttpRequestMessage CreateHttpRequest(string path,
Dictionary<string, object> queryPayload = null,
HttpMethod method = null)

View File

@@ -1592,6 +1592,9 @@ namespace BTCPayServer.Tests
Assert.Equal(firstAddress, (await viewOnlyClient.PreviewProposedStoreOnChainPaymentMethodAddresses(store.Id, "BTC",
new UpdateOnChainPaymentMethodRequest() { Enabled = true, DerivationScheme = xpub })).Addresses.First().Address);
await AssertValidationError(new[] { "accountKeyPath" }, () => viewOnlyClient.SendHttpRequest<GreenfieldValidationError[]>(path: $"api/v1/stores/{store.Id}/payment-methods/Onchain/BTC/preview", method: HttpMethod.Post,
bodyPayload: JObject.Parse("{\"accountKeyPath\": \"0/1\"}")));
var method = await client.UpdateStoreOnChainPaymentMethod(store.Id, "BTC",
new UpdateOnChainPaymentMethodRequest() { Enabled = true, DerivationScheme = xpub });

View File

@@ -14,7 +14,7 @@ namespace BTCPayServer.Filters
{
if (context.Exception is NBitcoin.JsonConverters.JsonObjectException jsonObject)
{
context.Result = new ObjectResult(new GreenfieldValidationError(jsonObject.Path, jsonObject.Message)) { StatusCode = 400 };
context.Result = new ObjectResult(new[] { new GreenfieldValidationError(jsonObject.Path, jsonObject.Message) }) { StatusCode = 422 };
context.ExceptionHandled = true;
}
}

View File

@@ -122,6 +122,7 @@ namespace BTCPayServer.Hosting
if (!Configuration.GetOrDefault<bool>("nocsp", false))
o.Filters.Add(new ContentSecurityPolicyAttribute(CSPTemplate.AntiXSS));
o.Filters.Add(new JsonHttpExceptionFilter());
o.Filters.Add(new JsonObjectExceptionFilter());
})
.ConfigureApiBehaviorOptions(options =>
{

View File

@@ -360,13 +360,20 @@
}
],
"description": "View addresses of a proposed payment method of the store",
"operationId": "StoreOnChainPaymentMethods_GetOnChainPaymentMethodPreview",
"operationId": "StoreOnChainPaymentMethods_POSTOnChainPaymentMethodPreview",
"requestBody": {
"x-name": "request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OnChainPaymentMethodDataPreview"
"type": "object",
"properties": {
"derivationScheme": {
"type": "string",
"description": "The derivation scheme",
"example": "xpub..."
}
}
}
}
},