Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Last active October 17, 2021 18:03
Show Gist options
  • Save techthoughts2/a047683980b0986be27410b0aa67a655 to your computer and use it in GitHub Desktop.
Save techthoughts2/a047683980b0986be27410b0aa67a655 to your computer and use it in GitHub Desktop.
#-----------------------------------------------------------------------------
Mock Invoke-RestMethod {
[System.Exception]$exception = "The remote server returned an error: (400) Bad Request."
[System.String]$errorId = 'BadRequest'
[Management.Automation.ErrorCategory]$errorCategory = [Management.Automation.ErrorCategory]::InvalidOperation
[System.Object]$target = 'Whatevs'
$errorRecord = New-Object Management.Automation.ErrorRecord ($exception, $errorID, $errorCategory, $target)
[System.Management.Automation.ErrorDetails]$errorDetails = '{"message":"Username does not exist: [user]"}'
$errorRecord.ErrorDetails = $errorDetails
throw $errorRecord
} #endMock
#-----------------------------------------------------------------------------
Mock -CommandName Invoke-RestMethod {
$errorDetails = '{ "ok":false, "error_code":429, "description":"Too Many Requests: retry after 10", "parameters": { "retry_after":10 } }'
$statusCode = 429
$response = New-Object System.Net.Http.HttpResponseMessage $statusCode
$exception = New-Object Microsoft.PowerShell.Commands.HttpResponseException "$statusCode ($($response.ReasonPhrase))", $response
$errorCategory = [System.Management.Automation.ErrorCategory]::InvalidOperation
$errorID = 'WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand'
$targetObject = $null
$errorRecord = New-Object Management.Automation.ErrorRecord $exception, $errorID, $errorCategory, $targetObject
$errorRecord.ErrorDetails = $errorDetails
throw $errorRecord
} #endMock
#-----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment