-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExceptionExtensions.cs
63 lines (56 loc) · 2.16 KB
/
ExceptionExtensions.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
namespace TypedRest;
/// <summary>
/// Provides extension methods for <see cref="Exception"/>.
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// Concatenates the <see cref="Exception.Message"/>s of the entire <see cref="Exception.InnerException"/> tree.
/// </summary>
public static string GetFullMessage(this Exception exception)
{
var builder = new StringBuilder();
for (Exception? current = exception; current != null; current = current.InnerException)
builder.AppendLine(current.Message);
return builder.ToString();
}
private const string HttpHeadersKey = "Http-Headers", HttpBodyKey = "Http-Body";
/// <summary>
/// Stores HTTP response headers in the exception.
/// </summary>
public static void SetHttpResponseHeaders(this Exception exception, HttpResponseHeaders headers)
{
try
{
exception.Data[HttpHeadersKey] = headers;
}
catch
{
// .NET Framework does not allow non-serializable data in exceptions
}
}
/// <summary>
/// Retrieves HTTP response headers from the exception.
/// </summary>
public static HttpResponseHeaders? GetHttpResponseHeaders(this Exception exception)
=> exception.Data[HttpHeadersKey] as HttpResponseHeaders;
/// <summary>
/// Stores an HTTP response body in the exception.
/// </summary>
public static void SetHttpResponseBody(this Exception exception, string body)
=> exception.Data[HttpBodyKey] = body;
/// <summary>
/// Retrieves an HTTP response body from the exception.
/// </summary>
public static string? GetHttpResponseBody(this Exception exception)
=> exception.Data[HttpBodyKey] as string;
/// <summary>
/// Waits for the duration specified in Retry-After header if it was set.
/// </summary>
public static async Task HttpRetryDelayAsync(this Exception exception, CancellationToken cancellationToken = default)
{
var wait = exception.GetHttpResponseHeaders()?.RetryAfter?.Delta;
if (wait != null)
await Task.Delay(wait.Value, cancellationToken);
}
}