Skip to content

Commit 5454f55

Browse files
authored
Fix style for terminating error in commands (#2312)
1 parent 0f3d639 commit 5454f55

17 files changed

+330
-107
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
195195

196196
### Changed
197197

198+
- `Remove-SqlDscAgentAlert`
199+
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
200+
functions for proper terminating error handling ([issue #2193](https://github.com/dsccommunity/SqlServerDsc/issues/2193)).
201+
- `Set-SqlDscAgentAlert`
202+
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
203+
functions for proper terminating error handling ([issue #2196](https://github.com/dsccommunity/SqlServerDsc/issues/2196)).
204+
- `Remove-SqlDscDatabase`
205+
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
206+
functions for proper terminating error handling ([issue #2195](https://github.com/dsccommunity/SqlServerDsc/issues/2195)).
207+
- `Remove-SqlDscRole`
208+
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
209+
functions for proper terminating error handling ([issue #2199](https://github.com/dsccommunity/SqlServerDsc/issues/2199)).
210+
- `New-SqlDscDatabase`
211+
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
212+
functions for proper terminating error handling ([issue #2200](https://github.com/dsccommunity/SqlServerDsc/issues/2200)).
213+
- `Set-SqlDscDatabase`
214+
- Now uses `$PSCmdlet.ThrowTerminatingError()` instead of exception helper
215+
functions for proper terminating error handling ([issue #2198](https://github.com/dsccommunity/SqlServerDsc/issues/2198)).
198216
- `Add-SqlDscTraceFlag`
199217
- Improved de-duplication logic to normalize element types to `[System.UInt32]`
200218
before sorting and removing duplicates, ensuring proper handling of mixed

source/Public/Get-SqlDscRole.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ function Get-SqlDscRole
7373

7474
if (-not $roleObject)
7575
{
76-
Write-Verbose -Message ($script:localizedData.Role_NotFound -f $Name)
76+
Write-Verbose -Message ($script:localizedData.Get_SqlDscRole_NotFound -f $Name)
7777

78-
$missingRoleMessage = $script:localizedData.Role_NotFound -f $Name
78+
$missingRoleMessage = $script:localizedData.Get_SqlDscRole_NotFound -f $Name
7979

8080
$writeErrorParameters = @{
8181
Message = $missingRoleMessage
@@ -85,6 +85,8 @@ function Get-SqlDscRole
8585
}
8686

8787
Write-Error @writeErrorParameters
88+
89+
return
8890
}
8991
else
9092
{

source/Public/New-SqlDscDatabase.ps1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,15 @@ function New-SqlDscDatabase
147147
if ($CompatibilityLevel -notin $supportedCompatibilityLevels.$($ServerObject.VersionMajor))
148148
{
149149
$errorMessage = $script:localizedData.Database_InvalidCompatibilityLevel -f $CompatibilityLevel, $ServerObject.InstanceName
150-
New-ArgumentException -ArgumentName 'CompatibilityLevel' -Message $errorMessage
150+
151+
$PSCmdlet.ThrowTerminatingError(
152+
[System.Management.Automation.ErrorRecord]::new(
153+
[System.ArgumentException]::new($errorMessage),
154+
'NSD0003', # cspell: disable-line
155+
[System.Management.Automation.ErrorCategory]::InvalidArgument,
156+
$CompatibilityLevel
157+
)
158+
)
151159
}
152160
}
153161

@@ -157,7 +165,15 @@ function New-SqlDscDatabase
157165
if ($Collation -notin $ServerObject.EnumCollations().Name)
158166
{
159167
$errorMessage = $script:localizedData.Database_InvalidCollation -f $Collation, $ServerObject.InstanceName
160-
New-ArgumentException -ArgumentName 'Collation' -Message $errorMessage
168+
169+
$PSCmdlet.ThrowTerminatingError(
170+
[System.Management.Automation.ErrorRecord]::new(
171+
[System.ArgumentException]::new($errorMessage),
172+
'NSD0004', # cspell: disable-line
173+
[System.Management.Automation.ErrorCategory]::InvalidArgument,
174+
$Collation
175+
)
176+
)
161177
}
162178
}
163179

source/Public/New-SqlDscRole.ps1

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ function New-SqlDscRole
7070
$Refresh
7171
)
7272

73+
begin
74+
{
75+
if ($Force.IsPresent -and -not $Confirm)
76+
{
77+
$ConfirmPreference = 'None'
78+
}
79+
}
80+
7381
process
7482
{
7583
if ($Refresh.IsPresent)
@@ -88,18 +96,18 @@ function New-SqlDscRole
8896
$PSCmdlet.ThrowTerminatingError(
8997
[System.Management.Automation.ErrorRecord]::new(
9098
[System.InvalidOperationException]::new($errorMessage),
91-
'NSR0001', # cspell: disable-line
99+
'NSDR0001', # cspell: disable-line
92100
[System.Management.Automation.ErrorCategory]::ResourceExists,
93101
$Name
94102
)
95103
)
96104
}
97105

98-
$verboseDescriptionMessage = $script:localizedData.Role_Create_ShouldProcessVerboseDescription -f $Name, $ServerObject.InstanceName
99-
$verboseWarningMessage = $script:localizedData.Role_Create_ShouldProcessVerboseWarning -f $Name
106+
$descriptionMessage = $script:localizedData.Role_Create_ShouldProcessDescription -f $Name, $ServerObject.InstanceName
107+
$confirmationMessage = $script:localizedData.Role_Create_ShouldProcessConfirmation -f $Name
100108
$captionMessage = $script:localizedData.Role_Create_ShouldProcessCaption
101109

102-
if ($Force.IsPresent -or $PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
110+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
103111
{
104112
try
105113
{
@@ -110,12 +118,8 @@ function New-SqlDscRole
110118
$serverRole.Owner = $Owner
111119
}
112120

113-
Write-Verbose -Message ($script:localizedData.Role_Creating -f $Name)
114-
115121
$serverRole.Create()
116122

117-
Write-Verbose -Message ($script:localizedData.Role_Created -f $Name)
118-
119123
return $serverRole
120124
}
121125
catch
@@ -125,7 +129,7 @@ function New-SqlDscRole
125129
$PSCmdlet.ThrowTerminatingError(
126130
[System.Management.Automation.ErrorRecord]::new(
127131
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
128-
'NSR0002', # cspell: disable-line
132+
'NSDR0002', # cspell: disable-line
129133
[System.Management.Automation.ErrorCategory]::InvalidOperation,
130134
$Name
131135
)

source/Public/Remove-SqlDscAgentAlert.ps1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ function Remove-SqlDscAgentAlert
117117

118118
if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
119119
{
120+
Write-Verbose -Message ($script:localizedData.Remove_SqlDscAgentAlert_RemovingAlert -f $alertObjectToRemove.Name)
121+
120122
try
121123
{
122-
Write-Verbose -Message ($script:localizedData.Remove_SqlDscAgentAlert_RemovingAlert -f $alertObjectToRemove.Name)
124+
$originalErrorActionPreference = $ErrorActionPreference
125+
126+
$ErrorActionPreference = 'Stop'
123127

124128
$alertObjectToRemove.Drop()
125129

@@ -128,7 +132,19 @@ function Remove-SqlDscAgentAlert
128132
catch
129133
{
130134
$errorMessage = $script:localizedData.Remove_SqlDscAgentAlert_RemoveFailed -f $alertObjectToRemove.Name
131-
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
135+
136+
$PSCmdlet.ThrowTerminatingError(
137+
[System.Management.Automation.ErrorRecord]::new(
138+
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
139+
'RSAA0005', # cspell: disable-line
140+
[System.Management.Automation.ErrorCategory]::InvalidOperation,
141+
$alertObjectToRemove
142+
)
143+
)
144+
}
145+
finally
146+
{
147+
$ErrorActionPreference = $originalErrorActionPreference
132148
}
133149
}
134150
}

source/Public/Remove-SqlDscDatabase.ps1

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ function Remove-SqlDscDatabase
9494
$DropConnections
9595
)
9696

97+
begin
98+
{
99+
if ($Force.IsPresent -and -not $Confirm)
100+
{
101+
$ConfirmPreference = 'None'
102+
}
103+
}
104+
97105
process
98106
{
99107
if ($PSCmdlet.ParameterSetName -eq 'ServerObject')
@@ -104,74 +112,90 @@ function Remove-SqlDscDatabase
104112
$ServerObject.Databases.Refresh()
105113
}
106114

107-
Write-Verbose -Message ($script:localizedData.Database_Remove -f $Name, $ServerObject.InstanceName)
108-
109-
# Check if the database is a system database (cannot be dropped)
110-
$systemDatabases = @('master', 'model', 'msdb', 'tempdb')
111-
if ($Name -in $systemDatabases)
112-
{
113-
$errorMessage = $script:localizedData.Database_CannotRemoveSystem -f $Name
114-
New-InvalidOperationException -Message $errorMessage
115-
}
116-
117115
# Get the database object
118116
$DatabaseObject = $ServerObject.Databases[$Name]
119117

120118
if (-not $DatabaseObject)
121119
{
122-
$errorMessage = $script:localizedData.Database_NotFound -f $Name
123-
New-InvalidOperationException -Message $errorMessage
120+
$errorMessage = $script:localizedData.Remove_SqlDscDatabase_NotFound -f $Name
121+
122+
$PSCmdlet.ThrowTerminatingError(
123+
[System.Management.Automation.ErrorRecord]::new(
124+
[System.Management.Automation.ItemNotFoundException]::new($errorMessage),
125+
'RSDD0002', # cspell: disable-line
126+
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
127+
$Name
128+
)
129+
)
124130
}
125131
}
126132
else
127133
{
128134
$Name = $DatabaseObject.Name
129-
Write-Verbose -Message ($script:localizedData.Database_Remove -f $Name, $DatabaseObject.Parent.InstanceName)
135+
}
130136

131-
# Check if the database is a system database (cannot be dropped)
132-
$systemDatabases = @('master', 'model', 'msdb', 'tempdb')
133-
if ($Name -in $systemDatabases)
134-
{
135-
$errorMessage = $script:localizedData.Database_CannotRemoveSystem -f $Name
136-
New-InvalidOperationException -Message $errorMessage
137-
}
137+
# Check if the database is a system database (cannot be dropped)
138+
if ($Name -in @('master', 'model', 'msdb', 'tempdb'))
139+
{
140+
$errorMessage = $script:localizedData.Database_CannotRemoveSystem -f $Name
141+
142+
$PSCmdlet.ThrowTerminatingError(
143+
[System.Management.Automation.ErrorRecord]::new(
144+
[System.InvalidOperationException]::new($errorMessage),
145+
'RSDD0001', # cspell: disable-line
146+
[System.Management.Automation.ErrorCategory]::InvalidOperation,
147+
$Name
148+
)
149+
)
138150
}
139151

140-
$verboseDescriptionMessage = $script:localizedData.Database_Remove_ShouldProcessVerboseDescription -f $Name, $DatabaseObject.Parent.InstanceName
141-
$verboseWarningMessage = $script:localizedData.Database_Remove_ShouldProcessVerboseWarning -f $Name
152+
$descriptionMessage = $script:localizedData.Database_Remove_ShouldProcessDescription -f $Name, $DatabaseObject.Parent.InstanceName
153+
$confirmationMessage = $script:localizedData.Database_Remove_ShouldProcessConfirmation -f $Name
142154
$captionMessage = $script:localizedData.Database_Remove_ShouldProcessCaption
143155

144-
if ($Force.IsPresent -or $PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
156+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
145157
{
146-
try
158+
# Drop all active connections if requested
159+
if ($DropConnections.IsPresent)
147160
{
148-
# Drop all active connections if requested
149-
if ($DropConnections.IsPresent)
161+
Write-Verbose -Message ($script:localizedData.Database_DroppingConnections -f $Name)
162+
163+
try
150164
{
151-
Write-Verbose -Message ($script:localizedData.Database_DroppingConnections -f $Name)
152-
153-
try
154-
{
155-
$DatabaseObject.UserAccess = 'Single'
156-
$DatabaseObject.Alter([Microsoft.SqlServer.Management.Smo.TerminationClause]::RollbackTransactionsImmediately)
157-
}
158-
catch
159-
{
160-
$errorMessage = $script:localizedData.Database_DropConnectionsFailed -f $Name
161-
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
162-
}
165+
$DatabaseObject.UserAccess = [Microsoft.SqlServer.Management.Smo.DatabaseUserAccess]::Single
166+
$DatabaseObject.Alter([Microsoft.SqlServer.Management.Smo.TerminationClause]::RollbackTransactionsImmediately)
163167
}
168+
catch
169+
{
170+
$errorMessage = $script:localizedData.Database_DropConnectionsFailed -f $Name
171+
172+
$PSCmdlet.ThrowTerminatingError(
173+
[System.Management.Automation.ErrorRecord]::new(
174+
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
175+
'RSDD0004', # cspell: disable-line
176+
[System.Management.Automation.ErrorCategory]::InvalidOperation,
177+
$DatabaseObject
178+
)
179+
)
180+
}
181+
}
164182

165-
Write-Verbose -Message ($script:localizedData.Database_Removing -f $Name)
166-
183+
try
184+
{
167185
$DatabaseObject.Drop()
168-
169-
Write-Verbose -Message ($script:localizedData.Database_Removed -f $Name)
170186
}
171187
catch
172188
{
173189
$errorMessage = $script:localizedData.Database_RemoveFailed -f $Name, $DatabaseObject.Parent.InstanceName
174-
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
190+
191+
$PSCmdlet.ThrowTerminatingError(
192+
[System.Management.Automation.ErrorRecord]::new(
193+
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
194+
'RSDD0005', # cspell: disable-line
195+
[System.Management.Automation.ErrorCategory]::InvalidOperation,
196+
$DatabaseObject
197+
)
198+
)
175199
}
176200
}
177201
}

0 commit comments

Comments
 (0)