-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathODataException.cs
147 lines (143 loc) · 8.46 KB
/
ODataException.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SenseNet.OData
{
/// <summary>
/// Represents the qualification of an error in the OData response.
/// </summary>
public enum ODataExceptionCode
{
/// <summary>Means: error is not qualified.</summary>
NotSpecified,
/// <summary>General request error.</summary>
RequestError,
/// <summary>Content id is in wrong format.</summary>
InvalidId,
/// <summary>The value is invalid in the $top parameter.</summary>
InvalidTopParameter,
/// <summary>The value is negative in the $top parameter.</summary>
NegativeTopParameter,
/// <summary>The value is invalid in the $skip parameter.</summary>
InvalidSkipParameter,
/// <summary>The value is negative in the $skip parameter.</summary>
NegativeSkipParameter,
/// <summary>The value is invalid in the $inlinecount parameter.</summary>
InvalidInlineCountParameter,
/// <summary>The value is invalid in the $format parameter.</summary>
InvalidFormatParameter,
/// <summary>The value is invalid in the $orderby parameter.</summary>
InvalidOrderByParameter,
/// <summary>The direction is invalid in the $orderby parameter.</summary>
InvalidOrderByDirectionParameter,
/// <summary>The value is invalid in the $expand parameter.</summary>
InvalidExpandParameter,
/// <summary>The value is invalid in the $select parameter.</summary>
InvalidSelectParameter,
/// <summary>Means: cannot create the content because it already exists.</summary>
ContentAlreadyExists,
/// <summary>The requested resource was not found. The equivalent HTTP status code: 404.</summary>
ResourceNotFound,
/// <summary>The value cannot serialize to JSON format.</summary>
CannotConvertToJSON,
/// <summary>An exception occurs when an action is not an OData operation or an OData action is invoked with HTTP GET.</summary>
IllegalInvoke,
/// <summary>The current user has not enough permission for accessing the requested resource. The equivalent HTTP status code: 403.</summary>
Forbidden,
/// <summary>User is not authorized. The equivalent HTTP status code: 401.</summary>
Unauthorized
}
/// <summary>
/// Represents a general error in OData.
/// </summary>
[Serializable]
public class ODataException : Exception
{
/// <summary>
/// Gets the <see cref="ODataExceptionCode"/> of the error.
/// </summary>
public ODataExceptionCode ODataExceptionCode { get; private set; }
/// <summary>
/// Gets the string representation of the ODataExceptionCode property.
/// The value can be extension of the <see cref="ODataExceptionCode"/> enumeration.
/// </summary>
public string ErrorCode { get; internal set; }
/// <summary>
/// Gets the string representation of the HTTP Status Cose (e.g. "403 Forbidden").
/// </summary>
public string HttpStatus { get; private set; }
/// <summary>
/// Gets the HTTP Status Code (e.g. 403).
/// </summary>
public int HttpStatusCode { get; private set; }
/// <summary>
/// Initializes a new instance of the ODataException with a general message and the given error code.
/// </summary>
/// <param name="code">The <see cref="ODataExceptionCode"/> of the exception.</param>
public ODataException(ODataExceptionCode code) : base(String.Format("An exception occured during the processing an OData request. Code: {0} ({1})", Convert.ToInt32(code), code.ToString())) { Initialize(code); }
/// <summary>
/// Initializes a new instance of the ODataException with the given parameters.
/// </summary>
/// <param name="message">The message of the error.</param>
/// <param name="code">The <see cref="ODataExceptionCode"/> of the exception.</param>
public ODataException(string message, ODataExceptionCode code) : base(message) { Initialize(code); }
/// <summary>
/// Initializes a new instance of the ODataException with the given parameters.
/// </summary>
/// <param name="message">The message of the error.</param>
/// <param name="code">The <see cref="ODataExceptionCode"/> of the exception.</param>
/// <param name="inner">The wrapped exception.</param>
public ODataException(string message, ODataExceptionCode code, Exception inner) : base(message, inner) { Initialize(code); }
/// <summary>
/// Initializes a new instance of the ODataException with the given parameters.
/// </summary>
/// <param name="code">The <see cref="ODataExceptionCode"/> of the exception.</param>
/// <param name="inner">The wrapped exception.</param>
public ODataException(ODataExceptionCode code, Exception inner) : base(GetRelevantMessage(inner), inner) { Initialize(code); }
/// <inheritdoc />
protected ODataException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
private const string HTTPERROR200 = "200 Ok";
private const string HTTPERROR400 = "400 Bad Request";
private const string HTTPERROR401 = "401 Unauthorized";
private const string HTTPERROR403 = "403 Forbidden";
private const string HTTPERROR404 = "404 Not Found";
private const string HTTPERROR500 = "500 Internal Server Error";
private void Initialize(ODataExceptionCode exceptionCode)
{
ODataExceptionCode = exceptionCode;
switch (exceptionCode)
{
case ODataExceptionCode.NotSpecified: HttpStatusCode = 500; HttpStatus = HTTPERROR500; break;
case ODataExceptionCode.RequestError: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.InvalidTopParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.NegativeTopParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.InvalidSkipParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.NegativeSkipParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.InvalidInlineCountParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.InvalidFormatParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.InvalidOrderByParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.InvalidOrderByDirectionParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.InvalidExpandParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.InvalidSelectParameter: HttpStatusCode = 400; HttpStatus = HTTPERROR400; break;
case ODataExceptionCode.ContentAlreadyExists: HttpStatusCode = 403; HttpStatus = HTTPERROR403; break;
case ODataExceptionCode.ResourceNotFound: HttpStatusCode = 404; HttpStatus = HTTPERROR404; break;
case ODataExceptionCode.CannotConvertToJSON: HttpStatusCode = 500; HttpStatus = HTTPERROR500; break;
case ODataExceptionCode.Forbidden: HttpStatusCode = 403; HttpStatus = HTTPERROR403; break;
case ODataExceptionCode.Unauthorized: HttpStatusCode = 401; HttpStatus = HTTPERROR401; break;
default: HttpStatusCode = 500; HttpStatus = HTTPERROR500; break;
}
}
private static string GetRelevantMessage(Exception e)
{
var ee = e;
while (ee != null)
{
if (ee.Message != "Exception has been thrown by the target of an invocation.")
break;
ee = ee.InnerException;
}
return ee == null ? e.Message : ee.Message;
}
}
}