Exception Handling
Robust error handling patterns for PowerShell and C# routes, with consistent API responses.
Overview
Kestrun exposes a flexible exception handling middleware that can:
- Re-execute to a dedicated error path
- Run a custom handler (PowerShell script or C# inline code)
- Emit RFC 7807 ProblemDetails
- Show developer exception page (development only)
These modes are driven by Enable-KrExceptionHandling and work uniformly for PowerShell, C#, and VB.NET routes.
When to use each mode
- Re-execute to Path: Centralize rendering of user-friendly error pages or JSON payloads.
- PowerShell Handler: Author error formatting in PowerShell; great for PS-first apps.
- C# Inline Handler: Maximum performance with typed access to
Context. - ProblemDetails: API-first services that want standardized machine-readable errors.
- Developer Exception Page: Local development diagnostics only.
Middleware ordering
Place exception handling early in the pipeline, before routing that might short-circuit errors, but after diagnostics you still want to run.
Common placement:
- Logging / correlation
- Exception handling
- Static files / compression
- Routing and endpoints
PowerShell vs C# behavior
- PowerShell routes can handle their own exceptions. If you want the middleware to handle PS exceptions, throw outside route blocks or ensure the middleware is configured to capture them.
- C# and other compiled handlers naturally flow exceptions into the middleware pipeline.
Note: Custom exception handlers implemented as PowerShell scripts are not supported at this time. Use C# or VB.NET for custom exception handling middleware.
ProblemDetails contracts
When -UseProblemDetails is enabled, unhandled exceptions become JSON with fields including:
- type: A URI reference that identifies the problem type (optional)
- title: Short, human-readable summary
- status: HTTP status code
- detail: Human-readable explanation
- traceId: Correlation identifier for diagnostics
Clients should not rely on exact wording. Contract shape follows RFC 7807.
Developer exception page
- Contains stack traces and, optionally, source snippets. Guard with environment checks.
- Use
-IncludeDetailsInDevelopmentand tweak-SourceCodeLineCountfor context length.
Cmdlets and APIs
- PowerShell:
Enable-KrExceptionHandling,Write-KrJsonResponse,Write-KrTextResponse - C#:
Context.Response.WriteJsonResponse,WriteTextResponse, etc.
Tutorials
- Re-execute to Path
- PowerShell Handler
- C# Inline Handler
- ProblemDetails Fallback
- Developer Exception Page
Security considerations
- Do not enable developer exception pages in production.
- Avoid leaking stack traces or sensitive details in ProblemDetails. Use generic titles and include only safe fields.
References
Return to the Guides index.