-
Notifications
You must be signed in to change notification settings - Fork 0
Error Handling
Jiri Formacek edited this page Jul 4, 2026
·
1 revision
Extracts and deserializes the Microsoft Graph error payload from a PowerShell ErrorRecord.
When a Graph API call fails, the HTTP response body contains a JSON object with an error key holding code, message, and sometimes innerError details. PowerShell stores this raw JSON in $_.ErrorDetails. ConvertFrom-GraphErrorRecord parses it so you can inspect and act on the specific Graph error.
| Parameter | Type | Required | Description |
|---|---|---|---|
ErrorRecord |
ErrorRecord |
Yes | A PowerShell ErrorRecord. Accepts pipeline input ($_ in a catch block). |
System.Object — the deserialized Graph error object when the error details contain a valid Graph JSON error payload. Returns nothing when the payload is absent or not parseable.
# Basic catch and parse
try {
Invoke-GraphWithRetry -RequestUri '/users/does-not-exist@contoso.com' -ErrorAction Stop
}
catch {
$graphError = $_ | ConvertFrom-GraphErrorRecord
Write-Host "Graph error code: $($graphError.error.code)"
Write-Host "Graph error message: $($graphError.error.message)"
}# Parse the most recent error from $Error
$details = $Error[0] | ConvertFrom-GraphErrorRecord
if ($details) {
Write-Warning "Last Graph error: $($details.error.message)"
}# Branch on error code
try {
Get-GraphData -RequestUri "/groups/$groupId" -ErrorAction Stop
}
catch {
$graphError = $_ | ConvertFrom-GraphErrorRecord
switch ($graphError.error.code) {
'Request_ResourceNotFound' { Write-Warning "Group $groupId not found." }
'Authorization_RequestDenied' { Write-Error "Insufficient permissions." }
default { Write-Error -ErrorRecord $_ }
}
}# Used internally by Add-GraphReference for PermissiveModify
try {
Invoke-GraphWithRetry -Method Post -RequestUri $refUri -Body $body -ErrorAction Stop
}
catch {
$details = $_ | ConvertFrom-GraphErrorRecord
if ($details.error.message -match 'object references already exist') {
Write-Verbose "Reference already exists, skipping."
} else {
Write-Error -ErrorRecord $_
}
}{
"error": {
"code": "Request_ResourceNotFound",
"message": "Resource 'user@contoso.com' does not exist or one of its queried reference-property objects are not present.",
"innerError": {
"request-id": "...",
"date": "...",
"client-request-id": "..."
}
}
}- Returns
$nullsilently when the error record has no JSON details or the JSON does not containerror.message. - Does not modify or re-throw the error — the caller decides what to do.
- All write operations in this module use
-ErrorAction Stopinternally when they need to inspect the error type, so you may need-ErrorAction Stopin your own calls to catch terminating errors.
Configuration
Making API Calls
Directory Objects
Files
Diagnostics
Reference