Error handling for ASP.NET Core APIs — exception middleware, ProblemDetails,
94
90%
Does it follow best practices?
Impact
100%
1.13xAverage score across 5 eval scenarios
Passed
No known issues
A financial analytics platform runs a public-facing ASP.NET Core API that serves long-running data queries (portfolio analysis, trend calculations). Users often navigate away mid-query — closing a browser tab or switching dashboards — which cancels the in-flight HTTP request. The operations team has raised two concerns from their monitoring dashboard:
OperationCanceledException at every level, making it hard to distinguish genuine failures from normal client navigation.You have been asked to fix the cancellation handling in the API. The existing exception middleware already handles domain errors and generic unhandled exceptions. You need to add proper handling so that when a client disconnects mid-request, the system responds appropriately without polluting the error metrics or flooding logs with noise.
The team also wants to make sure controller actions are written defensively so that database and service calls can actually be cancelled when the client disconnects — right now several actions don't propagate the cancellation signal at all.
Produce the following files in your working directory:
ExceptionMiddleware.cs — updated exception middleware with correct cancellation handling alongside the existing domain-error and generic-exception handlingQueryController.cs — a sample controller with two async GET endpoints (/api/queries/{id} and /api/queries/search) that correctly propagate the cancellation signal through to their (stubbed) service callscancellation_handling_notes.md — a short explanation (3–5 sentences) of what HTTP status code is used for cancelled requests, why it is chosen, and how cancelled requests are distinguished from real errors in the logsThe existing domain exception infrastructure is provided below. Extract the files before starting.
=============== FILE: inputs/AppException.cs =============== public abstract class AppException : Exception { public int StatusCode { get; } public string ErrorCode { get; }
protected AppException(int statusCode, string errorCode, string message)
: base(message)
{
StatusCode = statusCode;
ErrorCode = errorCode;
}}
public class NotFoundException : AppException { public NotFoundException(string resource, object id) : base(404, "NOT_FOUND", $"{resource} with id '{id}' was not found") { } }
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
skills
aspnet-error-handling
verifiers