-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathErrorsToHttpStatusCodeMapper.cs
65 lines (54 loc) · 1.99 KB
/
ErrorsToHttpStatusCodeMapper.cs
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
56
57
58
59
60
61
62
63
64
65
using Ocelot.Errors;
namespace Ocelot.Responder;
public class ErrorsToHttpStatusCodeMapper : IErrorsToHttpStatusCodeMapper
{
public int Map(List<Error> errors)
{
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthenticatedError))
{
return 401;
}
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthorizedError
|| e.Code == OcelotErrorCode.ClaimValueNotAuthorizedError
|| e.Code == OcelotErrorCode.ScopeNotAuthorizedError
|| e.Code == OcelotErrorCode.UserDoesNotHaveClaimError
|| e.Code == OcelotErrorCode.CannotFindClaimError))
{
return 403;
}
if (errors.Any(e => e.Code == OcelotErrorCode.QuotaExceededError))
{
return errors.Single(e => e.Code == OcelotErrorCode.QuotaExceededError).HttpStatusCode;
}
if (errors.Any(e => e.Code == OcelotErrorCode.RequestTimedOutError))
{
return 503;
}
if (errors.Any(e => e.Code == OcelotErrorCode.RequestCanceled))
{
// status code refer to
// https://stackoverflow.com/questions/46234679/what-is-the-correct-http-status-code-for-a-cancelled-request?answertab=votes#tab-top
// https://httpstatuses.com/499
return 499;
}
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToFindDownstreamRouteError))
{
return 404;
}
if (errors.Any(e => e.Code == OcelotErrorCode.ConnectionToDownstreamServiceError))
{
return 502;
}
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToCompleteRequestError
|| e.Code == OcelotErrorCode.CouldNotFindLoadBalancerCreator
|| e.Code == OcelotErrorCode.ErrorInvokingLoadBalancerCreator))
{
return 500;
}
if (errors.Any(e => e.Code == OcelotErrorCode.PayloadTooLargeError))
{
return 413;
}
return 404;
}
}