-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add response headers to HttpException (#658)
* feat: (WIP) add response headers to HttpException * chore: (WIP) reformat test code * feat: (WIP) makes response headers accessible from WriteErrorEvent * chore: reformat code in recent changes. * docs: adds examples of handling HTTP Headers in errors. * chore: tweak wait polling in new example. * docs: add API doc and update Examples/README.md * docs: tweaking new example. * docs: update CHANGELOG.md * chore: change Headers from raw field to AutoProperty * chore: explicitly set nullable context for Headers. --------- Co-authored-by: Jakub Bednář <jakub.bednar@gmail.com>
- Loading branch information
1 parent
fd2ea47
commit f212951
Showing
10 changed files
with
405 additions
and
0 deletions.
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
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.