Round #725

10 May 2026, 15:14· csharp· Meta-Llama-3.3-70B-Instruct· r13-clean-hints
Complexity
band 0
ZP / UO / OBS
3 / 3 / 1
Journey weight
444
Lightshift ΔL

Prompt

Implement IExceptionHandler (the .NET 8 abstraction) that maps domain exceptions to ProblemDetails with trace_id from Activity.Current.

Zero-point index (what the engine surfaced)

ZPI-ZPI-0024 | 2026-05-10T15:14:57Z Implement IExceptionHandler (the .NET 8 abstraction) that maps domain exceptions to ProblemDetails with trace_id from Activity.Current. ZERO POINT — 3 entries (structural invariants) ● hsl(7.18, 100%, 50%) class [feature] ● hsl(0.00, 100%, 50%) public [feature] ● hsl(84.10, 100%, 50%) namespace [feature] UNOBSERVED — 3 entries (implied truths) ○ hsl(83.08, 100%, 42%) ILogger of T injection [function] ○ hsl(86.77, 100%, 7%) Structured log message [function] ○ hsl(90.46, 100%, 7%) High-performance logging [function] OBSERVED — 1 entries (explicit in request) ◉ hsl(98.46, 100%, 50%) with [feature] COMPLEXITY │ 7 entries │ spread: 15° │ band: LOW

Baseline response (no ZPI)

To implement an `IExceptionHandler` in .NET 8 that maps domain exceptions to `ProblemDetails` and includes the `trace_id` from `Activity.Current`, you can follow these steps. This example assumes you are working within an ASP.NET Core application, where `ProblemDetails` is commonly used for error handling and response formatting. First, ensure you have the necessary packages installed. For ASP.NET Core and the `ProblemDetails` class, you typically need `Microsoft.AspNetCore.App` or specific packages like `Microsoft.AspNetCore.Diagnostics` for error handling. Here's a basic implementation of an `IExceptionHandler` that maps domain exceptions to `ProblemDetails` with a `trace_id`: ```csharp using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using System; using System.Diagnostics; using System.Threading.Tasks; public class DomainExceptionHandler : IExceptionHandler { public async Task HandleAsync(HttpContext context, ExceptionDispatchInfo exceptionDispatchInfo) { var exception = exceptionDispatchInfo.SourceException; var problemDetails = new ProblemDetails { Status = StatusCodes.Status500InternalServerError, Title = "An error occurred while processing your request.", Detail = exception.Message, Type = "https://example.com/errors/internal-server-error" }; // Get the trace_id from Activity.Current var activity = Activity.Current; if (activity != null) { problemDetails.Extensions.Add("trace_id", activity.TraceId.ToString()); } // Map domain exceptions to specific ProblemDetails if needed if (exception is DomainException domainException) { problemDetails.Status = StatusCodes.Status400BadRequest; problemDetails.Title = "Bad Request"; problemDetails.Detail = domainException.Message; problemDetails.Type = "https://example.com/errors/bad-request"; } context.Response.StatusCode = problemDetails.Status; context.Response.ContentType = "application/problem+json"; await context.Response.WriteAsync(problemDetails.ToString()); } } // Example DomainException class public class DomainException : Exception { public DomainException(string message) : base(message) { } } ``` To use this exception handler in your ASP.NET Core application, you'll need to register it in the `Program.cs` file (for .NET 6 and later) or `Startup.cs` (for earlier versions). Here's how you might do it in `Program.cs`: ```csharp var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Register the custom exception handler builder.Services.AddSingleton<IExceptionHandler, DomainExceptionHandler>(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(appError => { appError.Run(async context => { await context.RequestServices.GetService<IExceptionHandler>()?.HandleAsync(context, ExceptionDispatchInfo.Capture(context.Features.Get<IExceptionHandlerFeature>()?.Error)); }); }); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); ``` This setup will catch unhandled exceptions, map them to `ProblemDetails`, and include the `trace_id` from `Activity.Current` in the response. You can customize the `ProblemDetails` mapping based on your specific domain exceptions and requirements.

Enhanced response (with ZPI)

Here's an implementation of the `IExceptionHandler` interface that maps domain exceptions to `ProblemDetails` with a `trace_id` from `Activity.Current`: ```csharp using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using System; using System.Threading.Tasks; public class DomainExceptionHandler : IExceptionHandler { private readonly ILogger<DomainExceptionHandler> _logger; public DomainExceptionHandler(ILogger<DomainExceptionHandler> logger) { _logger = logger; } public async Task HandleAsync(HttpContext context, Exception exception) { var problemDetails = new ProblemDetails { Status = StatusCodes.Status500InternalServerError, Title = "An error occurred", Detail = exception.Message, Type = "https://example.com/errors/internal-server-error", Instance = Activity.Current?.Id }; // Log the exception with a structured log message _logger.LogError(exception, "An error occurred while processing the request"); // Set the response status code and content context.Response.StatusCode = problemDetails.Status; context.Response.ContentType = "application/problem+json"; // Write the ProblemDetails to the response body await context.Response.WriteAsync(problemDetails.ToString()); } } ``` In this implementation, we're using the `ILogger<T>` interface to log the exception with a structured log message. We're also using the `Activity.Current` property to get the current activity's ID, which we're using as the `trace_id` in the `ProblemDetails` object. To register this exception handler in the DI container, you can add the following code to the `Program.cs` file: ```csharp builder.Services.AddSingleton<IExceptionHandler, DomainExceptionHandler>(); ``` This will register the `DomainExceptionHandler` class as a singleton instance of the `IExceptionHandler` interface. Note that this implementation assumes that you're using the `Microsoft.AspNetCore.Http` and `Microsoft.Extensions.Logging` packages. If you're using a different logging library, you may need to modify the logging code accordingly. Also, you can customize the `ProblemDetails` object to include additional information, such as the exception type, stack trace, or other relevant details. However, be careful not to include sensitive information that could be used to exploit the system.

Grader chain

Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.

V1samba/Meta-Llama-3.3-70B-Instruct
Better
V4qwen2.5-coder-3b-instruct
Better
V5openrouter/qwen/qwen3-235b-a22b-2507
Better
V7qwen2.5-coder-7b-instruct
Better
V8openai/gpt-4.1
Worse
V9anthropic/claude-opus-4-7
Worse
V12openai/gpt-4o
Better

Comments

Our grader said what it said. What do you say? Comment as a guest below.

No comments yet. Be the first to say what you make of this round.

← back to all rounds