Skip to content

Commit

Permalink
Add error handling for missing contexts in Get-, Remove-, and Set-Con…
Browse files Browse the repository at this point in the history
…textSetting functions
  • Loading branch information
MariusStorhaug committed Nov 21, 2024
1 parent f68c9cc commit 35f5b55
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/functions/public/ContextSetting/Get-ContextSetting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function Get-ContextSetting {

$null = Get-ContextVault
$contextObj = Get-Context -ID $ID

if (-not $contexts) {
throw "Context [$ID] not found"
}

Write-Verbose "Returning setting: [$Name]"
$contextObj.$Name
}
4 changes: 4 additions & 0 deletions src/functions/public/ContextSetting/Remove-ContextSetting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ filter Remove-ContextSetting {
$null = Get-ContextVault
$contextObj = Get-Context -ID $ID

if (-not $contexts) {
throw "Context [$ID] not found"
}

if ($PSCmdlet.ShouldProcess("[$($contextObj.Name)]", "Remove [$Name]")) {
Write-Verbose "Setting [$Name] in [$($contextObj.Name)]"
$contextObj.PSObject.Properties.Remove($Name)
Expand Down
4 changes: 4 additions & 0 deletions src/functions/public/ContextSetting/Set-ContextSetting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ function Set-ContextSetting {
$null = Get-ContextVault
$contextObj = Get-Context -ID $ID

if (-not $contexts) {
throw "Context [$ID] not found"
}

if ($PSCmdlet.ShouldProcess($Name, "Set value [$Value]")) {
Write-Verbose "Setting [$Name] to [$Value] in [$($contextObj.Name)]"
if ($contextObj.PSObject.Properties[$Name]) {
Expand Down

0 comments on commit 35f5b55

Please sign in to comment.