< 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@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 3
Coverable lines: 3
Total lines: 70
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

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
 054    if ($PSCmdlet.ParameterSetName -eq 'Message') {
 055        $Context.Response.WriteErrorResponse(
 56            $Message,
 57            $StatusCode,
 58            $ContentType,
 59            $Details
 60        )
 61    } else {
 062        $Context.Response.WriteErrorResponse(
 63            $Exception,
 64            $StatusCode,
 65            $ContentType,
 66            $IncludeStack.IsPresent
 67        )
 68    }
 69}
 70

Methods/Properties

Write-KrErrorResponse()