Common/Error/Error.psm1

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) {
            Write-Error $Message.message
        }
    } elseif ($ErrorString.Contains("message") -And $ErrorString.Contains("resolution")) {
        $ErrorString = $ErrorString | ConvertFrom-Json
        if ($ErrorString.message -eq "Authentication is required to view resource.") {
            throw "Authentication credentials have expired, please re-login using cmdlet 'Invoke-RMLogin'"
        } else {
            Write-Error $ErrorString.message
            Write-Error $ErrorString.resolution
        }
    } else {
        Write-Error $ErrorString
    }
}

Export-ModuleMember -Function Show-RMError