Skip to content

Commit b79c253

Browse files
authored
Update errors.md
1 parent 49ff911 commit b79c253

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

docs/errors.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
Handling errors in the Microsoft Graph .NET Client Library
22
=====
33

4-
Errors in the Microsoft Graph .NET Client Library behave like errors returned from the Microsoft Graph service. You can read more about them [here](https://graph.microsoft.io/en-us/docs/overview/errors).
4+
Errors in the Microsoft Graph .NET Client Library behave like errors returned from the Microsoft Graph service. You can read more about them [here](https://learn.microsoft.com/en-us/graph/errors).
55

6-
Anytime you make a request against the service there is the potential for an error. In the case of an error, the request will throw a `ServiceException` object with an inner `Error` object that contains the service error details.
6+
Anytime you make a request against the service there is the potential for an error. In the case of an error, the request will throw a `ODataError` exception that contains the service error details and can be handled as below.
77

8-
## Checking the error
8+
```cs
9+
try
10+
{
11+
await graphServiceClient.Me.PatchAsync(user);
12+
}
13+
catch (ODataError odataError)
14+
{
15+
Console.WriteLine(odataError.Error.Code);
16+
Console.WriteLine(odataError.Error.Message);
17+
throw;
18+
}
19+
```
20+
21+
22+
## Checking the error status code
923

10-
There are a few different types of errors that can occur during a network call. These error codes are defined in [GraphErrorCode.cs](../src/Microsoft.Graph/Enums/GraphErrorCode.cs).
24+
You can check the status code that caused the error as below.
25+
26+
```csharp
27+
catch (ODataError odataError) when (odataError.ResponseStatusCode.Equals(404))
28+
{
29+
// Handle 404 status code
30+
}
31+
```
32+
33+
## Checking the error
1134

12-
### Checking the error code
13-
You can easily check if an error has a specific code by calling `IsMatch` on the error code value. `IsMatch` is not case sensitive:
35+
There are a few different types of errors that can occur during a network call. These most common error codes are defined in [GraphErrorCode.cs](../src/Microsoft.Graph/Enums/GraphErrorCode.cs). These can be checked by matching with the error code value as below.
1436

1537
```csharp
16-
if (exception.IsMatch(GraphErrorCode.AccessDenied.ToString())
38+
catch (ODataError odataError) when (odataError.Error.Code.Equals(GraphErrorCode.AccessDenied.ToString()))
1739
{
1840
// Handle access denied error
1941
}
2042
```
2143

22-
Each error object has a `Message` property as well as code. This message is for debugging purposes and is not be meant to be displayed to the user. Common error codes are defined in [GraphErrorCode.cs](../src/Microsoft.Graph/Enums/GraphErrorCode.cs).
44+
Each error object has a `Message` property as well as code. This message is for debugging purposes and is not be meant to be displayed to the user. Common error codes are defined in [GraphErrorCode.cs](../src/Microsoft.Graph/Enums/GraphErrorCode.cs).

0 commit comments

Comments
 (0)