Skip to content

Commit

Permalink
WSCL: unify error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
onvej-sl committed Jun 5, 2023
1 parent 6909b0c commit bd25508
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,56 @@
}
]
}
},
{
"name": "",
"request": {
"transactions": [
{
"internalInputs": [
{
"address": "bcrt1p0hghqgks9z83pwh3a3jzq3eldgzj8ezjrs247szks3w2nq7s7fhsqtqgg7",
"value": 20000000
}
],
"externalInputs": [],
"internalOutputs": [],
"externalOutputs": []
}
]
},
"expectedStatusCode": 500,
"expectedResponse": {
"description": "There is an internal input that references a non-existing transaction."
}
},
{
"name": "",
"request": {},
"expectedStatusCode": 500,
"expectedResponse": {
"description": "The Transactions field is required."
}
},
{
"name": "",
"request": {
"transactions": [
{
"internalInputs": [
{
"value": ""
}
],
"externalInputs": [],
"internalOutputs": [],
"externalOutputs": []
}
]
},
"expectedStatusCode": 500,
"expectedResponse": {
"description": "Unable to cast object of type 'System.String' to type 'System.Nullable`1[System.Int64]'."
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace WalletWasabi.WabiSabiClientLibrary.Controllers;

[ApiController]
[ExceptionTranslateFilter]
[ValidationFilter]
[Produces("application/json")]
public class Controller : ControllerBase, IDisposable
{
Expand Down
19 changes: 19 additions & 0 deletions WalletWasabi.WabiSabiClientLibrary/Filters/ValidationFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;

namespace WalletWasabi.WabiSabiClientLibrary.Filters;

public class ValidationFilterAttribute : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (!context.ModelState.IsValid)
{
string messages = string.Join("; ", context.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
throw new Exception(messages);
}

await next();
}
}
3 changes: 3 additions & 0 deletions WalletWasabi.WabiSabiClientLibrary/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static async Task Main(string[] args)
builder.Services.AddControllers().AddNewtonsoftJson(x =>
{
x.SerializerSettings.Converters = JsonSerializationOptions.Default.Settings.Converters;
}).ConfigureApiBehaviorOptions(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
builder.Services.AddSingleton<Global>(global);
#if (DEBUG)
Expand Down

0 comments on commit bd25508

Please sign in to comment.