Skip to content

Commit

Permalink
🩹 [Patch]: Improve error handling in context vault functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Nov 24, 2024
1 parent f6abb86 commit 14e4415
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/functions/private/Get-ContextVault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Get-ContextVault {

return $secretVault
} catch {
throw $_
Write-Error $_
throw 'Failed to get context vault'
}
}
3 changes: 2 additions & 1 deletion src/functions/private/Initialize-ContextVault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function Initialize-ContextVault {

Get-SecretVault | Where-Object { $_.ModuleName -eq $Type }
} catch {
throw $_
Write-Error $_
throw 'Failed to initialize context vault'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
foreach ($key in $Hashtable.Keys) {
$value = $Hashtable[$key]
Write-Debug "Processing [$key]"
Write-Debug "Value type: $($value.GetType().FullName)"
Write-Debug "Value: $value"
Write-Debug "Type: $($value.GetType().Name)"
if ($value -is [string] -and $value -like '`[SECURESTRING`]*') {
Write-Debug "Converting [$key] as [SecureString]"
$secureValue = $value -replace '^\[SECURESTRING\]', ''
Expand All @@ -64,6 +64,7 @@
}
return $result
} catch {
throw $_
Write-Error $_
throw 'Failed to convert hashtable to object'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$hashtableObject = $JsonString | ConvertFrom-Json -Depth 100 -AsHashtable
return Convert-ContextHashtableToObjectRecursive $hashtableObject
} catch {
throw $_
Write-Error $_
throw 'Failed to convert JSON to object'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
}

foreach ($property in $Object.PSObject.Properties) {
$name = $property.Name
$value = $property.Value
Write-Debug "Processing [$($property.Name)]"
Write-Debug "Value type: $($value.GetType().FullName)"
Write-Debug "Processing [$name]"
Write-Debug "Value: $value"
Write-Debug "Type: $($value.GetType().Name)"
if ($value -is [datetime]) {
Write-Debug '- as DateTime'
$result[$property.Name] = $value.ToString('o')
Expand Down Expand Up @@ -70,6 +72,7 @@
}
return $result
} catch {
throw $_
Write-Error $_
throw 'Failed to convert context object to hashtable'
}
}
3 changes: 2 additions & 1 deletion src/functions/private/ObjectToJSON/ConvertTo-ContextJson.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
$processedObject = Convert-ContextObjectToHashtableRecursive $Context
return ($processedObject | ConvertTo-Json -Depth 100 -Compress)
} catch {
throw $_
Write-Error $_
throw 'Failed to convert object to JSON'
}
}
3 changes: 2 additions & 1 deletion src/functions/public/Context/Get-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ filter Get-Context {
ConvertFrom-ContextJson -JsonString $contextJson
}
} catch {
throw $_
Write-Error $_
throw 'Failed to get context'
}
}
3 changes: 2 additions & 1 deletion src/functions/public/Context/Remove-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ filter Remove-Context {
Get-SecretInfo -Vault $script:Config.VaultName | Where-Object { $_.Name -eq $ID } | Remove-Secret
}
} catch {
throw $_
Write-Error $_
throw 'Failed to remove context'
}
}
23 changes: 16 additions & 7 deletions src/functions/public/Context/Set-Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,28 @@ function Set-Context {
[object] $Context
)

$contextVault = Get-ContextVault

try {
$contextVault = Get-ContextVault
$secret = ConvertTo-ContextJson -Context $Context
} catch {
Write-Error $_
throw 'Failed to convert context to JSON'
}

$param = @{
Name = "$($script:Config.SecretPrefix)$ID"
Secret = ConvertTo-ContextJson -Context $Context
Vault = $contextVault.Name
}
$param = @{
Name = "$($script:Config.SecretPrefix)$ID"
Secret = $secret
Vault = $contextVault.Name
}
Write-Verbose ($param | ConvertTo-Json -Depth 5)

try {
if ($PSCmdlet.ShouldProcess('Set-Secret', $param)) {
Set-Secret @param
}
} catch {
throw $_
Write-Error $_
throw 'Failed to set secret'
}
}
3 changes: 2 additions & 1 deletion src/functions/public/ContextSetting/Get-ContextSetting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function Get-ContextSetting {
Write-Verbose "Returning setting: [$Name]"
$context.$Name
} catch {
throw $_
Write-Error $_
throw 'Failed to get context setting'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ filter Remove-ContextSetting {
Set-Context -Context $context -ID $ID
}
} catch {
throw $_
Write-Error $_
throw 'Failed to remove context setting'
}
}
3 changes: 2 additions & 1 deletion src/functions/public/ContextSetting/Set-ContextSetting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function Set-ContextSetting {
Set-Context -Context $context -ID $ID
}
} catch {
throw $_
Write-Error $_
throw 'Failed to set context setting'
}
}

0 comments on commit 14e4415

Please sign in to comment.