blob: 01a3d52deb66a94bbd1f4e14f8d69cf6634a1c20 (
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Linq;
using Timeline.Models.Http;
using Timeline.Services;
namespace Timeline.Filters
{
public class CatchEntityNotExistExceptionFilter : IExceptionFilter
{
private static string MakeMessage(EntityNotExistException e)
{
return string.Format(Resource.MessageEntityNotExist, e.EntityType.Name, e.GenerateConstraintString());
}
private static CommonResponse MakeCommonResponse(EntityNotExistException e)
{
return new CommonResponse(e.EntityType.NotExistErrorCode, MakeMessage(e));
}
public void OnException(ExceptionContext context)
{
if (context.Exception is EntityNotExistException e)
{
if (context.HttpContext.Request.Path.StartsWithSegments("/api/v2"))
{
context.Result = new NotFoundResult();
}
else
{
if (HttpMethods.IsGet(context.HttpContext.Request.Method))
{
context.Result = new NotFoundObjectResult(MakeCommonResponse(e));
}
else if (HttpMethods.IsDelete(context.HttpContext.Request.Method))
{
if (context.ActionDescriptor.EndpointMetadata.OfType<NotEntityDeleteAttribute>().Any())
{
context.Result = new BadRequestObjectResult(MakeCommonResponse(e));
}
else
{
context.Result = new OkObjectResult(CommonDeleteResponse.NotExist());
}
}
else
{
context.Result = new BadRequestObjectResult(MakeCommonResponse(e));
}
}
}
}
}
}
|