CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl-labs/aspnet-error-handling

Error handling for ASP.NET Core APIs — exception middleware, ProblemDetails,

94

1.13x
Quality

90%

Does it follow best practices?

Impact

100%

1.13x

Average score across 5 eval scenarios

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

task.mdevals/scenario-2/

Analytics Query API — Request Cancellation Handling

Problem Description

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:

  1. Cancelled requests show up as 500 errors in their alerting system, inflating the error rate and triggering false-alarm pages to on-call engineers.
  2. When they look at server logs for these "errors", the log entries contain full .NET exception stack traces with 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.

Output Specification

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 handling
  • QueryController.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 calls
  • cancellation_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 logs

The 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

tile.json