|
1 | | -// Supress warnings for this file |
2 | | -#pragma warning disable RCS1194 |
3 | | - |
4 | 1 | namespace JsonApiToolkit.Models.Errors; |
5 | 2 |
|
| 3 | +/// <summary> |
| 4 | +/// Base class for all JSON:API exceptions that provides comprehensive error information. |
| 5 | +/// </summary> |
| 6 | +/// <remarks> |
| 7 | +/// This base class supports the full JSON:API error object specification, allowing |
| 8 | +/// for detailed error reporting with structured metadata, source information, and error codes. |
| 9 | +/// </remarks> |
| 10 | +public abstract class JsonApiException : Exception |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// HTTP status code for this error. |
| 14 | + /// </summary> |
| 15 | + public int StatusCode { get; } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Application-specific error code for categorizing the error. |
| 19 | + /// </summary> |
| 20 | + public string? Code { get; } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Source information indicating where the error occurred. |
| 24 | + /// </summary> |
| 25 | + public ErrorSource? ErrorSource { get; } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Additional metadata about the error. |
| 29 | + /// </summary> |
| 30 | + public Dictionary<string, object>? Meta { get; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initializes a new instance of the JsonApiException class. |
| 34 | + /// </summary> |
| 35 | + /// <param name="statusCode">HTTP status code</param> |
| 36 | + /// <param name="message">Error message</param> |
| 37 | + /// <param name="code">Application-specific error code</param> |
| 38 | + /// <param name="errorSource">Source information</param> |
| 39 | + /// <param name="meta">Additional metadata</param> |
| 40 | + /// <param name="innerException">Inner exception</param> |
| 41 | + protected JsonApiException( |
| 42 | + int statusCode, |
| 43 | + string message, |
| 44 | + string? code = null, |
| 45 | + ErrorSource? errorSource = null, |
| 46 | + Dictionary<string, object>? meta = null, |
| 47 | + Exception? innerException = null |
| 48 | + ) |
| 49 | + : base(message, innerException) |
| 50 | + { |
| 51 | + StatusCode = statusCode; |
| 52 | + Code = code; |
| 53 | + ErrorSource = errorSource; |
| 54 | + Meta = meta; |
| 55 | + } |
| 56 | +} |
| 57 | + |
6 | 58 | /// <summary> |
7 | 59 | /// Exception representing a 400 Bad Request error. |
8 | 60 | /// </summary> |
9 | | -public class JsonApiBadRequestException(string message) : Exception(message) { } |
| 61 | +public class JsonApiBadRequestException : JsonApiException |
| 62 | +{ |
| 63 | + /// <summary> |
| 64 | + /// Initializes a new instance of the JsonApiBadRequestException class. |
| 65 | + /// </summary> |
| 66 | + /// <param name="message">Error message</param> |
| 67 | + public JsonApiBadRequestException(string message) |
| 68 | + : base(400, message) { } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Initializes a new instance of the JsonApiBadRequestException class with detailed error information. |
| 72 | + /// </summary> |
| 73 | + /// <param name="message">Error message</param> |
| 74 | + /// <param name="code">Application-specific error code</param> |
| 75 | + /// <param name="errorSource">Source information</param> |
| 76 | + /// <param name="meta">Additional metadata</param> |
| 77 | + /// <param name="innerException">Inner exception</param> |
| 78 | + public JsonApiBadRequestException( |
| 79 | + string message, |
| 80 | + string? code = null, |
| 81 | + ErrorSource? errorSource = null, |
| 82 | + Dictionary<string, object>? meta = null, |
| 83 | + Exception? innerException = null |
| 84 | + ) |
| 85 | + : base(400, message, code, errorSource, meta, innerException) { } |
| 86 | +} |
10 | 87 |
|
11 | 88 | /// <summary> |
12 | 89 | /// Exception representing a 404 Not Found error. |
13 | 90 | /// </summary> |
14 | | -public class JsonApiNotFoundException(string message) : Exception(message) { } |
| 91 | +public class JsonApiNotFoundException : JsonApiException |
| 92 | +{ |
| 93 | + /// <summary> |
| 94 | + /// Initializes a new instance of the JsonApiNotFoundException class. |
| 95 | + /// </summary> |
| 96 | + /// <param name="message">Error message</param> |
| 97 | + public JsonApiNotFoundException(string message) |
| 98 | + : base(404, message) { } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Initializes a new instance of the JsonApiNotFoundException class with detailed error information. |
| 102 | + /// </summary> |
| 103 | + /// <param name="message">Error message</param> |
| 104 | + /// <param name="code">Application-specific error code</param> |
| 105 | + /// <param name="errorSource">Source information</param> |
| 106 | + /// <param name="meta">Additional metadata</param> |
| 107 | + /// <param name="innerException">Inner exception</param> |
| 108 | + public JsonApiNotFoundException( |
| 109 | + string message, |
| 110 | + string? code = null, |
| 111 | + ErrorSource? errorSource = null, |
| 112 | + Dictionary<string, object>? meta = null, |
| 113 | + Exception? innerException = null |
| 114 | + ) |
| 115 | + : base(404, message, code, errorSource, meta, innerException) { } |
| 116 | +} |
15 | 117 |
|
16 | 118 | /// <summary> |
17 | 119 | /// Exception representing a 409 Conflict error. |
18 | 120 | /// </summary> |
19 | | -public class JsonApiConflictException(string message) : Exception(message) { } |
| 121 | +public class JsonApiConflictException : JsonApiException |
| 122 | +{ |
| 123 | + /// <summary> |
| 124 | + /// Initializes a new instance of the JsonApiConflictException class. |
| 125 | + /// </summary> |
| 126 | + /// <param name="message">Error message</param> |
| 127 | + public JsonApiConflictException(string message) |
| 128 | + : base(409, message) { } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Initializes a new instance of the JsonApiConflictException class with detailed error information. |
| 132 | + /// </summary> |
| 133 | + /// <param name="message">Error message</param> |
| 134 | + /// <param name="code">Application-specific error code</param> |
| 135 | + /// <param name="errorSource">Source information</param> |
| 136 | + /// <param name="meta">Additional metadata</param> |
| 137 | + /// <param name="innerException">Inner exception</param> |
| 138 | + public JsonApiConflictException( |
| 139 | + string message, |
| 140 | + string? code = null, |
| 141 | + ErrorSource? errorSource = null, |
| 142 | + Dictionary<string, object>? meta = null, |
| 143 | + Exception? innerException = null |
| 144 | + ) |
| 145 | + : base(409, message, code, errorSource, meta, innerException) { } |
| 146 | +} |
20 | 147 |
|
21 | 148 | /// <summary> |
22 | 149 | /// Exception representing a 401 Unauthorized error. |
23 | 150 | /// </summary> |
24 | | -public class JsonApiUnauthorizedException(string message) : Exception(message) { } |
| 151 | +public class JsonApiUnauthorizedException : JsonApiException |
| 152 | +{ |
| 153 | + /// <summary> |
| 154 | + /// Initializes a new instance of the JsonApiUnauthorizedException class. |
| 155 | + /// </summary> |
| 156 | + /// <param name="message">Error message</param> |
| 157 | + public JsonApiUnauthorizedException(string message) |
| 158 | + : base(401, message) { } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Initializes a new instance of the JsonApiUnauthorizedException class with detailed error information. |
| 162 | + /// </summary> |
| 163 | + /// <param name="message">Error message</param> |
| 164 | + /// <param name="code">Application-specific error code</param> |
| 165 | + /// <param name="errorSource">Source information</param> |
| 166 | + /// <param name="meta">Additional metadata</param> |
| 167 | + /// <param name="innerException">Inner exception</param> |
| 168 | + public JsonApiUnauthorizedException( |
| 169 | + string message, |
| 170 | + string? code = null, |
| 171 | + ErrorSource? errorSource = null, |
| 172 | + Dictionary<string, object>? meta = null, |
| 173 | + Exception? innerException = null |
| 174 | + ) |
| 175 | + : base(401, message, code, errorSource, meta, innerException) { } |
| 176 | +} |
25 | 177 |
|
26 | 178 | /// <summary> |
27 | 179 | /// Exception representing a 403 Forbidden error. |
28 | 180 | /// </summary> |
29 | | -public class JsonApiForbiddenException(string message) : Exception(message) { } |
| 181 | +public class JsonApiForbiddenException : JsonApiException |
| 182 | +{ |
| 183 | + /// <summary> |
| 184 | + /// Initializes a new instance of the JsonApiForbiddenException class. |
| 185 | + /// </summary> |
| 186 | + /// <param name="message">Error message</param> |
| 187 | + public JsonApiForbiddenException(string message) |
| 188 | + : base(403, message) { } |
| 189 | + |
| 190 | + /// <summary> |
| 191 | + /// Initializes a new instance of the JsonApiForbiddenException class with detailed error information. |
| 192 | + /// </summary> |
| 193 | + /// <param name="message">Error message</param> |
| 194 | + /// <param name="code">Application-specific error code</param> |
| 195 | + /// <param name="errorSource">Source information</param> |
| 196 | + /// <param name="meta">Additional metadata</param> |
| 197 | + /// <param name="innerException">Inner exception</param> |
| 198 | + public JsonApiForbiddenException( |
| 199 | + string message, |
| 200 | + string? code = null, |
| 201 | + ErrorSource? errorSource = null, |
| 202 | + Dictionary<string, object>? meta = null, |
| 203 | + Exception? innerException = null |
| 204 | + ) |
| 205 | + : base(403, message, code, errorSource, meta, innerException) { } |
| 206 | +} |
30 | 207 |
|
31 | 208 | /// <summary> |
32 | 209 | /// Exception representing a 429 Too Many Requests error. |
33 | 210 | /// </summary> |
34 | | -public class JsonApiTooManyRequestsException(string message) : Exception(message) { } |
| 211 | +public class JsonApiTooManyRequestsException : JsonApiException |
| 212 | +{ |
| 213 | + /// <summary> |
| 214 | + /// Initializes a new instance of the JsonApiTooManyRequestsException class. |
| 215 | + /// </summary> |
| 216 | + /// <param name="message">Error message</param> |
| 217 | + public JsonApiTooManyRequestsException(string message) |
| 218 | + : base(429, message) { } |
| 219 | + |
| 220 | + /// <summary> |
| 221 | + /// Initializes a new instance of the JsonApiTooManyRequestsException class with detailed error information. |
| 222 | + /// </summary> |
| 223 | + /// <param name="message">Error message</param> |
| 224 | + /// <param name="code">Application-specific error code</param> |
| 225 | + /// <param name="errorSource">Source information</param> |
| 226 | + /// <param name="meta">Additional metadata</param> |
| 227 | + /// <param name="innerException">Inner exception</param> |
| 228 | + public JsonApiTooManyRequestsException( |
| 229 | + string message, |
| 230 | + string? code = null, |
| 231 | + ErrorSource? errorSource = null, |
| 232 | + Dictionary<string, object>? meta = null, |
| 233 | + Exception? innerException = null |
| 234 | + ) |
| 235 | + : base(429, message, code, errorSource, meta, innerException) { } |
| 236 | +} |
0 commit comments