< Summary - Kestrun — Combined Coverage

Information
Class: Public.Response.Write-KrErrorResponse
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrErrorResponse.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 5
Coverable lines: 5
Total lines: 74
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 01:25:22 Line coverage: 0% (0/3) Total lines: 69 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/04/2025 - 22:37:32 Line coverage: 0% (0/3) Total lines: 70 Tag: Kestrun/Kestrun@afb7aadc0a8a42bfa2b51ea62c8a6e2cf63faec609/12/2025 - 03:43:11 Line coverage: 0% (0/5) Total lines: 74 Tag: Kestrun/Kestrun@d160286e3020330b1eb862d66a37db2e26fc9042

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrErrorResponse.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Writes an error response to the HTTP client.
 4    .DESCRIPTION
 5        This function allows you to send an error message or exception details back to the client.
 6    .PARAMETER Message
 7        The error message to send in the response. This is used when the error is a simple
 8        message rather than an exception.
 9    .PARAMETER Exception
 10        The exception object containing error details. This is used when you want to send
 11        detailed exception information back to the client.
 12    .PARAMETER StatusCode
 13        The HTTP status code to set for the response. Defaults to 500 (Internal Server Error).
 14    .PARAMETER ContentType
 15        The content type of the response. If not specified, defaults to "application/json".
 16    .PARAMETER Details
 17        Additional details to include in the error response. This can be used to provide
 18        more context about the error.
 19    .PARAMETER IncludeStack
 20        A switch to indicate whether to include the stack trace in the error response. This is useful for debugging purp
 21    .EXAMPLE
 22        Write-KrErrorResponse -Message "An error occurred while processing your request." -StatusCode 400 -ContentType "
 23        Writes a simple error message to the response with a 400 Bad Request status code and content type "application/j
 24    .EXAMPLE
 25        Write-KrErrorResponse -Exception $exception -StatusCode 500 -ContentType "application/json" -IncludeStack
 26        Writes the details of the provided exception to the response with a 500 Internal Server Error status
 27        code and content type "application/json". The stack trace is included in the response.
 28    .NOTES
 29        This function is designed to be used in the context of a Kestrun server response.
 30#>
 31function Write-KrErrorResponse {
 32    [KestrunRuntimeApi('Route')]
 33    [CmdletBinding(DefaultParameterSetName = 'Message')]
 34    param (
 35        [Parameter(ParameterSetName = 'Message', Mandatory = $true)]
 36        [string]$Message,
 37
 38        [Parameter(ParameterSetName = 'Exception', Mandatory = $true)]
 39        [System.Exception]$Exception,
 40
 41        [Parameter()]
 42        [int]$StatusCode = 500,
 43
 44        [Parameter()]
 45        [string]$ContentType,
 46
 47        [Parameter()]
 48        [string]$Details,
 49
 50        [Parameter()]
 51        [switch]$IncludeStack
 52    )
 53    # Only works inside a route script block where $Context is available
 054    if ($null -ne $Context.Response) {
 055        if ($PSCmdlet.ParameterSetName -eq 'Message') {
 056            $Context.Response.WriteErrorResponse(
 57                $Message,
 58                $StatusCode,
 59                $ContentType,
 60                $Details
 61            )
 62        } else {
 063            $Context.Response.WriteErrorResponse(
 64                $Exception,
 65                $StatusCode,
 66                $ContentType,
 67                $IncludeStack.IsPresent
 68            )
 69        }
 70    } else {
 071        Write-KrOutsideRouteWarning
 72    }
 73}
 74

Methods/Properties

Write-KrErrorResponse()