blob: 469f2de556dbf517f41e2bc1b8eb4eb0dc48bfee (
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
31
32
33
34
35
|
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Timeline.Models.Http;
using Timeline.Services;
namespace Timeline.Filters
{
public class CatchEntityAlreadyExistExceptionFilter : IExceptionFilter
{
private static string MakeMessage(EntityAlreadyExistException e)
{
return string.Format(Resource.MessageEntityAlreadyExist, e.EntityType.Name, e.GenerateConstraintString());
}
private static CommonResponse MakeCommonResponse(EntityAlreadyExistException e)
{
return new CommonResponse(e.EntityType.ConflictErrorCode, MakeMessage(e));
}
public void OnException(ExceptionContext context)
{
if (context.Exception is EntityAlreadyExistException e)
{
if (context.HttpContext.Request.Path.StartsWithSegments("/api/v2"))
{
context.Result = new UnprocessableEntityObjectResult(new ErrorResponse(ErrorResponse.EntityExist, "An entity with given key already exists."));
}
else
{
context.Result = new BadRequestObjectResult(MakeCommonResponse(e));
}
}
}
}
}
|