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