-
Notifications
You must be signed in to change notification settings - Fork 94
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
feat: add response headers to HttpException #658
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e147c98
feat: (WIP) add response headers to HttpException
karel-rehor cf99eb6
chore: (WIP) reformat test code
karel-rehor 888fb59
feat: (WIP) makes response headers accessible from WriteErrorEvent
karel-rehor 6ed10dc
chore: reformat code in recent changes.
karel-rehor 652fcf6
docs: adds examples of handling HTTP Headers in errors.
karel-rehor 4f9604b
chore: tweak wait polling in new example.
karel-rehor ed14a8f
docs: add API doc and update Examples/README.md
karel-rehor 2b49ff5
docs: tweaking new example.
karel-rehor 94ac2a7
docs: update CHANGELOG.md
karel-rehor b855896
chore: change Headers from raw field to AutoProperty
karel-rehor 4b27cf0
chore: explicitly set nullable context for Headers.
karel-rehor 2a24856
Merge branch 'master' into feat/errorHeaders
bednar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using InfluxDB.Client.Core.Exceptions; | ||
using Newtonsoft.Json.Linq; | ||
using NUnit.Framework; | ||
using RestSharp; | ||
|
||
namespace InfluxDB.Client.Test | ||
{ | ||
[TestFixture] | ||
public class InfluxExceptionTest | ||
{ | ||
[Test] | ||
public void ExceptionHeadersTest() | ||
{ | ||
try | ||
{ | ||
throw HttpException.Create( | ||
JObject.Parse("{\"callId\": \"123456789\", \"message\": \"error in content object\"}"), | ||
new List<HeaderParameter> | ||
{ | ||
new HeaderParameter("Trace-Id", "123456789ABCDEF0"), | ||
new HeaderParameter("X-Influx-Version", "1.0.0"), | ||
new HeaderParameter("X-Platform-Error-Code", "unavailable"), | ||
new HeaderParameter("Retry-After", "60000") | ||
}, | ||
null, | ||
HttpStatusCode.ServiceUnavailable); | ||
} | ||
catch (HttpException he) | ||
{ | ||
Assert.AreEqual("error in content object", he?.Message); | ||
|
||
Assert.AreEqual(4, he?.Headers.Count()); | ||
var headers = new Dictionary<string, string>(); | ||
foreach (var header in he?.Headers) headers.Add(header.Name, header.Value); | ||
Assert.AreEqual("123456789ABCDEF0", headers["Trace-Id"]); | ||
Assert.AreEqual("1.0.0", headers["X-Influx-Version"]); | ||
Assert.AreEqual("unavailable", headers["X-Platform-Error-Code"]); | ||
Assert.AreEqual("60000", headers["Retry-After"]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using InfluxDB.Client.Api.Domain; | ||
using InfluxDB.Client.Writes; | ||
|
||
namespace InfluxDB.Client.Test | ||
{ | ||
[TestFixture] | ||
public class ItErrorEventsTest : AbstractItClientTest | ||
{ | ||
private Organization _org; | ||
private Bucket _bucket; | ||
private string _token; | ||
private InfluxDBClientOptions _options; | ||
|
||
[SetUp] | ||
public new async Task SetUp() | ||
{ | ||
_org = await FindMyOrg(); | ||
_bucket = await Client.GetBucketsApi() | ||
.CreateBucketAsync(GenerateName("fliers"), null, _org); | ||
|
||
// | ||
// Add Permissions to read and write to the Bucket | ||
// | ||
var resource = new PermissionResource(PermissionResource.TypeBuckets, _bucket.Id, null, | ||
_org.Id); | ||
|
||
var readBucket = new Permission(Permission.ActionEnum.Read, resource); | ||
var writeBucket = new Permission(Permission.ActionEnum.Write, resource); | ||
|
||
var loggedUser = await Client.GetUsersApi().MeAsync(); | ||
Assert.IsNotNull(loggedUser); | ||
|
||
var authorization = await Client.GetAuthorizationsApi() | ||
.CreateAuthorizationAsync(_org, | ||
new List<Permission> { readBucket, writeBucket }); | ||
|
||
_token = authorization.Token; | ||
|
||
Client.Dispose(); | ||
|
||
_options = new InfluxDBClientOptions(InfluxDbUrl) | ||
{ | ||
Token = _token, | ||
Org = _org.Id, | ||
Bucket = _bucket.Id | ||
}; | ||
|
||
Client = new InfluxDBClient(_options); | ||
} | ||
|
||
|
||
[Test] | ||
public void HandleEvents() | ||
{ | ||
using (var writeApi = Client.GetWriteApi()) | ||
{ | ||
writeApi.EventHandler += (sender, eventArgs) => | ||
{ | ||
switch (eventArgs) | ||
{ | ||
case WriteSuccessEvent successEvent: | ||
Assert.Fail("Call should not succeed"); | ||
break; | ||
case WriteErrorEvent errorEvent: | ||
Assert.AreEqual("unable to parse 'velocity,unit=C3PO mps=': missing field value", | ||
errorEvent.Exception.Message); | ||
var eventHeaders = errorEvent.GetHeaders(); | ||
if (eventHeaders == null) | ||
{ | ||
Assert.Fail("WriteErrorEvent must return headers."); | ||
} | ||
|
||
var headers = new Dictionary<string, string> { }; | ||
foreach (var hp in eventHeaders) | ||
{ | ||
Console.WriteLine("DEBUG {0}: {1}", hp.Name, hp.Value); | ||
headers.Add(hp.Name, hp.Value); | ||
} | ||
|
||
Assert.AreEqual(4, headers.Count); | ||
Assert.AreEqual("OSS", headers["X-Influxdb-Build"]); | ||
Assert.True(headers["X-Influxdb-Version"].StartsWith('v')); | ||
Assert.AreEqual("invalid", headers["X-Platform-Error-Code"]); | ||
Assert.AreNotEqual("missing", headers.GetValueOrDefault("Date", "missing")); | ||
break; | ||
case WriteRetriableErrorEvent retriableErrorEvent: | ||
Assert.Fail("Call should not be retriable."); | ||
break; | ||
case WriteRuntimeExceptionEvent runtimeExceptionEvent: | ||
Assert.Fail("Call should not result in runtime exception. {0}", runtimeExceptionEvent); | ||
break; | ||
} | ||
}; | ||
|
||
writeApi.WriteRecord("velocity,unit=C3PO mps=", WritePrecision.S, _bucket.Name, _org.Name); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not have a strong opinion on this, but since
Headers
property inRestResponse
class is nullable (T?
), I think it should be here too, ie.And, using a property is more common than a plain field, and follows the style of existing code.
Otherwise good to go after CHANGELOG is updated. The amount of test coverage of the new feature is outstanding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added as suggested