src/_Internals/Test-XrmNotFoundError.ps1
|
function Test-XrmNotFoundError { param( [Parameter(Mandatory = $true)] [System.Management.Automation.ErrorRecord] $ErrorRecord ) # A Dataverse fault decides by its code: ObjectDoesNotExist (0x80040217) for a missing row, table, column, option set, # relationship, key or component. A wrong table or column name in a row request has its own codes (0x80041102, 0x80041103): # an error to raise, not "not found" (M16: a typo in a table name passed for a missing row with -IfExists) $exception = $ErrorRecord.Exception; while ($null -ne $exception) { if ($exception -is [System.ServiceModel.FaultException[Microsoft.Xrm.Sdk.OrganizationServiceFault]]) { return ($exception.Detail.ErrorCode -eq -2147220969); } $exception = $exception.InnerException; } # No fault (error raised elsewhere): message patterns $messages = New-Object System.Collections.Generic.List[string]; $exception = $ErrorRecord.Exception; while ($null -ne $exception) { if (-not [string]::IsNullOrWhiteSpace($exception.Message)) { $messages.Add($exception.Message) | Out-Null; } $exception = $exception.InnerException; } if (-not [string]::IsNullOrWhiteSpace($ErrorRecord.ToString())) { $messages.Add($ErrorRecord.ToString()) | Out-Null; } if ($messages.Count -eq 0) { return $false; } $notFoundPatterns = @( "could not find", "not found", "does not exist", "doesn't exist", "cannot find", "was not found" ); foreach ($message in $messages) { # A table missing from the metadata cache is a wrong name, not a missing row if ($message.IndexOf("MetadataCache", [System.StringComparison]::OrdinalIgnoreCase) -ge 0) { return $false; } foreach ($pattern in $notFoundPatterns) { if ($message.IndexOf($pattern, [System.StringComparison]::OrdinalIgnoreCase) -ge 0) { return $true; } } } return $false; } |