Skip to content

What is the proper way of performing error handling in scripting? #2247

Answered by plamber
plamber asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,
the latest version of the CLI allows you to make it even simpler.

m365 cli config set --key output --value json
m365 cli config set --key errorOutput --value stdout
m365 cli config set --key showHelpOnFailure --value false
m365 cli config set --key printErrorsAsPlainText --value false

function Get-CLIValue {
  [cmdletbinding()]
  param(
    [parameter(Mandatory = $true, ValueFromPipeline = $true)]
    $input
  )
    $output = $input | ConvertFrom-Json
    if ($output.error -ne $null) {
    throw $output.error
    }
    return $output
 }

try {
   // this command will fail due to missing parameters
   $convertedObject = m365 teams team get | Get-CLIValue 
}
catch  {
    Write-Host $_.…

Replies: 5 comments 14 replies

Comment options

You must be logged in to vote
4 replies
@plamber
Comment options

@waldekmastykarz
Comment options

@plamber
Comment options

@waldekmastykarz
Comment options

Comment options

You must be logged in to vote
7 replies
@waldekmastykarz
Comment options

@waldekmastykarz
Comment options

@waldekmastykarz
Comment options

@plamber
Comment options

@waldekmastykarz
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@veronicageek
Comment options

@plamber
Comment options

Comment options

You must be logged in to vote
1 reply
@plamber
Comment options

Answer selected by waldekmastykarz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants