-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathA2AExceptionExtensions.cs
More file actions
84 lines (75 loc) · 3.77 KB
/
A2AExceptionExtensions.cs
File metadata and controls
84 lines (75 loc) · 3.77 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
namespace A2A.AspNetCore
{
/// <summary>
/// Provides extension methods for <see cref="A2AException"/>.
/// </summary>
public static class A2AExceptionExtensions
{
private const string RequestIdKey = "RequestId";
/// <summary>
/// Associates a request ID with the specified <see cref="A2AException"/>.
/// </summary>
/// <param name="exception">The <see cref="A2AException"/> to associate the request ID with.</param>
/// <param name="requestId">The request ID to associate with the exception. Can be null.</param>
/// <returns>The same <see cref="A2AException"/> instance with the request ID stored in its Data collection.</returns>
/// <remarks>
/// This method stores the request ID in the exception's Data collection using the key "RequestId".
/// The request ID can be later retrieved using the <see cref="GetRequestId"/> method.
/// This is useful for correlating exceptions with specific HTTP requests in logging and debugging scenarios.
/// </remarks>
public static A2AException WithRequestId(this A2AException exception, string? requestId)
{
if (exception is null)
{
throw new ArgumentNullException(nameof(exception));
}
exception.Data[RequestIdKey] = requestId;
return exception;
}
/// <summary>
/// Associates a request ID with the specified <see cref="A2AException"/>.
/// </summary>
/// <param name="exception">The <see cref="A2AException"/> to associate the request ID with.</param>
/// <param name="requestId">The request ID to associate with the exception.</param>
/// <returns>The same <see cref="A2AException"/> instance with the request ID stored in its Data collection.</returns>
/// <remarks>
/// This method stores the request ID in the exception's Data collection using the key "RequestId".
/// The request ID can be later retrieved using the <see cref="GetRequestId"/> method.
/// This is useful for correlating exceptions with specific HTTP requests in logging and debugging scenarios.
/// </remarks>
public static A2AException WithRequestId(this A2AException exception, JsonRpcId requestId)
{
if (exception is null)
{
throw new ArgumentNullException(nameof(exception));
}
exception.Data[RequestIdKey] = requestId.ToString();
return exception;
}
/// <summary>
/// Retrieves the request ID associated with the specified <see cref="A2AException"/>.
/// </summary>
/// <param name="exception">The <see cref="A2AException"/> to retrieve the request ID from.</param>
/// <returns>
/// The request ID associated with the exception if one was previously set using <see cref="WithRequestId(A2AException, string?)"/>,
/// or null if no request ID was set or if the stored value is not a string.
/// </returns>
/// <remarks>
/// This method retrieves the request ID from the exception's Data collection using the key "RequestId".
/// If the stored value is not a string or doesn't exist, null is returned.
/// This method is typically used in exception handlers to correlate exceptions with specific HTTP requests.
/// </remarks>
public static string? GetRequestId(this A2AException exception)
{
if (exception is null)
{
throw new ArgumentNullException(nameof(exception));
}
if (exception.Data[RequestIdKey] is string requestIdString)
{
return requestIdString;
}
return null;
}
}
}