Skip to content

Commit

Permalink
Error reporting changes
Browse files Browse the repository at this point in the history
The code is less lazy about reporting errors now.  It builds ErrorRecord objects with module-specific FullyQualifiedErrorId values, and the top-level (exported) functions use $PSCmdlet.ThrowTerminatingError() to return those values to the caller.  This results in errors that look more like what you get from a cmdlet, and should be more useful to users of the module.

Since this could technically be a breaking change for scripts that are looking for the old errors, I'll be bumping the version number up to v2.0.

I don't have a lot of tests around the error conditions at the moment; should add some later to make sure that predictable errors produce the output that they should.
  • Loading branch information
dlwyatt committed Apr 5, 2015
1 parent 8780b28 commit 1b5bafd
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 22 deletions.
79 changes: 64 additions & 15 deletions Commands.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ function Set-PolicyFileEntry
}

$dirty = $false
$policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop

try
{
$policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
}

process
Expand Down Expand Up @@ -139,7 +147,8 @@ function Set-PolicyFileEntry
$bytes = $Data -as [byte[]]
if ($null -eq $bytes)
{
throw 'When -Type is set to Binary, -Data must be passed a Byte[] array.'
$errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to Binary, -Data must be passed a Byte[] array.'
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
else
{
Expand All @@ -155,7 +164,8 @@ function Set-PolicyFileEntry

if ($array.Count -ne 1)
{
throw 'When -Type is set to String, -Data must be passed a scalar value or single-element array.'
$errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to String, -Data must be passed a scalar value or single-element array.'
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
else
{
Expand All @@ -171,7 +181,8 @@ function Set-PolicyFileEntry

if ($array.Count -ne 1)
{
throw 'When -Type is set to ExpandString, -Data must be passed a scalar value or single-element array.'
$errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to ExpandString, -Data must be passed a scalar value or single-element array.'
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
else
{
Expand All @@ -187,7 +198,8 @@ function Set-PolicyFileEntry
$dword = ($array | Select-Object -First 1) -as [UInt32]
if ($null -eq $dword -or $array.Count -ne 1)
{
throw 'When -Type is set to DWord, -Data must be passed a valid UInt32 value.'
$errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to DWord, -Data must be passed a valid UInt32 value.'
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
else
{
Expand All @@ -203,7 +215,8 @@ function Set-PolicyFileEntry
$qword = ($array | Select-Object -First 1) -as [UInt64]
if ($null -eq $qword -or $array.Count -ne 1)
{
throw 'When -Type is set to QWord, -Data must be passed a valid UInt64 value.'
$errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to QWord, -Data must be passed a valid UInt64 value.'
$PSCmdlet.ThrowTerminatingError($errorRecord)
}
else
{
Expand Down Expand Up @@ -243,9 +256,16 @@ function Set-PolicyFileEntry
{
$doUpdateGptIni = -not $NoGptIniUpdate

# SavePolicyFile contains the calls to $PSCmdlet.ShouldProcess, and will inherit our
# WhatIfPreference / ConfirmPreference values from here.
SavePolicyFile -PolicyFile $policyFile -UpdateGptIni:$doUpdateGptIni -ErrorAction Stop
try
{
# SavePolicyFile contains the calls to $PSCmdlet.ShouldProcess, and will inherit our
# WhatIfPreference / ConfirmPreference values from here.
SavePolicyFile -PolicyFile $policyFile -UpdateGptIni:$doUpdateGptIni -ErrorAction Stop
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
}
}
}
Expand Down Expand Up @@ -313,7 +333,14 @@ function Get-PolicyFileEntry
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
}

$policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop
try
{
$policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}

if ($PSCmdlet.ParameterSetName -eq 'ByKeyAndValue')
{
Expand Down Expand Up @@ -406,7 +433,15 @@ function Remove-PolicyFileEntry
}

$dirty = $false
$policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop

try
{
$policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
}

process
Expand All @@ -430,9 +465,16 @@ function Remove-PolicyFileEntry
{
$doUpdateGptIni = -not $NoGptIniUpdate

# SavePolicyFile contains the calls to $PSCmdlet.ShouldProcess, and will inherit our
# WhatIfPreference / ConfirmPreference values from here.
SavePolicyFile -PolicyFile $policyFile -UpdateGptIni:$doUpdateGptIni -ErrorAction Stop
try
{
# SavePolicyFile contains the calls to $PSCmdlet.ShouldProcess, and will inherit our
# WhatIfPreference / ConfirmPreference values from here.
SavePolicyFile -PolicyFile $policyFile -UpdateGptIni:$doUpdateGptIni -ErrorAction Stop
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
}
}
}
Expand Down Expand Up @@ -502,5 +544,12 @@ function Update-GptIniVersion
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
}

IncrementGptIniVersion @PSBoundParameters
try
{
IncrementGptIniVersion @PSBoundParameters
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
}
44 changes: 39 additions & 5 deletions Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,28 @@ function OpenPolicyFile
{
$policyFile.LoadFile()
}
catch [TJX.PolFileEditor.FileFormatException]
{
$message = "File '$Path' is not a valid POL file."
$exception = New-Object System.Exception($message)

$errorRecord = New-Object System.Management.Automation.ErrorRecord(
$exception, 'InvalidPolFileContents', [System.Management.Automation.ErrorCategory]::InvalidData, $Path
)

throw $errorRecord
}
catch
{
$errorRecord = $_
$message = "Error loading policy file at path '$Path': $($errorRecord.Exception.Message)"
$exception = New-Object System.Exception($message, $errorRecord.Exception)
throw $exception

$newErrorRecord = New-Object System.Management.Automation.ErrorRecord(
$exception, 'FailedToOpenPolicyFile', [System.Management.Automation.ErrorCategory]::OperationStopped, $Path
)

throw $newErrorRecord
}
}

Expand Down Expand Up @@ -112,7 +128,12 @@ function SavePolicyFile
$errorRecord = $_
$message = "Error creating parent folder of path '$Path': $($errorRecord.Exception.Message)"
$exception = New-Object System.Exception($message, $errorRecord.Exception)
throw $exception

$newErrorRecord = New-Object System.Management.Automation.ErrorRecord(
$exception, 'CreateParentFolderError', $errorRecord.CategoryInfo.Category, $Path
)

throw $newErrorRecord
}
}

Expand All @@ -125,7 +146,12 @@ function SavePolicyFile
$errorRecord = $_
$message = "Error saving policy file to path '$($PolicyFile.FileName)': $($errorRecord.Exception.Message)"
$exception = New-Object System.Exception($message, $errorRecord.Exception)
throw $exception

$newErrorRecord = New-Object System.Management.Automation.ErrorRecord(
$exception, 'FailedToSavePolicyFile', [System.Management.Automation.ErrorCategory]::OperationStopped, $PolicyFile
)

throw $newErrorRecord
}
}

Expand Down Expand Up @@ -479,7 +505,7 @@ function GetSidForAccount($Account)
$Acc
)

$PSCmdlet.ThrowTerminatingError($errorRecord)
throw $errorRecord
}
}

Expand Down Expand Up @@ -654,7 +680,15 @@ function Assert-ValidDataAndType
$Type -ne [Microsoft.Win32.RegistryValueKind]::Binary -and
$Data.Count -gt 1)
{
throw 'Do not pass arrays with multiple values to the -Data parameter when -Type is not set to either Binary or MultiString.'
$errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'Do not pass arrays with multiple values to the -Data parameter when -Type is not set to either Binary or MultiString.'
throw $errorRecord
}
}

function InvalidDataTypeCombinationErrorRecord($Message)
{
$exception = New-Object System.Exception($Message)
return New-Object System.Management.Automation.ErrorRecord(
$exception, 'InvalidDataTypeCombination', [System.Management.Automation.ErrorCategory]::InvalidArgument, $null
)
}
4 changes: 2 additions & 2 deletions PolicyFileEditor.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@{
ModuleToProcess = 'PolicyFileEditor.psm1'
ModuleVersion = '1.4.2'
ModuleVersion = '2.0'
GUID = '110a2398-3053-4ffc-89d1-1b6a38a2dc86'
Author = 'Dave Wyatt'
CompanyName = 'Home'
Expand Down Expand Up @@ -36,7 +36,7 @@
LicenseUri = 'https://www.apache.org/licenses/LICENSE-2.0.html'
ProjectUri = 'https://github.com/dlwyatt/PolicyFileEditor'
# IconUri = ''
ReleaseNotes = 'Build script update'
ReleaseNotes = 'Error reporting changes'
}
}
}
Expand Down

0 comments on commit 1b5bafd

Please sign in to comment.