Internal/Resolve-JiraError.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function Resolve-JiraError {
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $true,
            Position = 0
        )]
        [Object[]] $InputObject,

        # Write error results to the error stream (Write-Error) instead of to the output stream
        [Switch] $WriteError
    )

    process {
        foreach ($i in $InputObject) {
            Write-Debug "[Resolve-JiraError] Processing object [$i]"
            if ($i.errorMessages) {
                foreach ($e in $i.errorMessages) {
                    if ($WriteError) {
                        Write-Debug "[Resolve-JiraError] Writing Error output for error [$e]"
                        Write-Error "Jira encountered an error: [$($e)]"
                    }
                    else {
                        Write-Debug "[Resolve-JiraError] Creating PSObject for error [$e]"
                        $obj = [PSCustomObject] @{
                            'Message' = $e;
                        }

                        $obj.PSObject.TypeNames.Insert(0, 'JiraPS.Error')
                        $obj | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
                            Write-Output "Jira error [$($this.Message)"
                        }
                        Write-Output $obj
                    }
                }
            }
            elseif ($i.errors) {
                $keys = (Get-Member -InputObject $i.errors | Where-Object -FilterScript {$_.MemberType -eq 'NoteProperty'}).Name
                foreach ($k in $keys) {
                    if ($WriteError) {
                        Write-Debug "[Resolve-JiraError] Writing Error output for error [$k]"
                        Write-Error "Jira encountered an error: [$($k)] - $($i.errors.$k)"
                    }
                    else {
                        Write-Debug "[Resolve-JiraError] Creating PSObject for error [$k]"
                        $obj = [PSCustomObject] @{
                            'Key'     = $k;
                            'Message' = $i.errors.$k;
                        }

                        $obj.PSObject.TypeNames.Insert(0, 'JiraPS.Error')
                        $obj | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
                            Write-Output "Jira error [$($this.ID)]: $($this.Message)"
                        }

                        Write-Output $obj
                    }
                }
            }
            else {
                Write-Debug "[Resolve-JiraError] Object [$i] does not have an errors property"
            }
        }
    }
}