|
| 1 | +function Format-RaindropError { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Formats detailed error information from a Raindrop API call. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Extracts HTTP status code, status description, and exception message from an error object |
| 8 | + to provide comprehensive error details for Raindrop API failures. |
| 9 | +
|
| 10 | + .PARAMETER ErrorRecord |
| 11 | + The error record from a catch block. |
| 12 | +
|
| 13 | + .PARAMETER Url |
| 14 | + The URL that was being accessed when the error occurred. |
| 15 | +
|
| 16 | + .PARAMETER Operation |
| 17 | + A description of the operation being performed (e.g., "export", "backup"). |
| 18 | + If not provided, uses the name of the calling cmdlet. |
| 19 | +
|
| 20 | + .EXAMPLE |
| 21 | + try { |
| 22 | + Invoke-RestMethod -Uri $url -Headers $headers -Method Get |
| 23 | + } |
| 24 | + catch { |
| 25 | + Format-RaindropError -ErrorRecord $_ -Url $url |
| 26 | + } |
| 27 | + #> |
| 28 | + param ( |
| 29 | + [Parameter(Mandatory)] |
| 30 | + $ErrorRecord, |
| 31 | + |
| 32 | + [Parameter(Mandatory)] |
| 33 | + [string] $Url, |
| 34 | + |
| 35 | + [Parameter(Mandatory = $false)] |
| 36 | + [string] $Operation |
| 37 | + ) |
| 38 | + |
| 39 | + # If Operation not provided, use the calling cmdlet's name |
| 40 | + if ([string]::IsNullOrEmpty($Operation)) { |
| 41 | + $Operation = (Get-PSCallStack)[1].Command |
| 42 | + } |
| 43 | + |
| 44 | + $errorMessage = $ErrorRecord.Exception.Message |
| 45 | + $statusCode = $ErrorRecord.Exception.Response.StatusCode.value__ |
| 46 | + $statusDescription = $ErrorRecord.Exception.Response.StatusDescription |
| 47 | + |
| 48 | + $errorDetails = @() |
| 49 | + $errorDetails += "Failed to run '$Operation'" |
| 50 | + $errorDetails += "(URL: $Url)" |
| 51 | + |
| 52 | + if ($statusCode) { |
| 53 | + $httpStatus = "HTTP $statusCode" |
| 54 | + if ($statusDescription) { |
| 55 | + $httpStatus += " ($statusDescription)" |
| 56 | + } |
| 57 | + $errorDetails += $httpStatus |
| 58 | + } |
| 59 | + |
| 60 | + $errorDetails += "Message: $errorMessage" |
| 61 | + |
| 62 | + return $errorDetails -join " | " |
| 63 | +} |
0 commit comments