Private/Format-RyverV1Error.ps1

function Format-RyverV1Error {
    <#
    .SYNOPSIS
        Parse error messages.
 
    .DESCRIPTION
        Parse error messages.
 
    .INPUTS
        System.Management.Automation.PSCustomObject[]
 
    .INPUTS
        System.Management.Automation.PSCustomObject
 
    .NOTES
        - Troy Lindsay
        - Twitter: @troylindsay42
        - GitHub: tlindsay42
 
    .EXAMPLE
 
    .LINK
        https://tlindsay42.github.io/PSRyver/Private/Format-RyverV1Error/
 
    .LINK
        https://github.com/tlindsay42/PSRyver/blob/master/PSRyver/Private/Format-RyverV1Error.ps1
 
    .FUNCTIONALITY
        Ryver
    #>

    [CmdletBinding(
        HelpUri = 'https://tlindsay42.github.io/PSRyver/Private/Format-RyverV1Error/'
    )]
    [OutputType( [Void] )]
    param (
        # The response object from Ryver's API.
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $true
        )]
        [Object]
        $ResponseObject,

        # The exception from Invoke-RestMethod, if available.
        [Parameter( Position = 1 )]
        [Exception]
        $Exception
    )

    begin {
        $function = $MyInvocation.MyCommand.Name

        Write-Verbose -Message (
            "Beginning: '${function}' with ParameterSetName '$( $PSCmdlet.ParameterSetName )' and Parameters: " +
            ( $PSBoundParameters | Remove-SensitiveData | Format-Table -AutoSize | Out-String )
        )

        $RyverErrorData = @{
            # Messages are adapted from Ryver API documentation
            missing_scope     = @{
                Message           = 'The method requires a scope that your token is missing. For example, a user might need to be in the admin scope'
                RecommendedAction = 'Review the API method documentation (https://api.ryver.com/methods), these generally list valid scopes (https://api.ryver.com/scopes)'
            }

            invalid_arg_name  = @{
                Message           = 'The method was passed an argument whose name falls outside the bounds of accepted or expected values. This includes very long names and names with non-alphanumeric characters other than _.'
                RecommendedAction = 'Verify API call is well-formed.'
            }

            invalid_array_arg = @{
                Message           = 'The method was passed a PHP-style array argument (e.g. with a name like foo[7]). These are never valid with the Ryver API.'
                RecommendedAction = 'Rename or remove the argument'
            }

            invalid_charset   = @{
                Message = 'The method was called via a POST request, but the charset specified in the Content-Type header was invalid. Valid charset names are: utf-8 iso-8859-1.'
            }

            invalid_form_data = @{
                Message = 'The method was called via a POST request with Content-Type application/x-www-form-urlencoded or multipart/form-data, but the form data was either missing or syntactically invalid.'
            }

            invalid_post_type = @{
                Message = 'The method was called via a POST request, but the specified Content-Type was invalid. Valid types are: application/x-www-form-urlencoded multipart/form-data text/plain.'
            }

            missing_post_type = @{
                Message = 'The method was called via a POST request and included a data payload, but the request did not include a Content-Type header.'
            }

            team_added_to_org = @{
                Message           = 'The workspace associated with your request is currently undergoing migration to an Enterprise Organization. Web API and other platform operations will be intermittently unavailable until the transition is complete.'
                RecommendedAction = 'Wait until migration is complete, then try the request again.'
            }

            request_timeout   = @{
                Message = 'The method was called via a POST request, but the POST data was either missing or truncated.'
            }

            fatal_error       = @{
                Message = 'The server could not complete your operation(s) without encountering a catastrophic error. Some aspect of the operation may have succeeded before the error was raised.'
            }

            not_authed        = @{
                Message           = 'No authentication token provided.'
                RecommendedAction = 'Specify an authentication token via the -Token or -URI parameters, then try again.'
            }

            invalid_auth      = @{
                Message = 'Some aspect of authentication cannot be validated. Either the provided token is invalid or the request originates from an IP address disallowed from making the request.'
            }

            account_inactive  = @{
                Message = 'Authentication token is for a deleted user or workspace.'
            }

            no_permission     = @{
                Message = 'The workspace token used in this request does not have the permissions necessary to complete the request.'

            }

            ratelimited       = @{
                Message           = 'Ryver API rate-limit exceeded.'
                RecommendedAction = 'Try again in a few moments.'
            }
        }
    }

    process {
        if ( $ResponseObject.OK ) {
            # We weren't actually given an error in this case
            Write-Debug -Message 'Format-RyverV1Error: Received non-error response, skipping.'
        }
        else {
            $errorParameters = $RyverErrorData[$ResponseObject.Error]

            if ( $null -eq $errorParameters ) {
                $errorParameters = @{
                    Message = "Unknown error $( $ResponseObject.Error ) received from Ryver API."
                }
            }

            if ( $Exception ) {
                $errorParameters.Exception = $Exception
            }

            Write-Error -ErrorID $ResponseObject.Error @errorParameters
        }
    }

    end {
        Write-Verbose -Message "Ending: '${function}'."
    }
}