forked from RehanSaeed/Serilog.Exceptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiExceptionDestructurer.cs
71 lines (63 loc) · 3.02 KB
/
ApiExceptionDestructurer.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
namespace Serilog.Exceptions.Refit.Destructurers
{
using System;
using System.Collections.Generic;
using global::Refit;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.Destructurers;
/// <summary>
/// A destructurer for the Refit <see cref="ApiException"/>.
/// </summary>
/// <seealso cref="ExceptionDestructurer" />
public class ApiExceptionDestructurer : ExceptionDestructurer
{
private readonly bool destructureCommonExceptionProperties;
private readonly bool destructureHttpContent;
/// <summary>
/// Initializes a new instance of the <see cref="ApiExceptionDestructurer"/> class.
/// </summary>
/// <param name="destructureCommonExceptionProperties">Destructure common public Exception properties or not.</param>
/// <param name="destructureHttpContent">Destructure the HTTP body. This is left optional due to possible security and log size concerns.</param>
public ApiExceptionDestructurer(bool destructureCommonExceptionProperties = true, bool destructureHttpContent = false)
{
this.destructureCommonExceptionProperties = destructureCommonExceptionProperties;
this.destructureHttpContent = destructureHttpContent;
}
/// <inheritdoc cref="IExceptionDestructurer.TargetTypes"/>
public override Type[] TargetTypes => new[] { typeof(ApiException) };
/// <inheritdoc />
public override void Destructure(Exception exception, IExceptionPropertiesBag propertiesBag, Func<Exception, IReadOnlyDictionary<string, object?>?> destructureException)
{
if (this.destructureCommonExceptionProperties)
{
base.Destructure(exception, propertiesBag, destructureException);
}
else
{
// Argument checks are usually done in <see cref="ExceptionDestructurer.Destructure"/>
// but as we didn't call this method we need to do the checks here.
if (exception is null)
{
throw new ArgumentNullException(nameof(propertiesBag));
}
if (propertiesBag is null)
{
throw new ArgumentNullException(nameof(propertiesBag));
}
if (destructureException is null)
{
throw new ArgumentNullException(nameof(destructureException));
}
}
#pragma warning disable CA1062 // Validate arguments of public methods
var apiException = (ApiException)exception;
if (this.destructureHttpContent)
{
propertiesBag.AddProperty(nameof(ApiException.Content), apiException.Content);
}
propertiesBag.AddProperty(nameof(ApiException.Uri), apiException.Uri);
propertiesBag.AddProperty(nameof(ApiException.StatusCode), apiException.StatusCode);
#pragma warning restore CA1062 // Validate arguments of public methods
}
}
}