Skip to content

Set the property error category when an item is not found, and clean up Activity property #274

Open
@KirkMunro

Description

@KirkMunro

If I attempt to retrieve a resource that does not exist, I get an exception. That exception currently shows the category for the error as InvalidOperation, which is incorrect. The operation was valid, but the resource was not found.

For example, run this:

Get-MgUser -UserId (New-Guid).Guid -ErrorVariable lookupError
$lookupError.CategoryInfo

This script results in an error from the first command (you'll get an error because there is no user with that random GUID that you just created), and then it shows the category information for the error. The output from the second command will look something like this:

Category   : InvalidOperation
Activity   : Get-MgUser_Get
Reason     : Exception
TargetName : { UserId = 4854a621-9117-4cd3-999b-5f7ca6882432, Property = , ExpandProperty =  }
TargetType : <>f__AnonymousType0`3

The categorization of this error is being done incorrectly, and that makes it difficult for scripters to properly handle a scenario when a resource is not found. For example, you delete a resource and then want to make sure that it was deleted. The category of InvalidOperation should be reserved for operations that failed due to the operation being invalid. When a resource is not found, you should use [System.Management.Automation.ErrorCategory]::ObjectNotFound as the category instead.

Further, for the Activity property, why does it show an extra "_Get" at the end? I invoked Get-MgUser. I expect the activity would be simply Get-MgUser. What is the significance of the extra verb appended on the end of that string?
AB#7414

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions