-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemException.cs
More file actions
208 lines (181 loc) · 6.88 KB
/
ProblemException.cs
File metadata and controls
208 lines (181 loc) · 6.88 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
namespace SharedCode.Problems;
/// <summary>
/// Exception that represents a Problem Details failure ( <see href="https://tools.ietf.org/html/rfc7807"/>).
/// </summary>
public class ProblemException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="ProblemException"/> class with a <see cref="Problem"/>.
/// </summary>
/// <param name="problem">The <see cref="Problem"/> that caused this exception.</param>
public ProblemException(Problem problem) : base(GetMessage(problem))
{
this.Problem = problem;
this.PopulateData(problem);
}
/// <summary>
/// Initializes a new instance of the <see cref="ProblemException"/> class with a title.
/// </summary>
/// <param name="title">A short, human-readable summary of the problem type.</param>
public ProblemException(string title)
: this(Problem.Create(title, title))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ProblemException"/> class with a title and detail.
/// </summary>
/// <param name="title">A short, human-readable summary of the problem type.</param>
/// <param name="detail">
/// A human-readable explanation specific to this occurrence of the problem.
/// </param>
public ProblemException(string title, string detail)
: this(Problem.Create(title, detail))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ProblemException"/> class with a title,
/// detail, and status code.
/// </summary>
/// <param name="title">A short, human-readable summary of the problem type.</param>
/// <param name="detail">
/// A human-readable explanation specific to this occurrence of the problem.
/// </param>
/// <param name="statusCode">
/// The HTTP status code generated by the origin server for this occurrence of the problem.
/// </param>
public ProblemException(string title, string detail, int statusCode)
: this(Problem.Create(title, detail, statusCode))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ProblemException"/> class with an inner exception.
/// </summary>
/// <param name="innerException">The exception to create a <see cref="Problem"/> from.</param>
public ProblemException(Exception innerException)
: this(Problem.Create(innerException))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ProblemException"/> class.
/// </summary>
public ProblemException()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ProblemException"/> class with a specified
/// error message and inner exception.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">
/// The exception that is the cause of the current exception, or <c>null</c> if no inner
/// exception is specified.
/// </param>
public ProblemException(string message, Exception innerException) : base(message, innerException)
{
}
/// <summary>
/// Gets the problem detail.
/// </summary>
public string? Detail => string.IsNullOrEmpty(this.Problem.Detail) ? null : this.Problem.Detail;
/// <summary>
/// Gets the error code if available.
/// </summary>
public string ErrorCode => this.Problem.ErrorCode;
/// <summary>
/// Checks if this is a validation problem.
/// </summary>
public bool IsValidationProblem => this.Problem.Type == "https://tools.ietf.org/html/rfc7231#section-6.5.1";
/// <summary>
/// Gets the <see cref="Problem"/> that caused this exception.
/// </summary>
[SuppressMessage("Style", "GCop443:Remove redundant auto-property initialization", Justification = "Needed for reference nullability.")]
public Problem Problem { get; } = default!;
/// <summary>
/// Gets the HTTP status code from the <see cref="Problem"/>.
/// </summary>
public int StatusCode => this.Problem.StatusCode;
/// <summary>
/// Gets the problem title.
/// </summary>
public string? Title => string.IsNullOrEmpty(this.Problem.Title) ? null : this.Problem.Title;
/// <summary>
/// Gets the problem type URI.
/// </summary>
[SuppressMessage("Naming", "CA1721:Property names should not match get methods", Justification = "<Pending>")]
public string? Type => string.IsNullOrEmpty(this.Problem.Type) || this.Problem.Type == "https://httpstatuses.io/0" ? null : this.Problem.Type;
/// <summary>
/// Gets the validation errors if this is a validation problem.
/// </summary>
public Dictionary<string, List<string>>? ValidationErrors => this.Problem.GetValidationErrors();
/// <summary>
/// Creates a <see cref="ProblemException"/> from a <see cref="Problem"/>.
/// </summary>
/// <param name="problem">The <see cref="Problem"/> to convert.</param>
/// <returns>A new <see cref="ProblemException"/> instance.</returns>
public static ProblemException FromProblem(Problem problem) => new(problem);
private static string GetMessage(Problem problem)
{
// Create a detailed message based on problem type
if (problem.Type == "https://tools.ietf.org/html/rfc7231#section-6.5.1")
{
// Validation error
var validationErrors = problem.GetValidationErrors();
if (validationErrors?.Count > 0)
{
var errors = new List<string>();
foreach (var error in validationErrors)
{
errors.Add($"{error.Key}: {string.Join(", ", error.Value)}");
}
return $"Validation failed: {string.Join("; ", errors)}";
}
}
// Generic message construction
var parts = new List<string>();
if (!string.IsNullOrEmpty(problem.Title))
{
parts.Add(problem.Title!);
}
if (!string.IsNullOrEmpty(problem.Detail))
{
parts.Add(problem.Detail!);
}
if (problem.StatusCode > 0)
{
parts.Add($"(HTTP {problem.StatusCode})");
}
if (!string.IsNullOrEmpty(problem.ErrorCode))
{
parts.Add($"[{problem.ErrorCode}]");
}
return parts.Count > 0 ? string.Join(" - ", parts) : "An error occurred";
}
private void PopulateData(Problem problem)
{
// Add problem data to exception Data collection
this.Data[$"{nameof(this.Problem)}.{nameof(problem.Type)}"] = problem.Type;
this.Data[$"{nameof(this.Problem)}.{nameof(problem.Title)}"] = problem.Title;
this.Data[$"{nameof(this.Problem)}.{nameof(problem.StatusCode)}"] = problem.StatusCode;
this.Data[$"{nameof(this.Problem)}.{nameof(problem.Detail)}"] = problem.Detail;
this.Data[$"{nameof(this.Problem)}.{nameof(problem.Instance)}"] = problem.Instance;
// Add extensions to Data
foreach (var extension in problem.Extensions)
{
this.Data[$"{nameof(this.Problem)}.{nameof(problem.Extensions)}.{extension.Key}"] = extension.Value;
}
// Handle error code if present
if (problem.ErrorCode is not null)
{
this.Data[$"{nameof(this.Problem)}.{nameof(problem.ErrorCode)}"] = problem.ErrorCode;
}
// Handle validation errors if present
var validationErrors = problem.GetValidationErrors()?.ToList();
if (validationErrors is not null)
{
foreach (var error in validationErrors)
{
this.Data[$"{nameof(this.Problem)}.ValidationError.{error.Key}"] = string.Join("; ", error.Value);
}
}
}
}