I assume use are using the graph sdk and not the rest api. See docs for getting the response code:
https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/main/docs/errors.md
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
When a Graph API encounters an error, how can I extract the HTTP status code in C#?
I assume use are using the graph sdk and not the rest api. See docs for getting the response code:
https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/main/docs/errors.md
Hello @Giorgio Sfiligoi, Since you're using the Microsoft Graph SDK, you're likely expecting exceptions like ODataError
or ServiceException
when something goes wrong. However, if the internet connection is lost during the request, the SDK throws an HttpRequestException
instead — and in this case, StatusCode
will be null
, and HttpRequestError
is marked as Unknown
.
400 Bad Request
, 403 Forbidden
, or 404 Not Found
.System.Net.Http
layer. It’s thrown when the request fails before it ever reaches the Microsoft Graph service — for example, due to network issues, DNS problems, or no internet connection. Since there's no HTTP response in these cases, there’s no status code available.So, in your case, ODataError
wasn’t throw because both ODataError
and ServiceException
depend on the existence of a valid HTTP response from Microsoft Graph. If the network is disconnected and the request can’t reach the service at all, no response is received — and therefore, no structured error can be parsed. ODataError
is typically used when there is a response body to analyze, which doesn't apply in this scenario.
Hence, The HttpRequestException
is the correct exception to handle in this scenario, and it indicates a problem with the request itself rather than a server-side error.
Hope this clarifies things!
If this answer was helpful, please click "Accept the answer" and mark Yes
, as this can help other community members.
If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.