blob: 906ac566e27317fe2fa2fb008c4252c8ad831942 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Timeline.Models.Http;
namespace Timeline.Helpers
{
public static class InvalidModelResponseFactory
{
public static IActionResult Factory(ActionContext context)
{
if (context.HttpContext.Request.Path.StartsWithSegments("/api/v2"))
{
return new UnprocessableEntityObjectResult(new CommonResponse(ErrorCodes.Common.InvalidModel, "Request is of bad format."));
}
var modelState = context.ModelState;
var messageBuilder = new StringBuilder();
foreach (var model in modelState)
foreach (var error in model.Value.Errors)
{
messageBuilder.Append(model.Key);
messageBuilder.Append(" : ");
messageBuilder.AppendLine(error.ErrorMessage);
}
return new BadRequestObjectResult(new CommonResponse(ErrorCodes.Common.InvalidModel, $"Request format is bad. {messageBuilder}"));
}
}
}
|