AtlassianPS / JiraPS

PowerShell module to interact with Atlassian JIRA
https://AtlassianPS.org/module/JiraPS
MIT License
323 stars 131 forks source link

Improve Errors in module #199

Closed lipkau closed 6 years ago

lipkau commented 6 years ago

Expected Behavior

The errors returned by the module are meaningful

Current Behavior

The errors returned by the module contain a good text (returned by the server), but it doesn't show where the error happened. It shows the point in the internal functions where the module got the error from the server

Possible Solution

Have the $PSCmdlet from the calling script pass down to the internals and use $PSCmdlet.ThrowError() to write to the error stream.

The following internal functions would be helpful (source):

function ThrowError
{
    param
    (        
        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCmdlet]
        $CallerPSCmdlet,

        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.String]        
        $ExceptionName,

        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $ExceptionMessage,

        [System.Object]
        $ExceptionObject,

        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $ErrorId,

        [parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [System.Management.Automation.ErrorCategory]
        $ErrorCategory
    )

    $exception = New-Object $ExceptionName $ExceptionMessage;
    $errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, $ErrorId, $ErrorCategory, $ExceptionObject    
    $CallerPSCmdlet.ThrowTerminatingError($errorRecord)
}
function Invoke-JiraMethod {
    #Requires -Version 3
    [CmdletBinding()]
    param(
...
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCmdlet]
        $CallerPSCmdlet = $PSCmdlet
    )
...

Things to consider:

lipkau commented 6 years ago

https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.errorrecord?redirectedfrom=MSDN&view=powershellsdk-1.1.0 https://gist.github.com/wpsmith/e8a9c54ca1c7c741b5e9