mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-03-03 17:36:59 +01:00
[Greenfield] if some json property are invalid, throw nice error instead of an exception (fix #2795)
This commit is contained in:
parent
e164b1f169
commit
8bea3dd21e
4 changed files with 12 additions and 2 deletions
|
@ -18,7 +18,6 @@ namespace BTCPayServer.Client
|
||||||
private readonly string _username;
|
private readonly string _username;
|
||||||
private readonly string _password;
|
private readonly string _password;
|
||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
|
|
||||||
public Uri Host => _btcpayHost;
|
public Uri Host => _btcpayHost;
|
||||||
|
|
||||||
public string APIKey => _apiKey;
|
public string APIKey => _apiKey;
|
||||||
|
@ -84,6 +83,13 @@ namespace BTCPayServer.Client
|
||||||
using var resp = await _httpClient.SendAsync(CreateHttpRequest(path, queryPayload, method), cancellationToken);
|
using var resp = await _httpClient.SendAsync(CreateHttpRequest(path, queryPayload, method), cancellationToken);
|
||||||
return await HandleResponse<T>(resp);
|
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,
|
protected virtual HttpRequestMessage CreateHttpRequest(string path,
|
||||||
Dictionary<string, object> queryPayload = null,
|
Dictionary<string, object> queryPayload = null,
|
||||||
HttpMethod method = null)
|
HttpMethod method = null)
|
||||||
|
|
|
@ -1592,6 +1592,9 @@ namespace BTCPayServer.Tests
|
||||||
Assert.Equal(firstAddress, (await viewOnlyClient.PreviewProposedStoreOnChainPaymentMethodAddresses(store.Id, "BTC",
|
Assert.Equal(firstAddress, (await viewOnlyClient.PreviewProposedStoreOnChainPaymentMethodAddresses(store.Id, "BTC",
|
||||||
new UpdateOnChainPaymentMethodRequest() { Enabled = true, DerivationScheme = xpub })).Addresses.First().Address);
|
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",
|
var method = await client.UpdateStoreOnChainPaymentMethod(store.Id, "BTC",
|
||||||
new UpdateOnChainPaymentMethodRequest() { Enabled = true, DerivationScheme = xpub });
|
new UpdateOnChainPaymentMethodRequest() { Enabled = true, DerivationScheme = xpub });
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace BTCPayServer.Filters
|
||||||
{
|
{
|
||||||
if (context.Exception is NBitcoin.JsonConverters.JsonObjectException jsonObject)
|
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;
|
context.ExceptionHandled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,6 +122,7 @@ namespace BTCPayServer.Hosting
|
||||||
if (!Configuration.GetOrDefault<bool>("nocsp", false))
|
if (!Configuration.GetOrDefault<bool>("nocsp", false))
|
||||||
o.Filters.Add(new ContentSecurityPolicyAttribute(CSPTemplate.AntiXSS));
|
o.Filters.Add(new ContentSecurityPolicyAttribute(CSPTemplate.AntiXSS));
|
||||||
o.Filters.Add(new JsonHttpExceptionFilter());
|
o.Filters.Add(new JsonHttpExceptionFilter());
|
||||||
|
o.Filters.Add(new JsonObjectExceptionFilter());
|
||||||
})
|
})
|
||||||
.ConfigureApiBehaviorOptions(options =>
|
.ConfigureApiBehaviorOptions(options =>
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue