Common/Error/Error.psm1
using module '../Result' Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath Wrappers | Join-Path -ChildPath Wrappers) function Show-RMError { param( [Parameter(Mandatory)] [System.Management.Automation.ErrorRecord] $ErrorObj ) $ErrorString = $ErrorObj.ToString() if($ErrorString.Contains("validation_errors")) { $ErrorString = $ErrorString | ConvertFrom-Json foreach($Message in $ErrorString.validation_errors.errors) { if ($null -eq $Message.message) { Write-RMError -Message $ErrorObj.ToString() break } else { Write-RMError -Message $Message.message } } } elseif ($ErrorString.Contains("message") -And $ErrorString.Contains("resolution")) { $ErrorStringJson = $ErrorString | ConvertFrom-Json if ($null -ne $ErrorStringJson.message -and $null -ne $ErrorStringJson.resolution) { Write-RMError -Message $ErrorStringJson.message Write-RMError -Message $ErrorStringJson.resolution } elseif ($null -ne $ErrorStringJson.message) { if ($ErrorStringJson.message -eq "Authentication is required to view resource.") { Write-RMError -Message "Authentication credentials have expired, please re-login using cmdlet 'Invoke-RMLogin'" } else { Write-RMError -Message $ErrorStringJson.message } } else { Write-RMError -Message $ErrorObj.ToString() } } else { Write-RMError -Message $ErrorString } } # Currently most of the code is duplicated in this method, this has been # done to avoid regression as this method is expected to be called in lots # of scenarios and we may not be able to verify all cases. function Build-RMError { param( [Parameter(Mandatory)] [System.Management.Automation.ErrorRecord] $ErrorObj ) $ErrorString = $ErrorObj.ToString() [RMReturn]$RMReturn = [RMReturn]::new([RMReturn]::ERROR, @(), @(), @{}) if($ErrorString.Contains("validation_errors")) { $ErrorString = $ErrorString | ConvertFrom-Json foreach($Message in $ErrorString.validation_errors.errors) { if ($null -eq $Message.message) { $RMReturn.AddRMError([RMError]::new("", $ErrorObj.ToString())) break } else { $RMReturn.AddRMError([RMError]::new("", $Message.message)) } } } elseif ($ErrorString.Contains("message") -And $ErrorString.Contains("resolution")) { $ErrorStringJson = $ErrorString | ConvertFrom-Json if ($null -ne $ErrorStringJson.message -and $null -ne $ErrorStringJson.resolution) { $RMReturn.AddRMError([RMError]::new($ErrorStringJson.error_code, $ErrorStringJson.message, $ErrorStringJson.resolution)) } elseif ($null -ne $ErrorStringJson.message) { if ($ErrorStringJson.message -eq "Authentication is required to view resource.") { $RMReturn.AddRMError([RMError]::new($ErrorStringJson.error_code, "Authentication credentials have expired", "Please re-login using cmdlet 'Invoke-RMLogin'")) } else { $RMReturn.AddRMError([RMError]::new($ErrorStringJson.error_code, $ErrorStringJson.message)) } } else { $RMReturn.AddRMError([RMError]::new($ErrorStringJson.error_code, $ErrorObj.ToString())) } } else { $RMReturn.AddRMError([RMError]::new($ErrorString)) } return $RMReturn } function Add-RMErrorAndWarning { param( [RMReturn] $RMReturnObject, [string[]] $ErrorMessage, [string[]] $WarningMessage ) if ($ErrorMessage.Count -gt 0) { $RMReturnObject.SetReturnCode([RMReturn]::ERROR) $ErrorMessage | ForEach-Object -Process {$RMReturnObject.AddRMError([RMError]::new($_))} } if ($WarningMessage.Count -gt 0) { $WarningMessage | ForEach-Object -Process {$RMReturnObject.AddRMWarning([RMWarning]::new($_))} } } |