Skip to content

Commit 0d30dab

Browse files
Merge pull request #102 from microsoftgraph/exposeHeaders
Expose response headers and http status code on the ServiceException …
2 parents 23cde55 + 3258ee1 commit 0d30dab

File tree

6 files changed

+70
-11
lines changed

6 files changed

+70
-11
lines changed

src/Microsoft.Graph.Core/Exceptions/ServiceException.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public ServiceException(Error error, Exception innerException = null)
1616

1717
public Error Error { get; private set; }
1818

19+
// ResponseHeaders and StatusCode exposed as pass-through.
20+
public System.Net.Http.Headers.HttpResponseHeaders ResponseHeaders { get; internal set; }
21+
22+
public System.Net.HttpStatusCode StatusCode { get; internal set; }
23+
1924
public bool IsMatch(string errorCode)
2025
{
2126
if (string.IsNullOrEmpty(errorCode))
@@ -48,4 +53,4 @@ public override string ToString()
4853
return null;
4954
}
5055
}
51-
}
56+
}

src/Microsoft.Graph.Core/Microsoft.Graph.Core.nuspec

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@
1313
<projectUrl>https://graph.microsoft.io</projectUrl>
1414
<licenseUrl>http://aka.ms/devservicesagreement</licenseUrl>
1515
<releaseNotes>
16-
November 2016 Release Summary (version 1.3.0)
16+
April 2017 Release Summary (version 1.3.1)
1717

1818
New features
1919

20-
* Added LINQ support for $select and $expand query options.
21-
22-
Bug fixes
23-
24-
* Fixed an issue in BaseRequest.cs so that query options can support a sub-query option.
25-
* Added missing configureAwait(false) in BaseRequest.cs to address potential deadlock scenario.
20+
* Expose HTTP status codes and the response header collection on the ServiceException class.
2621
</releaseNotes>
2722
<tags>Microsoft Office365 Graph GraphServiceClient Outlook OneDrive AzureAD GraphAPI Productivity SharePoint SDK</tags>
2823
<dependencies>

src/Microsoft.Graph.Core/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
//
2525
// You can specify all the values or you can default the Build and Revision Numbers
2626
// by using the '*' as shown below:
27-
[assembly: AssemblyVersion("1.3.0")]
28-
[assembly: AssemblyFileVersion("1.3.0.0")]
27+
[assembly: AssemblyVersion("1.3.1")]
28+
[assembly: AssemblyFileVersion("1.3.1.0")]
2929

3030
#if DEBUG
3131
[assembly: InternalsVisibleTo("Microsoft.Graph.Core.Test")]

src/Microsoft.Graph.Core/Requests/HttpProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,15 @@ public async Task<HttpResponseMessage> SendAsync(
204204
}
205205
}
206206

207-
throw new ServiceException(error);
207+
throw new ServiceException(error)
208+
{
209+
// Pass through the response headers to the ServiceException.
210+
ResponseHeaders = response.Headers,
211+
212+
// System.Net.HttpStatusCode does not support RFC 6585, Additional HTTP Status Codes.
213+
// Throttling status code 429 is in RFC 6586. The status code 429 will be passed through.
214+
StatusCode = response.StatusCode
215+
};
208216
}
209217
}
210218

tests/Microsoft.Graph.Test/Microsoft.Graph.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<Compile Include="Requests\Functional\GraphTestBase.cs" />
9393
<Compile Include="Requests\Functional\MailTests.cs" />
9494
<Compile Include="Requests\Functional\OneDriveTests.cs" />
95+
<Compile Include="Requests\Functional\ErrorTests.cs" />
9596
<Compile Include="Requests\Functional\UsersTests.cs" />
9697
<Compile Include="Requests\Generated\EntityWithReferenceRequestTests.cs" />
9798
<Compile Include="Requests\Generated\EntityReferenceRequestTests.cs" />
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Graph.Test.Requests.Functional
8+
{
9+
[Ignore]
10+
[TestClass]
11+
public class ErrorTests : GraphTestBase
12+
{
13+
[Ignore] // Setup Fiddler autoresponder
14+
[TestMethod]
15+
public async Task ErrorThrottlingError()
16+
{
17+
try
18+
{
19+
// All requests should have a client-request-id set so that the client can correlate a
20+
// request with a response.
21+
var headerOptions = new List<HeaderOption>()
22+
{
23+
new HeaderOption("client-request-id", "dddddddd-dddd-dddd-dddd-dddddddddddd")
24+
};
25+
26+
// To get a throttling error, I mocked up a 429 response in a text file and turned on the Fiddler
27+
// autoresponder to return the text file as the response envelope. The autoresponder for this
28+
// scenario responds to EXACT:https://graph.microsoft.com/v1.0/groups/036bd54c-c6e5-43eb-b8b5-03e019e75bd1
29+
var group = await graphClient.Groups["036bd54c-c6e5-43eb-b8b5-03e019e75bd1"].Request(headerOptions).GetAsync();
30+
}
31+
catch (Microsoft.Graph.ServiceException e)
32+
{
33+
if ((int)e.StatusCode == 429) // Too Many Requests
34+
{
35+
// We have the client-request-id for correlating the response to the request that failed.
36+
IEnumerable<string> clientrequestidvalues;
37+
Assert.IsTrue(e.ResponseHeaders.TryGetValues("client-request-id", out clientrequestidvalues), "client-request-id not found");
38+
39+
// We have the Retry-After that the client can use to wait and resubmit the rejected request.
40+
IEnumerable<string> retryaftervalues;
41+
Assert.IsTrue(e.ResponseHeaders.TryGetValues("Retry-After", out retryaftervalues), "Retry-After not found");
42+
}
43+
else
44+
{
45+
Assert.Fail("Something happened, check out a trace. Error code: {0}", e.Error.Code);
46+
}
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)