-
Notifications
You must be signed in to change notification settings - Fork 247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle errors in MS Graph SDK v5 #1558
Comments
@andrueastman Any update on this? How should we handle errors in the new v5 SDK? There is a difference in documentation between the upgrade to v5 docs and the handling errors doc (same folder in the repository). I think the handling errors doc is old and we should follow the upgrade error handling docs? Do you have a simple example that checks if a user exists and if not, you can create a new user? Should that be in the catch? |
Is this in example the intended way to handle not found? public static async Task AddGroupMemberAsync(GraphServiceClient graphClient, Group group, string memberToAdd)
{
// Get user to add
var user = await graphClient.Users[memberToAdd]
.GetAsync(requestConfig =>
requestConfig.QueryParameters.Select = new string[] { "id", "displayName" });
try
{
var memberOfGroup = await graphClient
.Groups[group.Id]
.Members
.GetAsync(requestConfig =>
{
requestConfig.QueryParameters.Filter = $"id eq '{memberToAdd}'";
});
Console.WriteLine($"User {user.Id} already member of group '{group.DisplayName}'");
}
catch (ODataError odataError) when (odataError.Error.Code.Equals("Request_ResourceNotFound"))
{
//Console.WriteLine(odataError.Error.Code);
//Console.WriteLine(odataError.Error.Message);
Console.WriteLine($"Add user {user.Id} as member to group '{group.DisplayName}'");
ReferenceCreate referenceCreate = new ReferenceCreate();
referenceCreate.OdataId = "https://graph.microsoft.com/v1.0/directoryObjects/" + user.Id;
await graphClient.Groups[group.Id]
.Members
.Ref
.PostAsync(referenceCreate);
Console.WriteLine($"User {user.Id} added as member to group '{group.DisplayName}'");
}
} |
That would be correct @LockTar Another alternative would be to match the status code as failing to find a user would result in a 404 response code. catch (ODataError odataError) when (odataError.ResponseStatusCode.Equals(404)) |
Hi @andrueastman, Thank you for updating the docs but I don't think this should be closed already. I see a few problems with this setup:
catch (ODataError odataError) when (odataError.ResponseStatusCode.Equals(HttpStatusCode.NotFound))
{
// Handle 404 status code
}
|
Thanks for the follow up @LockTar These should be resolved via Where |
Describe the bug
I'm searching for a teams app in the appcatalog. It can be that the app doesn't exist. This would result in an error not found. Following the error handling documentation, this error should be checked with in example:
exception.IsMatch(GraphErrorCode.AccessDenied.ToString()
.I assume that I would receive a GraphErrorCode.ItemNotFound but I get a
NotFound
and that is not in the GraphErrorCode list.To Reproduce
Steps to reproduce the behavior:
Expected behavior
I would expect to catch the exception strongly typed.
The text was updated successfully, but these errors were encountered: