Trapping Errors in MS Graph

I was tasked with automating how Outlook contacts are written to a users profile using Microsoft Graph. I wrote about how to do it in my situation here. However, my last task was to accomplish this but with a much larger contact list. This required trapping errors in MS Graph. I will show you the behavior of the MS Graph Rest API when it writes many records.

Possible Errors You Will Get

Microsoft clearly outlines what errors you could get while writing / reading data. In my case the ones I came across were the following: 400, 401,503 and 429

400 – Bad Request – This will happen when the command your are sending or the data you are trying to write are malformed.

A “reading “example is when you have constructed a command in PowerShell that using the $ and ? characters together. It works find in Graph explorer but errors in PowerShell. Using an escape character between the two symbols remedies this ($`?).

A “writing example” is when you have data in a format (a cell in a CSV) that can not be read by the command so it errors out with a 400. When you are writing thousands of records it might be hard to check for format beforehand.

401 – Unauthorized – You would think a token for a session would last for the entire time your are issueing commands in a PowerShell session but it doesn’t. I have learned it lasts about 1 hour.

429 – Too Many Requests – if you issue too many reads or writes in an allotted period of time, Microsoft will throttle you. It is best to wait before you write again. If you keep trying , you will keep getting throttled. Even though I was issuing several writes in a 10 minute period, it wasn’t enough to trigger this error but it is still a consideration.

503 – Service Unavailable – This is mostly to do with network traffic. I am sure the service actually is not down. MS has built enough redundancy in their infrastructure to take care of this. It is like being in a very bad rain storm. Just pull over and wait a few minutes. Then you can start up again.

Trapping these errors will allow you to start up again. I will show you how.

Trapping the Errors

For this section it is good to download the sample script I provided in a previous article and tweak it as necessary.

How to you trap the errors? The part of your script that writes the records needs two things. One, a while loop that counts the records that goes through each record and knows exactly what record number it is (sorry, a for each x in x’s loop won’t do the trick here) and a try / catch block to handle the error.

$Results = “”
$Script:StatusCode = “”

While ($script:x-lt $tot) {

try {

$NewContact = ImportContact -Mailbox $mailbox -token $token -contact $contacts

$Script:StatusCode = $Results.StatusCode

} catch {

$Script:StatusCode = $_.Exception.Response.StatusCode.value__
$script:x = $script:x – 1
Write-Host Error processing contact. Backing up and trying again in 30 seconds…
Start-Sleep -seconds 30

#Login again

#Get Graph Token

Try {
$Token = GetGraphToken -ClientSecret $ClientSecret -ClientID $ClientID -TenantID $TenantID
}
catch {
throw “Error obtaining Token”
break

}

}

$script:x++

}

You can get a little more granular by specifying what to do with each exact error I mentioned about with an if statement inside the catch block but since I know exactly what errors I am going to get I left it general.

What this code accomplishes is it goes through each record and writes it to the users contact folder. If it encounters an error a long the way, it waits 30 seconds, tries the record that failed again and if successful, keeps writing records until it encounters an error again (hopefully not) and starts the process over again.

I have tested this several times with a contact list of thousands of records. and it works like a charm. One test showed three errors but every single record was written.

Happy IT’ing

Dan

Avatar photo

I am an IT professional with over twenty years experience in the field. I have supported thousands of users over the years. The organizations I have worked for range in size from one person to hundreds of people. I have performed support from Help Desk, Network / Cloud Administration, Network Support, Application Support, Implementation and Security.

Pin It on Pinterest