[Client bug]: BaseRequest and HttpProvider don't deal with 404 NotFound
Describe the bug The implementation of the NotFound code is not working as you would typically expect from a dotnet SDK library.
The following request call doesn't give the developer the option to "check" if the non-existent user exists without going through a ServiceException.
await _graphClient.Users["[email protected]"].Request().GetAsync();
Even if you work with the response, it still throws:
var request = _graphClient.Users["[email protected]"].Request();
var result = await request.GetResponseAsync(cancellationToken);
The reason why it does that is in the HttpProvider implementation:

errorResponse.Error is not null, it contains a response message and code for the IUserRequest.
To Reproduce To reproduce, try to check if a user exists and handle a NotFound response without causing a runtime exception.
Expected behavior My expectation is for the Microsoft Graph dotnet library team to not throw exceptions when they can be avoided, just like the dotnet colleagues suggest: https://learn.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions
GetResponseAsync equivalents should give the developer the option to gracefully deal with an error codes if they choose to.
The library does not provide the ability to issue a HEAD request either.
In my case, I am retrieving a user and I want to handle cases when the user doesn't exist without exceptions. It is recommended that exceptions are not used for expected scenarios.
Client version 4.52.0
Desktop (please complete the following information):
Bump. Any updates on this? I strugle with the exact same issue. I also use on 4.X version
The change of the behavior of the HttpProvider can be customized using the IHttpProvider. The SDK provides a default implementation to make it easier for common scenario of API errors. The example below shows a default implementation that does no error handling
public class CustomHttpProvider : IHttpProvider
{
internal readonly HttpClient _httpClient;
public CustomHttpProvider()
{
_httpClient = GraphClientFactory.Create(authenticationProvider:null);
Serializer = new Serializer();
}
public ISerializer Serializer { get; private set; }
public TimeSpan OverallTimeout { get => _httpClient.Timeout; set => _httpClient.Timeout = value; }
public void Dispose() => _httpClient?.Dispose();
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
{
return SendAsync(request, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
}
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
{
return _httpClient.SendAsync(request, completionOption, cancellationToken);
}
}
Then initializing the client as below
var httpProvider = new CustomHttpProvider();
var graphClient = new GraphServiceClient(new InteractiveBrowserCredential("clientId"),scopes, httpProvider);
Also, the http request message can be obtained from the builders and passed directly to a client if the raw HttpResponseMessage is desired.
var httpClient = GraphClientFactory.Create(new TokenCredentialAuthProvider(new InteractiveBrowserCredential("11e584da-5b80-404b-9a3c-0d510c105513"), scopes)); //configured HTTPclient
var request = graphClient.Users["[email protected]"].Request().GetHttpRequestMessage(); //get message from builders
var response = await httpClient.SendAsync(request);// send
We'll be closing this one for now as the 4.x is out of support and won't be pulling new features with the move to Kiota.
This issue is still very much present in version 5. I need to wrap the calls in order to not crash on a 404. Also interesting side note. Why is the steam on the photo nullable if the client crashes on a 404?
try
{
using Stream? photoStream = await _gsClient.Me.Photo.Content.GetAsync();
pbyte = photoStream == null ? [] : ((MemoryStream)photoStream).ToArray();
}
catch (ODataError e)
{
//do nothing, prevent the server from crashing on a 404
}