-
-
Notifications
You must be signed in to change notification settings - Fork 780
Expand file tree
/
Copy pathValidationApiException.cs
More file actions
63 lines (55 loc) · 2.24 KB
/
ValidationApiException.cs
File metadata and controls
63 lines (55 loc) · 2.24 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
using System.Text.Json;
namespace Refit
{
/// <summary>
/// An ApiException that is raised according to RFC 7807, which contains problem details for validation exceptions.
/// </summary>
[Serializable]
public class ValidationApiException : ApiException
{
static readonly JsonSerializerOptions SerializerOptions = new();
static ValidationApiException()
{
SerializerOptions.PropertyNameCaseInsensitive = true;
SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
SerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
}
ValidationApiException(ApiException apiException)
: base(
apiException.RequestMessage,
apiException.HttpMethod,
apiException.Content,
apiException.StatusCode,
apiException.ReasonPhrase,
apiException.Headers,
apiException.RefitSettings
) { }
/// <summary>
/// Creates a new instance of a ValidationException from an existing ApiException.
/// </summary>
/// <param name="exception">An instance of an ApiException to use to build a ValidationException.</param>
/// <returns>ValidationApiException</returns>
public static ValidationApiException Create(ApiException exception)
{
if (exception is null)
throw new ArgumentNullException(nameof(exception));
if (string.IsNullOrWhiteSpace(exception.Content))
throw new ArgumentException(
"Content must be an 'application/problem+json' compliant json string."
);
var ex = new ValidationApiException(exception);
if (!string.IsNullOrWhiteSpace(exception.Content))
{
ex.Content = JsonSerializer.Deserialize<ProblemDetails>(
exception.Content!,
SerializerOptions
);
}
return ex;
}
/// <summary>
/// The problem details of the RFC 7807 validation exception.
/// </summary>
public new ProblemDetails? Content { get; private set; }
}
}