Get the FREE Ultimate OpenClaw Setup Guide →

ln-772-error-handler-setup

npx machina-cli add skill levnikolaevich/claude-code-skills/ln-772-error-handler-setup --openclaw
Files (1)
SKILL.md
6.2 KB

ln-772-error-handler-setup

Type: L3 Worker Category: 7XX Project Bootstrap Parent: ln-770-crosscutting-setup

Configures global error handling for .NET and Python backend applications.


Overview

AspectDetails
InputContext Store from ln-770
OutputException handling middleware and custom exceptions
Stacks.NET (ASP.NET Core Middleware), Python (FastAPI exception handlers)

Phase 1: Receive Context

Accept Context Store from coordinator.

Required Context:

  • STACK: .NET or Python
  • FRAMEWORK: ASP.NET Core or FastAPI
  • PROJECT_ROOT: Project directory path
  • ENVIRONMENT: Development or Production

Idempotency Check:

  • .NET: Grep for GlobalExceptionMiddleware or UseExceptionHandler
  • Python: Grep for @app.exception_handler or exception_handlers.py
  • If found: Return { "status": "skipped" }

Phase 2: Research Error Handling Patterns

Use MCP tools to get up-to-date documentation.

For .NET:

MCP ref: "ASP.NET Core global exception handling middleware"
Context7: /dotnet/aspnetcore

For Python:

MCP ref: "FastAPI exception handlers custom exceptions"
Context7: /tiangolo/fastapi

Key Patterns to Research:

  1. Middleware pipeline positioning
  2. Exception type mapping to HTTP status codes
  3. ProblemDetails (RFC 7807) format
  4. Development vs Production error details
  5. Logging integration

Phase 3: Decision Points

Q1: Error Response Format

OptionDescription
ProblemDetails (RFC 7807) (Recommended)Standardized format, widely adopted
Custom FormatProject-specific requirements

Q2: Error Detail Level

EnvironmentStack TraceInner ExceptionsRequest Details
Development✓ Show✓ Show✓ Show
Production✗ Hide✗ Hide✗ Hide

Q3: Error Taxonomy

Define standard error codes:

CodeHTTP StatusDescription
VALIDATION_ERROR400Invalid input data
UNAUTHORIZED401Authentication required
FORBIDDEN403Insufficient permissions
NOT_FOUND404Resource not found
CONFLICT409Resource state conflict
INTERNAL_ERROR500Unexpected server error

Phase 4: Generate Configuration

.NET Output Files

FilePurpose
Middleware/GlobalExceptionMiddleware.csException handling middleware
Exceptions/AppException.csBase exception class
Exceptions/ValidationException.csValidation errors
Exceptions/NotFoundException.csNot found errors
Models/ErrorResponse.csError response model

Generation Process:

  1. Use MCP ref to get current ASP.NET Core exception handling patterns
  2. Generate GlobalExceptionMiddleware with:
    • Exception type to HTTP status mapping
    • Logging of exceptions
    • ProblemDetails response format
    • Environment-aware detail level
  3. Generate custom exception classes

Registration Code:

app.UseMiddleware<GlobalExceptionMiddleware>();

Python Output Files

FilePurpose
exceptions/app_exceptions.pyCustom exception classes
exceptions/handlers.pyFastAPI exception handlers
models/error_response.pyPydantic error models

Generation Process:

  1. Use MCP ref to get current FastAPI exception handling patterns
  2. Generate exception handlers with:
    • HTTPException handling
    • Custom AppException handling
    • Validation error handling
    • Request validation error handling
  3. Generate custom exception classes

Registration Code:

app.add_exception_handler(AppException, app_exception_handler)
app.add_exception_handler(RequestValidationError, validation_exception_handler)

Phase 5: Validate

Validation Steps:

  1. Syntax check:

    • .NET: dotnet build --no-restore
    • Python: python -m py_compile exceptions/handlers.py
  2. Test error handling:

    • Create test endpoint that throws exception
    • Verify error response format
    • Check that stack trace hidden in Production

Expected Error Response (ProblemDetails):

{
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "Validation Error",
  "status": 400,
  "detail": "Invalid input data",
  "instance": "/api/users",
  "errors": [
    { "field": "email", "message": "Invalid email format" }
  ],
  "traceId": "abc-123-def-456"
}

Return to Coordinator

{
  "status": "success",
  "files_created": [
    "Middleware/GlobalExceptionMiddleware.cs",
    "Exceptions/AppException.cs",
    "Models/ErrorResponse.cs"
  ],
  "packages_added": [],
  "registration_code": "app.UseMiddleware<GlobalExceptionMiddleware>();",
  "message": "Configured global exception handling"
}

Reference Links


Critical Rules

  • Use ProblemDetails (RFC 7807) by default — standardized error response format
  • Hide stack traces in Production — environment-aware detail level is mandatory
  • Use MCP ref for current patterns — do not hardcode middleware from memory
  • Idempotent — if GlobalExceptionMiddleware or exception_handlers.py exists, return status: "skipped"
  • Map all custom exceptions to HTTP status codes — no unhandled exception types reaching the client

Definition of Done

  • Context Store received (stack, framework, environment)
  • Error handling patterns researched via MCP tools
  • GlobalExceptionMiddleware generated (.NET) or exception handlers generated (Python)
  • Custom exception classes created (AppException, ValidationException, NotFoundException)
  • Error response model created (ProblemDetails format)
  • Syntax validated (dotnet build or py_compile)
  • Structured JSON response returned to ln-770 coordinator

Version: 2.0.0 Last Updated: 2026-01-10

Source

git clone https://github.com/levnikolaevich/claude-code-skills/blob/master/ln-772-error-handler-setup/SKILL.mdView on GitHub

Overview

ln-772-error-handler-setup configures a global exception handling layer for both .NET (ASP.NET Core) and Python (FastAPI) backends. It outputs a unified error response format (ProblemDetails/RFC 7807) and uses environment-aware detail exposure to enable consistent error propagation and centralized logging across services.

How This Skill Works

It consumes the Context Store (STACK, FRAMEWORK, PROJECT_ROOT, ENVIRONMENT), performs an idempotency check for existing middleware, and uses MCP-guided patterns to decide on a ProblemDetails-based response with consistent HTTP status mappings. It then generates .NET and Python output files (middleware, exceptions, models) and registers them in the app pipeline (e.g., UseMiddleware for .NET or app.add_exception_handler for FastAPI).

When to Use It

  • Bootstrapping a new .NET or FastAPI project and implementing standardized error responses
  • Migrating legacy error handling to RFC 7807 ProblemDetails and a unified error taxonomy
  • Enforcing development vs production detail levels to avoid leaking sensitive info
  • Standardizing exception-to-HTTP status mappings across services (400/401/404/409/500)
  • Integrating centralized error handling into CI/CD and project bootstrap processes

Quick Start

  1. Step 1: Provide Context (STACK, FRAMEWORK, PROJECT_ROOT, ENVIRONMENT) from the coordinator
  2. Step 2: Generate Middleware/Handlers and error models for .NET and Python, following MCP references
  3. Step 3: Register the middleware or exception handlers in your app startup (e.g., UseMiddleware or add_exception_handler) and test with sample requests

Best Practices

  • Adopt RFC 7807 ProblemDetails as the standard error response format
  • Map exceptions to consistent HTTP status codes (e.g., 400, 401, 403, 404, 409, 500)
  • Show full error details only in Development; suppress in Production
  • Centralize logging of all exceptions with correlation IDs
  • Test error paths comprehensively, including validation and not-found scenarios, across .NET and Python

Example Use Cases

  • ASP.NET Core app uses GlobalExceptionMiddleware to return ProblemDetails on unhandled exceptions
  • FastAPI app registers app_exception_handler for AppException and RequestValidationError
  • Custom NotFoundException yields 404 with a structured error payload
  • Validation errors surface as 400 with field-level details in ProblemDetails
  • Environment-based detail toggling prevents leaking stack traces in Production

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers