microsoftgraph / microsoftgraph/msgraph-sdk-powershell

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

Open
#274 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

AutoREST-dependency Blocked Request: Kiota
Dominant language
C#
Stars
898
Forks
230
Avg merge
2d 5h
Merged PRs (30d)
31

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Get-MgUser lookup shown in the issue and inspect the generated cmdlet's error handling and ErrorRecord construction. The work is done when a missing resource reports ObjectNotFound and the Activity value no longer has the unexplained _Get suffix, with the behavior verified through the displayed CategoryInfo output.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.