| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Enables exception handling middleware for a Kestrun server instance. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This cmdlet configures the exception handling middleware for a Kestrun server instance, |
| | | 6 | | allowing for customizable error handling and response generation. |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun server instance (resolved if omitted via Resolve-KestrunServer). |
| | | 9 | | .PARAMETER ExceptionHandlingPath |
| | | 10 | | The path to re-execute when an exception occurs (e.g., '/error'). |
| | | 11 | | .PARAMETER CreateScopeForErrors |
| | | 12 | | If specified, creates a new scope for handling errors. |
| | | 13 | | .PARAMETER AllowStatusCode404Response |
| | | 14 | | If specified, allows handling of 404 status code responses. |
| | | 15 | | .PARAMETER IncludeDetailsInDevelopment |
| | | 16 | | If specified, includes detailed error information when the environment is set to Development. |
| | | 17 | | .PARAMETER UseProblemDetails |
| | | 18 | | If specified, formats error responses using the Problem Details standard (RFC 7807). |
| | | 19 | | .PARAMETER Compress |
| | | 20 | | If specified, compress the json response for error handling. |
| | | 21 | | .PARAMETER DeveloperExceptionPage |
| | | 22 | | If specified, enables the Developer Exception Page middleware with default options. |
| | | 23 | | .PARAMETER SourceCodeLineCount |
| | | 24 | | The number of source code lines to display around the error line in the Developer Exception Page. |
| | | 25 | | .PARAMETER SourceCodePath |
| | | 26 | | The base path to use for locating source code files in the Developer Exception Page. |
| | | 27 | | .PARAMETER LanguageOptions |
| | | 28 | | A LanguageOptions object defining the scripting language, code, references, imports, and arguments |
| | | 29 | | for custom error handling logic. |
| | | 30 | | .PARAMETER ScriptBlock |
| | | 31 | | A PowerShell script block to execute for custom error handling logic. |
| | | 32 | | .PARAMETER Code |
| | | 33 | | A string containing the code to execute for custom error handling logic. |
| | | 34 | | .PARAMETER Language |
| | | 35 | | The scripting language of the provided code (e.g., PowerShell, CSharp, VisualBasic). |
| | | 36 | | .PARAMETER CodeFilePath |
| | | 37 | | The file path to a script file containing the code to execute for custom error handling logic. |
| | | 38 | | .PARAMETER ExtraRefs |
| | | 39 | | An array of additional assemblies to reference when executing the custom error handling code. |
| | | 40 | | .PARAMETER Arguments |
| | | 41 | | A hashtable of arguments to pass to the custom error handling code. |
| | | 42 | | .PARAMETER ExtraImports |
| | | 43 | | An array of additional namespaces to import when executing the custom error handling code. |
| | | 44 | | .PARAMETER PassThru |
| | | 45 | | If specified, returns the modified Kestrun server instance. |
| | | 46 | | .OUTPUTS |
| | | 47 | | The modified Kestrun server instance if the PassThru parameter is specified; otherwise, no output. |
| | | 48 | | .EXAMPLE |
| | | 49 | | Enable-KrExceptionHandling -ExceptionHandlingPath '/error' -CreateScopeForErrors -AllowStatusCode404Response -PassTh |
| | | 50 | | Enables exception handling middleware on the default Kestrun server instance, |
| | | 51 | | re-executing requests to '/error' when exceptions occur, creating a scope for error handling, |
| | | 52 | | and allowing handling of 404 status code responses. The modified server instance is returned. |
| | | 53 | | .EXAMPLE |
| | | 54 | | Enable-KrExceptionHandling -Server $myServer -DeveloperExceptionPage -SourceCodeLineCount 10 -SourceCodePath 'C:\MyA |
| | | 55 | | Enables the Developer Exception Page middleware on the specified Kestrun server instance, |
| | | 56 | | displaying 10 lines of source code around the error line and using 'C:\MyApp' as the base path for source files. |
| | | 57 | | .EXAMPLE |
| | | 58 | | $langOptions = [Kestrun.Hosting.Options.LanguageOptions]::new() |
| | | 59 | | $langOptions.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 60 | | $langOptions.Code = { param($exception) "An error occurred: $($exception.Message)" } |
| | | 61 | | Enable-KrExceptionHandling -Server $myServer -LanguageOptions $langOptions |
| | | 62 | | Enables custom exception handling on the specified Kestrun server instance, |
| | | 63 | | executing the provided PowerShell code block when an exception occurs. The code block receives the exception object |
| | | 64 | | The custom error handling logic can be defined using the LanguageOptions, ScriptBlock, Code, or CodeFilePath paramet |
| | | 65 | | .NOTES |
| | | 66 | | This function is part of the Kestrun PowerShell module and is used to manage Kestrun servers |
| | | 67 | | and their middleware components. |
| | | 68 | | #> |
| | | 69 | | function Enable-KrExceptionHandling { |
| | | 70 | | [KestrunRuntimeApi('Definition')] |
| | | 71 | | [CmdletBinding(DefaultParameterSetName = 'Default')] |
| | | 72 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 73 | | param( |
| | | 74 | | [Parameter(ValueFromPipeline = $true)] |
| | | 75 | | [Kestrun.Hosting.KestrunHost] $Server, |
| | | 76 | | |
| | | 77 | | # Redirect |
| | | 78 | | [Parameter(Mandatory = $true, ParameterSetName = 'ExceptionHandlingPath')] |
| | | 79 | | [string] $ExceptionHandlingPath, |
| | | 80 | | [Parameter(ParameterSetName = 'LanguageOptions')] |
| | | 81 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 82 | | [Parameter(ParameterSetName = 'Code')] |
| | | 83 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 84 | | [Parameter(ParameterSetName = 'ExceptionHandlingPath')] |
| | | 85 | | [switch] $CreateScopeForErrors, |
| | | 86 | | [Parameter(ParameterSetName = 'LanguageOptions')] |
| | | 87 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 88 | | [Parameter(ParameterSetName = 'Code')] |
| | | 89 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 90 | | [Parameter(ParameterSetName = 'ExceptionHandlingPath')] |
| | | 91 | | [switch] $AllowStatusCode404Response, |
| | | 92 | | |
| | | 93 | | [Parameter(ParameterSetName = 'Json')] |
| | | 94 | | [switch] $IncludeDetailsInDevelopment, |
| | | 95 | | [Parameter(ParameterSetName = 'Json')] |
| | | 96 | | [switch] $UseProblemDetails, |
| | | 97 | | [Parameter(ParameterSetName = 'Json')] |
| | | 98 | | [switch] $Compress, |
| | | 99 | | |
| | | 100 | | [Parameter(Mandatory = $true, ParameterSetName = 'DeveloperExceptionPage')] |
| | | 101 | | [switch] $DeveloperExceptionPage, |
| | | 102 | | [Parameter(ParameterSetName = 'DeveloperExceptionPage')] |
| | | 103 | | [int] $SourceCodeLineCount, |
| | | 104 | | [Parameter(ParameterSetName = 'DeveloperExceptionPage')] |
| | | 105 | | [string] $SourceCodePath, |
| | | 106 | | |
| | | 107 | | # Custom via LanguageOptions or ScriptBlock |
| | | 108 | | [Parameter(ParameterSetName = 'LanguageOptions')] |
| | | 109 | | [Kestrun.Hosting.Options.LanguageOptions] $LanguageOptions, |
| | | 110 | | |
| | | 111 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 112 | | [scriptblock] $ScriptBlock, |
| | | 113 | | |
| | | 114 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 115 | | [Alias('CodeBlock')] |
| | | 116 | | [string]$Code, |
| | | 117 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 118 | | [Kestrun.Scripting.ScriptLanguage]$Language, |
| | | 119 | | |
| | | 120 | | [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')] |
| | | 121 | | [string]$CodeFilePath, |
| | | 122 | | |
| | | 123 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 124 | | [Parameter(ParameterSetName = 'Code')] |
| | | 125 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 126 | | [System.Reflection.Assembly[]]$ExtraRefs = $null, |
| | | 127 | | |
| | | 128 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 129 | | [Parameter(ParameterSetName = 'Code')] |
| | | 130 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 131 | | [hashtable]$Arguments, |
| | | 132 | | |
| | | 133 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 134 | | [Parameter(ParameterSetName = 'Code')] |
| | | 135 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 136 | | [string[]]$ExtraImports = $null, |
| | | 137 | | |
| | | 138 | | [switch] $PassThru |
| | | 139 | | ) |
| | | 140 | | begin { |
| | 0 | 141 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 142 | | } |
| | | 143 | | process { |
| | 0 | 144 | | $exceptionOptions = [Kestrun.Hosting.Options.ExceptionOptions]::new($Server) |
| | | 145 | | |
| | | 146 | | # Map direct ExceptionOptions properties from parameters |
| | 0 | 147 | | if ($PSBoundParameters.ContainsKey('CreateScopeForErrors')) { |
| | 0 | 148 | | $exceptionOptions.CreateScopeForErrors = $CreateScopeForErrors.IsPresent |
| | | 149 | | } |
| | 0 | 150 | | if ($PSBoundParameters.ContainsKey('AllowStatusCode404Response')) { |
| | 0 | 151 | | $exceptionOptions.AllowStatusCode404Response = $AllowStatusCode404Response.IsPresent |
| | | 152 | | } |
| | 0 | 153 | | if ($PSCmdlet.ParameterSetName -eq 'ExceptionHandlingPath') { |
| | 0 | 154 | | $exceptionOptions.ExceptionHandlingPath = [Microsoft.AspNetCore.Http.PathString]::new($ExceptionHandlingPath |
| | 0 | 155 | | } elseif ($PSCmdlet.ParameterSetName -eq 'Json') { |
| | | 156 | | # If using JSON fallback, set the options accordingly |
| | | 157 | | # If using the JSON fallback, set up the built-in JSON handler |
| | 0 | 158 | | $exceptionOptions.UseJsonExceptionHandler($UseProblemDetails.IsPresent, $IncludeDetailsInDevelopment.IsPrese |
| | 0 | 159 | | } elseif ($PSCmdlet.ParameterSetName -eq 'LanguageOptions') { |
| | | 160 | | # If using LanguageOptions, assign it directly |
| | 0 | 161 | | $exceptionOptions.LanguageOptions = $LanguageOptions |
| | 0 | 162 | | } elseif ($PSCmdlet.ParameterSetName -eq 'DeveloperExceptionPage') { |
| | | 163 | | # Warn if not in Development environment |
| | 0 | 164 | | if (-not (Test-KrDebugContext)) { |
| | 0 | 165 | | if (Test-KrLogger) { |
| | 0 | 166 | | Write-KrLog -Level Warning -Message 'DeveloperExceptionPage is typically used in Development environ |
| | | 167 | | } else { |
| | 0 | 168 | | Write-Warning -Message 'DeveloperExceptionPage is typically used in Development environment. Current |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | # If using Developer Exception Page, set up the options accordingly |
| | 0 | 172 | | $exceptionOptions.DeveloperExceptionPageOptions = [Microsoft.AspNetCore.Builder.DeveloperExceptionPageOption |
| | | 173 | | # Configure Developer Exception Page options if specified |
| | 0 | 174 | | if ($PSBoundParameters.ContainsKey('SourceCodeLineCount')) { |
| | 0 | 175 | | $exceptionOptions.DeveloperExceptionPageOptions.SourceCodeLineCount = $SourceCodeLineCount |
| | | 176 | | } |
| | 0 | 177 | | if ($PSBoundParameters.ContainsKey('SourceCodePath')) { |
| | 0 | 178 | | $exceptionOptions.DeveloperExceptionPageOptions.FileProvider = [Microsoft.Extensions.FileProviders.Physi |
| | | 179 | | } |
| | 0 | 180 | | } elseif ($PSCmdlet.ParameterSetName -ne 'Default') { |
| | 0 | 181 | | $lo = [Kestrun.Hosting.Options.LanguageOptions]::new() |
| | 0 | 182 | | $lo.ExtraImports = $ExtraImports |
| | 0 | 183 | | $lo.ExtraRefs = $ExtraRefs |
| | 0 | 184 | | if ($null -ne $Arguments) { |
| | 0 | 185 | | $dict = [System.Collections.Generic.Dictionary[string, object]]::new() |
| | 0 | 186 | | foreach ($key in $Arguments.Keys) { |
| | 0 | 187 | | $dict[$key] = $Arguments[$key] |
| | | 188 | | } |
| | 0 | 189 | | $lo.Arguments = $dict |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 193 | | 'ScriptBlock' { |
| | 0 | 194 | | $lo.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | 0 | 195 | | $lo.Code = $ScriptBlock.ToString() |
| | | 196 | | } |
| | | 197 | | 'Code' { |
| | 0 | 198 | | $lo.Language = $Language |
| | 0 | 199 | | $lo.Code = $Code |
| | | 200 | | } |
| | | 201 | | 'CodeFilePath' { |
| | 0 | 202 | | if (-not (Test-Path -Path $CodeFilePath)) { |
| | 0 | 203 | | throw "The specified code file path does not exist: $CodeFilePath" |
| | | 204 | | } |
| | 0 | 205 | | $extension = Split-Path -Path $CodeFilePath -Extension |
| | 0 | 206 | | switch ($extension) { |
| | | 207 | | '.ps1' { |
| | 0 | 208 | | $lo.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 209 | | } |
| | | 210 | | '.cs' { |
| | 0 | 211 | | $lo.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp |
| | | 212 | | } |
| | | 213 | | '.vb' { |
| | 0 | 214 | | $lo.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic |
| | | 215 | | } |
| | | 216 | | default { |
| | 0 | 217 | | throw "Unsupported '$extension' code file extension." |
| | | 218 | | } |
| | | 219 | | } |
| | 0 | 220 | | $lo.Code = Get-Content -Path $CodeFilePath -Raw |
| | | 221 | | } |
| | | 222 | | default { |
| | 0 | 223 | | throw "Unrecognized ParameterSetName: $($PSCmdlet.ParameterSetName)" |
| | | 224 | | } |
| | | 225 | | } |
| | 0 | 226 | | $exceptionOptions.LanguageOptions = $lo |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | |
| | | 230 | | # Assign the configured options to the server instance |
| | 0 | 231 | | $Server.ExceptionOptions = $exceptionOptions |
| | | 232 | | |
| | 0 | 233 | | if ($PassThru.IsPresent) { |
| | | 234 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 235 | | return $Server |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | } |