Skip to content

Commit 515ca71

Browse files
committed
fix exception message handling for unknown errors
display code if message is empty store entire exception object in LastGraphError property along with URL for easy error tracing
1 parent 9d4d116 commit 515ca71

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Modules/CIPPCore/Public/GraphHelper/New-GraphGetRequest.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function New-GraphGetRequest {
5959
$RetryCount = 0
6060
$MaxRetries = 3
6161
$RequestSuccessful = $false
62-
Write-Host "This is attempt $($RetryCount + 1) of $MaxRetries"
62+
Write-Information "GET [$nextURL] | tenant: $tenantid | attempt: $($RetryCount + 1) of $MaxRetries"
6363
do {
6464
try {
6565
$GraphRequest = @{
@@ -117,11 +117,24 @@ function New-GraphGetRequest {
117117
} catch {
118118
$ShouldRetry = $false
119119
$WaitTime = 0
120-
121120
try {
122-
$Message = ($_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue).error.message
121+
$MessageObj = $_.ErrorDetails.Message | ConvertFrom-Json -ErrorAction SilentlyContinue
122+
if ($MessageObj.error) {
123+
$MessageObj | Add-Member -NotePropertyName 'url' -NotePropertyValue $nextURL -Force
124+
$Message = $MessageObj.error.message -ne '' ? $MessageObj.error.message : $MessageObj.error.code
125+
}
123126
} catch { $Message = $null }
124-
if ($Message -eq $null) { $Message = $($_.Exception.Message) }
127+
128+
if ([string]::IsNullOrEmpty($Message)) {
129+
$Message = $($_.Exception.Message)
130+
$MessageObj = @{
131+
error = @{
132+
code = $_.Exception.GetType().FullName
133+
message = $Message
134+
url = $nextURL
135+
}
136+
}
137+
}
125138

126139
# Check for 429 Too Many Requests
127140
if ($_.Exception.Response.StatusCode -eq 429) {
@@ -147,7 +160,7 @@ function New-GraphGetRequest {
147160
} else {
148161
# Final failure - update tenant error tracking and throw
149162
if ($Message -ne 'Request not applicable to target tenant.' -and $Tenant) {
150-
$Tenant.LastGraphError = $Message
163+
$Tenant.LastGraphError = [string]($MessageObj | ConvertTo-Json -Compress)
151164
if ($Tenant.PSObject.Properties.Name -notcontains 'GraphErrorCount') {
152165
$Tenant | Add-Member -MemberType NoteProperty -Name 'GraphErrorCount' -Value 0 -Force
153166
}

0 commit comments

Comments
 (0)