microsoftgraph / microsoftgraph/msgraph-sdk-dotnet
Handle errors with NativeResponseHandler in V5
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 789
- Forks
- 264
- Avg merge
- 15h 17m
- Merged PRs (30d)
- 3
Description
With V4 we could just catch ServiceException, get the error and log/handle it.
With V5 we have a new way to handle long running operations.
For Clone, Archive, Unarchive Team, etc we can use NativeResponseHandler to get the result of execution. The problem is that if you intend to use it, then you should also take care about handling errors yourself, parsing the response content, because it won't throw any exception explicitly, hence you can't use try/catch.
And I suppose it's passable until we have another way to handle long running operations. But the bigger problem here is that the response error is not so easy to extract.
Let's have a look at this example which extracts our message:
public async static Task<string> GetLocationHeader(this NativeResponseHandler nativeResponseHandler)
{
var responseMessage = nativeResponseHandler.Value as HttpResponseMessage;
if (!responseMessage.IsSuccessStatusCode)
{
var result = await responseMessage.Content.ReadAsStringAsync();
var ex = JsonSerializer.Deserialize<GraphAsyncException>(result); // this line interests us
throw new CustomGraphException(ex?.Error?.Message);
}
var locationString = responseMessage.Headers.Location?.OriginalString;
if (locationString == null)
throw new CustomGraphException("Location header is missing");
return locationString;
}
The JSON structure we get from the Content:
public class GraphAsyncException
{
[JsonPropertyName("error")]
public GraphAsyncExceptionError Error { get; set; }
}
public class GraphAsyncExceptionError
{
[JsonPropertyName("code")]
public string Code { get; set; }
[JsonPropertyName("message")]
public string Message { get; set; } // this is what we are looking for
}
And now about the problem... Message property has the following content:
Failed to execute Templates backend request ArchiveRequest. Request Url: https://teams.microsoft.com/fabric/emea/templates/api/groups/{group_id}/Archive, Request Method: POST, Response Status Code: BadRequest, Response Headers: Strict-Transport-Security: max-age=2592000
x-operationid: someId
x-telemetryid: someId
X-MSEdge-Ref: someData
Date: someDate
, ErrorMessage : {"errors":[{"message":"Team already archived","errorCode":"Unknown"}],"operationId":"someId"}
It would be good to to extract the exception from the ErrorMessage part which is obviously a JSON. But as you can see this message itself is a raw string, which should be additionaly parsed.
Please, guide me if there is another way to make this any simpler?
Summarizing the upgade from V4 with this particular case, instead of a simple try catch block, we have to add a lot of code to maintain the same behaviour.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing NativeResponseHandler and the V5 handling path for long-running operations such as Clone and Archive. Compare the GraphAsyncException response with the nested ErrorMessage JSON; done should provide a simpler error-handling path that exposes the underlying message without requiring callers to parse the raw response themselves.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 32/100