Skip to content

Commit 110c600

Browse files
committed
Refactor error handling in Get-SqlDscRSConfiguration to use New-ErrorRecord for improved clarity
1 parent b4633c7 commit 110c600

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

source/Public/Get-SqlDscRSConfiguration.ps1

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,9 @@ function Get-SqlDscRSConfiguration
108108
{
109109
$errorMessage = $script:localizedData.Get_SqlDscRSConfiguration_FailedToGetConfiguration -f $InstanceName, $_.Exception.Message
110110

111-
$PSCmdlet.ThrowTerminatingError(
112-
[System.Management.Automation.ErrorRecord]::new(
113-
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
114-
'GSRSCD0003',
115-
[System.Management.Automation.ErrorCategory]::InvalidOperation,
116-
$InstanceName
117-
)
118-
)
111+
$errorRecord = New-ErrorRecord -Exception (New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ -PassThru) -ErrorId 'GSRSCD0003' -ErrorCategory 'InvalidOperation' -TargetObject $InstanceName
112+
113+
$PSCmdlet.ThrowTerminatingError($errorRecord)
119114
}
120115

121116
# Filter to ensure we get the correct instance if multiple are returned.
@@ -128,14 +123,9 @@ function Get-SqlDscRSConfiguration
128123
{
129124
$errorMessage = $script:localizedData.Get_SqlDscRSConfiguration_ConfigurationNotFound -f $InstanceName
130125

131-
$PSCmdlet.ThrowTerminatingError(
132-
[System.Management.Automation.ErrorRecord]::new(
133-
[System.InvalidOperationException]::new($errorMessage),
134-
'GSRSCD0004',
135-
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
136-
$InstanceName
137-
)
138-
)
126+
$errorRecord = New-ErrorRecord -Exception (New-InvalidOperationException -Message $errorMessage -PassThru) -ErrorId 'GSRSCD0004' -ErrorCategory 'ObjectNotFound' -TargetObject $InstanceName
127+
128+
$PSCmdlet.ThrowTerminatingError($errorRecord)
139129
}
140130

141131
return $reportingServicesConfiguration

0 commit comments

Comments
 (0)