Skip to content

Commit 52f8547

Browse files
committed
Handle Bad Requests like points with Double.NaN
1 parent 77bdfcb commit 52f8547

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

AdysTech.InfluxDB.Client.Net.Test/InfluxDBClientTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,30 @@ public async Task TestPostPointsAsync()
357357
}
358358

359359

360+
[TestMethod]
361+
[ExpectedException (typeof (InfluxDBException))]
362+
public async Task TestPostPointAsync_InvalidReq()
363+
{
364+
var client = new InfluxDBClient (influxUrl, dbUName, dbpwd);
365+
var time = DateTime.Now;
366+
var today = DateTime.Now.ToShortDateString ();
367+
var now = DateTime.Now.ToShortTimeString ();
368+
369+
var valDouble = new InfluxDatapoint<double> ();
370+
valDouble.UtcTimestamp = DateTime.UtcNow;
371+
valDouble.Tags.Add ("TestDate", today);
372+
valDouble.Tags.Add ("TestTime", now);
373+
valDouble.Fields.Add ("Doublefield", DataGen.RandomDouble ());
374+
valDouble.Fields.Add ("Doublefield2", Double.NaN);
375+
valDouble.MeasurementName = measurementName;
376+
valDouble.Precision = TimePrecision.Seconds;
377+
378+
var r = await client.PostPointAsync (dbName, valDouble);
379+
Assert.IsTrue (r, "PostPointsAsync retunred false");
380+
381+
}
382+
383+
360384
[TestMethod]
361385
[ExpectedException (typeof (InfluxDBException))]
362386
public async Task TestPostPointsAsync_PartialWrite()

AdysTech.InfluxDB.Client.Net/InfluxDBClient.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,28 @@ public async Task<bool> PostPointAsync(string dbName, IInfluxDatapoint point)
413413
if ( response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadGateway || ( response.StatusCode == HttpStatusCode.InternalServerError && response.ReasonPhrase == "INKApi Error" ) ) //502 Connection refused
414414
throw new UnauthorizedAccessException ("InfluxDB needs authentication. Check uname, pwd parameters");
415415
//if(response.StatusCode==HttpStatusCode.NotFound)
416+
else if ( response.StatusCode == HttpStatusCode.BadRequest )
417+
{
418+
var content = await response.Content.ReadAsStringAsync ();
419+
//regex assumes error text from https://github.com/influxdata/influxdb/blob/master/models/points.go ParsePointsWithPrecision
420+
//fmt.Sprintf("'%s': %v", string(block[start:len(block)])
421+
List<string> parts; bool partialWrite;
422+
if ( content.Contains ("partial write") )
423+
{
424+
if ( content.Contains ("\\n") )
425+
parts = Regex.Matches (content.Substring (content.IndexOf ("partial write:\\n") + 16), @"([\P{Cc}].*?) '([\P{Cc}].*?)':([\P{Cc}].*?)\\n").ToList ();
426+
else
427+
parts = Regex.Matches (content.Substring (content.IndexOf ("partial write:\\n") + 16), @"([\P{Cc}].*?) '([\P{Cc}].*?)':([\P{Cc}].*?)").ToList ();
428+
partialWrite = true;
429+
}
430+
else
431+
{
432+
parts = Regex.Matches (content, @"{\""error"":""([9\P{Cc}]+) '([\P{Cc}]+)':([a-zA-Z0-9 ]+)").ToList ();
433+
partialWrite = false;
434+
}
435+
throw new InfluxDBException (partialWrite ? "Partial Write" : "Failed to Write", String.Format ("{0}: {1} due to {2}", partialWrite ? "Partial Write" : "Failed to Write", parts[0], parts[2]), point);
436+
return false;
437+
}
416438
else if ( response.StatusCode == HttpStatusCode.NoContent )
417439
{
418440
point.Saved = true;

0 commit comments

Comments
 (0)