Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xADRecycleBin: Replace Write-Error with the correct helper function #393

Merged
merged 7 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@
- Remove unneeded example and resource designer files.
- Added missing property schema descriptions ([issue #368](https://github.com/PowerShell/xActiveDirectory/issues/368)).
- Code cleanup.
- It now set back the `$ErrorActionPreference` that was set prior to
- It now sets back the `$ErrorActionPreference` that was set prior to
setting it to `'Stop'`.
- Replace Write-Error with the correct helper function ([issue #327](https://github.com/PowerShell/xActiveDirectory/issues/327)).
- Changes to xADReplicationSiteLink
- Fix ADIdentityNotFoundException when creating a new site link.
- Code cleanup.
Expand Down
36 changes: 18 additions & 18 deletions DSCResources/MSFT_xADRecycleBin/MSFT_xADRecycleBin.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ function Get-TargetResource
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException], [Microsoft.ActiveDirectory.Management.ADServerDownException]
{
Write-Error -Message ($script:localizedData.ForestNotFound -f $ForestFQDN)
throw $_
$errorMessage = $script:localizedData.ForestNotFound -f $ForestFQDN
New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_
}
catch [System.Security.Authentication.AuthenticationException]
{
Write-Error -Message $script:localizedData.CredentialError
throw $_
$errorMessage = $script:localizedData.CredentialError
New-InvalidArgumentException -Message $errorMessage -ArgumentName 'EnterpriseAdministratorCredential'
}
catch
{
Write-Error -Message ($script:localizedData.GetUnhandledException -f $ForestFQDN)
throw $_
$errorMessage = $script:localizedData.GetUnhandledException -f $ForestFQDN
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
finally
{
Expand Down Expand Up @@ -112,18 +112,18 @@ function Set-TargetResource
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException], [Microsoft.ActiveDirectory.Management.ADServerDownException]
{
Write-Error -Message ($script:localizedData.ForestNotFound -f $ForestFQDN)
throw $_
$errorMessage = $script:localizedData.ForestNotFound -f $ForestFQDN
New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_
}
catch [System.Security.Authentication.AuthenticationException]
{
Write-Error -Message $script:localizedData.CredentialError
throw $_
$errorMessage = $script:localizedData.CredentialError
New-InvalidArgumentException -Message $errorMessage -ArgumentName 'EnterpriseAdministratorCredential'
}
catch
{
Write-Error -Message ($script:localizedData.SetUnhandledException -f $ForestFQDN)
throw $_
$errorMessage = $script:localizedData.SetUnhandledException -f $ForestFQDN
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
finally
{
Expand Down Expand Up @@ -171,18 +171,18 @@ function Test-TargetResource
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException], [Microsoft.ActiveDirectory.Management.ADServerDownException]
{
Write-Error -Message ($script:localizedData.ForestNotFound -f $ForestFQDN)
throw $_
$errorMessage = $script:localizedData.ForestNotFound -f $ForestFQDN
New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_
}
catch [System.Security.Authentication.AuthenticationException]
{
Write-Error -Message $script:localizedData.CredentialError
throw $_
$errorMessage = $script:localizedData.CredentialError
New-InvalidArgumentException -Message $errorMessage -ArgumentName 'EnterpriseAdministratorCredential'
}
catch
{
Write-Error -Message ($script:localizedData.TestUnhandledException -f $ForestFQDN)
throw $_
$errorMessage = $script:localizedData.TestUnhandledException -f $ForestFQDN
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
finally
{
Expand Down
84 changes: 60 additions & 24 deletions Tests/Unit/MSFT_xADRecycleBin.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,35 @@ try
Mock -CommandName Write-Error

It 'Should throw ADIdentityNotFoundException' {
Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) }
{ Get-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException
Mock -CommandName Get-ADObject -MockWith {
throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException)
}
$expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN
{ Get-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw ADServerDownException' {
Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) }
{ Get-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADServerDownException
Mock -CommandName Get-ADObject -MockWith {
throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException)
}
$expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN
{ Get-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw AuthenticationException' {
Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) }
{ Get-TargetResource @targetResourceParameters } | Should -Throw 'System error'
Mock -CommandName Get-ADObject -MockWith {
throw (New-Object -TypeName System.Security.Authentication.AuthenticationException)
}
$expectedError = $script:localizedData.CredentialError
{ Get-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw UnhandledException' {
Mock -CommandName Get-ADObject -MockWith { throw Unhandled.Exception }
{ Get-TargetResource @targetResourceParameters } | Should -Throw Unhandled.Exception
Mock -CommandName Get-ADObject -MockWith {
throw Unhandled.Exception
}
$expectedError = $script:localizedData.GetUnhandledException -f $ForestFQDN
{ Get-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}
}
}
Expand Down Expand Up @@ -158,23 +170,35 @@ try
Mock -CommandName Write-Error

It 'Should throw ADIdentityNotFoundException' {
Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) }
{ Test-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException
Mock -CommandName Get-ADObject -MockWith {
throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException)
}
$expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN
{ Test-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw ADServerDownException' {
Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) }
{ Test-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADServerDownException
Mock -CommandName Get-ADObject -MockWith {
throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException)
}
$expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN
{ Test-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw AuthenticationException' {
Mock -CommandName Get-ADObject -MockWith { throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) }
{ Test-TargetResource @targetResourceParameters } | Should -Throw 'System error'
Mock -CommandName Get-ADObject -MockWith {
throw (New-Object -TypeName System.Security.Authentication.AuthenticationException)
}
$expectedError = $script:localizedData.CredentialError
{ Test-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw UnhandledException' {
Mock -CommandName Get-ADObject -MockWith { throw Unhandled.Exception }
{ Test-TargetResource @targetResourceParameters } | Should -Throw Unhandled.Exception
Mock -CommandName Get-ADObject -MockWith {
throw Unhandled.Exception
}
$expectedError = $script:localizedData.TestUnhandledException -f $ForestFQDN
{ Test-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}
}
}
Expand Down Expand Up @@ -210,23 +234,35 @@ try
Mock -CommandName Write-Error

It 'Should throw ADIdentityNotFoundException' {
Mock -CommandName Get-ADForest -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) }
{ Set-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException
Mock -CommandName Get-ADForest -MockWith {
throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException)
}
$expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN
{ Set-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw ADServerDownException' {
Mock -CommandName Get-ADForest -MockWith { throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException) }
{ Set-TargetResource @targetResourceParameters } | Should -Throw Microsoft.ActiveDirectory.Management.ADServerDownException
Mock -CommandName Get-ADForest -MockWith {
throw (New-Object -TypeName Microsoft.ActiveDirectory.Management.ADServerDownException)
}
$expectedError = $script:localizedData.ForestNotFound -f $ForestFQDN
{ Set-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw AuthenticationException' {
Mock -CommandName Get-ADForest -MockWith { throw (New-Object -TypeName System.Security.Authentication.AuthenticationException) }
{ Set-TargetResource @targetResourceParameters } | Should -Throw 'System error'
Mock -CommandName Get-ADForest -MockWith {
throw (New-Object -TypeName System.Security.Authentication.AuthenticationException)
}
$expectedError = $script:localizedData.CredentialError
{ Set-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}

It 'Should throw UnhandledException' {
Mock -CommandName Get-ADForest -MockWith { throw Unhandled.Exception }
{ Set-TargetResource @targetResourceParameters } | Should -Throw Unhandled.Exception
Mock -CommandName Get-ADForest -MockWith {
throw Unhandled.Exception
}
$expectedError = $script:localizedData.SetUnhandledException -f $ForestFQDN
{ Set-TargetResource @targetResourceParameters } | Should -Throw $expectedError
}
}
}
Expand Down