Skip to content

Commit fbe1f0e

Browse files
author
Vadim Belov
committed
Add SetTraceIdentifier to IHttpError and middleware
Introduce SetTraceIdentifier to IHttpError for trace correlation. Update exception middleware to set TraceIdentifier on errors. WebApiException implements the method (not yet implemented). This enables better tracking and diagnostics of errors.
1 parent 6d4947d commit fbe1f0e

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

Sources/EasyExtensions.AspNetCore/Exceptions/WebApiException.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ public ErrorModel GetErrorModel()
6767
};
6868
}
6969

70+
/// <summary>
71+
/// Sets the trace identifier for the current context.
72+
/// </summary>
73+
/// <param name="traceId">The unique identifier to associate with the current trace. Cannot be null.</param>
74+
/// <exception cref="NotImplementedException">Thrown when the method is called, as the implementation is not provided.</exception>
75+
public void SetTraceIdentifier(string traceId)
76+
{
77+
throw new NotImplementedException();
78+
}
79+
7080
private static string GetRfcType(HttpStatusCode statusCode)
7181
=> statusCode switch
7282
{

Sources/EasyExtensions.AspNetCore/Extensions/ServiceCollectionExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ private static async Task HandleException(HttpContext context)
199199
var exception = exceptionHandlerPathFeature.Error;
200200
if (exception is IHttpError httpError)
201201
{
202+
var traceId = context.TraceIdentifier;
203+
if (!string.IsNullOrEmpty(traceId))
204+
{
205+
httpError.SetTraceIdentifier(traceId);
206+
}
202207
context.Response.StatusCode = (int)httpError.StatusCode;
203208
await context.Response.WriteAsJsonAsync(httpError.GetErrorModel());
204209
}

Sources/EasyExtensions/Abstractions/IHttpError.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public interface IHttpError
1717
/// <returns> Error model. </returns>
1818
ErrorModel GetErrorModel();
1919

20+
/// <summary>
21+
/// Sets trace identifier for the error. This method can be used to associate a specific trace identifier
22+
/// with the error, which can be useful for tracking and correlation purposes in logging and diagnostics.
23+
/// </summary>
24+
/// <param name="traceId">The trace identifier to associate with the error.</param>
25+
void SetTraceIdentifier(string traceId);
26+
2027
/// <summary>
2128
/// Gets status code.
2229
/// </summary>

0 commit comments

Comments
 (0)