Proper Exception Handling when authoring PnP-PowerShell Cmdlets

%3CLINGO-SUB%20id%3D%22lingo-sub-136550%22%20slang%3D%22en-US%22%3EProper%20Exception%20Handling%20when%20authoring%20PnP-PowerShell%20Cmdlets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-136550%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20wondering%20about%20the%20proper%20or%20recommended%20way%20to%20handle%20Exceptions%20in%20the%20PnP%20PowerShell%20cmdlets.%20I'll%20use%20the%26nbsp%3BGet-PnPTerm%20cmdlet%20as%20an%20example.%20Currently%2C%20if%20the%20term%20does%20not%20exist%20the%20cmdlet%20throws%20a%20non%20terminating%20error%3A%26nbsp%3B%3CSTRONG%3EGet-PnPTerm%20%3A%20Specified%20argument%20was%20out%20of%20the%20range%20of%20valid%20values.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EPersonally%2C%20I%20don't%20see%20this%20as%20an%20exception%2C%20the%20term%20does%20not%20exist%20it's%20one%20of%20the%20expected%20conditions.%20(It%20also%20clutters%20up%20the%20output.%20%3A))%3C%2FP%3E%0A%3CP%3EMy%20question%20is%2C%20should%20the%20cmdlet%20return%20%3CSTRONG%3Enull%3C%2FSTRONG%3Eor%26nbsp%3B%3CSTRONG%3ENotFoundException%3C%2FSTRONG%3Erather%20than%20the%20current%26nbsp%3B%3CSTRONG%3EArgumentOutOfRangeException%3C%2FSTRONG%3E%3F%20Or%2C%20should%20we%20just%20leave%20it%20to%20the%20user%20to%20suppress%20the%20errors%20with%20-ErrorAction%20options%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-136550%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-136650%22%20slang%3D%22en-US%22%3ERe%3A%20Proper%20Exception%20Handling%20when%20authoring%20PnP-PowerShell%20Cmdlets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-136650%22%20slang%3D%22en-US%22%3E%3CP%3EThat%20is%20what%20I%20am%20asking%2C%20in%20general%2C%20if%20I%20write%20new%20cmdlets%2C%20what%20is%20the%20preferred%20handling%20approach%3F%20(That's%20why%20the%20title%20reads%20%22when%20authoring%22.)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-136590%22%20slang%3D%22en-US%22%3ERe%3A%20Proper%20Exception%20Handling%20when%20authoring%20PnP-PowerShell%20Cmdlets%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-136590%22%20slang%3D%22en-US%22%3EIf%20you%20use%20the%20cmdlets%20in%20your%20own%20PS%20script%20I%20think%20you%20can%20handle%20properly%20the%20exceptions%20produced%20when%20executing%20Gt-PnPTerm...the%20other%20option%2C%20since%20PnP%20is%20a%20community%20initiative%2C%20is%20to%20participate%20in%20the%20project%20and%20help%20on%20improving%20these%20cmdlets%3C%2FLINGO-BODY%3E
Highlighted
MVP

I am wondering about the proper or recommended way to handle Exceptions in the PnP PowerShell cmdlets. I'll use the Get-PnPTerm cmdlet as an example. Currently, if the term does not exist the cmdlet throws a non terminating error: Get-PnPTerm : Specified argument was out of the range of valid values.

Personally, I don't see this as an exception, the term does not exist it's one of the expected conditions. (It also clutters up the output. :))

My question is, should the cmdlet return null or NotFoundException rather than the current ArgumentOutOfRangeException? Or, should we just leave it to the user to suppress the errors with -ErrorAction options?

2 Replies
Highlighted
If you use the cmdlets in your own PS script I think you can handle properly the exceptions produced when executing Gt-PnPTerm...the other option, since PnP is a community initiative, is to participate in the project and help on improving these cmdlets
Highlighted

That is what I am asking, in general, if I write new cmdlets, what is the preferred handling approach? (That's why the title reads "when authoring".)