-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathHttpErrorHandling.cs
More file actions
147 lines (132 loc) · 5.24 KB
/
HttpErrorHandling.cs
File metadata and controls
147 lines (132 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core.Exceptions;
using InfluxDB.Client.Writes;
using RestSharp;
namespace Examples
{
public class HttpErrorHandling
{
private static InfluxDBClient _client;
private static List<string> _lpRecords;
private static void Setup()
{
_client = new InfluxDBClient("http://localhost:9999",
"my-user", "my-password");
var nowMillis = DateTimeOffset.Now.ToUnixTimeMilliseconds();
_lpRecords = new List<string>()
{
$"temperature,location=north value=42 {nowMillis}",
$"temperature,location=north value=17 {nowMillis - 10000}",
$"temperature,location=north value= {nowMillis - 20000}", // one flaky record
$"temperature,location=north value=5 {nowMillis - 30000}"
};
}
private static void TearDown()
{
_client.Dispose();
}
private static Dictionary<string, string> Headers2Dictionary(IEnumerable<HeaderParameter> headers)
{
var result = new Dictionary<string, string>();
foreach (var hp in headers)
result.Add(hp.Name, hp.Value);
return result;
}
private static async Task WriteRecordsAsync()
{
Console.WriteLine("Write records async with one invalid record.");
//
// Write Data
//
var writeApiAsync = _client.GetWriteApiAsync();
try
{
await writeApiAsync.WriteRecordsAsync(_lpRecords, WritePrecision.Ms,
"my-bucket", "my-org");
}
catch (HttpException he)
{
Console.WriteLine(" WARNING write failed");
var headersDix = Headers2Dictionary(he.Headers);
Console.WriteLine(" Caught Exception({0}) \"{1}\"",
he.GetType(),
he.Message);
Console.WriteLine(" Response Status: {0}", he.Status);
Console.WriteLine(" Headers:");
foreach (var key in headersDix.Keys)
Console.WriteLine($" {key}: {headersDix[key]}");
}
finally
{
Console.WriteLine(" ====================");
}
}
private static void WriteRecordsWithErrorEvent()
{
Console.WriteLine("Write records with error event.");
var caughtError = false;
using (var writeApi = _client.GetWriteApi())
{
writeApi.EventHandler += (sender, eventArgs) =>
{
switch (eventArgs)
{
case WriteErrorEvent wee:
Console.WriteLine(" WARNING write failed");
Console.WriteLine(" Received event WriteErrorEvent with:");
Console.WriteLine(" Status: {0}", ((HttpException)wee.Exception).Status);
Console.WriteLine(" Exception: {0}", wee.Exception.GetType());
Console.WriteLine(" Message: {0}", wee.Exception.Message);
Console.WriteLine(" Headers:");
var headersDix = Headers2Dictionary(wee.GetHeaders());
foreach (var key in headersDix.Keys)
Console.WriteLine($" {key}: {headersDix[key]}");
caughtError = true;
break;
default:
throw new Exception("Should only receive WriteErrorEvent");
}
};
Console.WriteLine("Trying the records list");
writeApi.WriteRecords(_lpRecords, WritePrecision.Ms, "my-bucket", "my-org");
var slept = 0;
while (!caughtError && slept < 3001)
{
Thread.Sleep(1000);
slept += 1000;
}
if (!caughtError)
{
Console.WriteLine("WARN, did not encounter expected error");
}
// manually retry the bad record
Console.WriteLine("Manually retrying the bad record.");
writeApi.WriteRecord(_lpRecords[2], WritePrecision.Ms, "my-bucket", "my-org");
caughtError = false;
slept = 0;
while (!caughtError && slept < 3001)
{
Thread.Sleep(1000);
slept += 1000;
}
if (!caughtError)
{
Console.WriteLine("WARN, did not encounter expected error");
}
}
}
public static async Task Main()
{
Console.WriteLine("Main()");
Setup();
await WriteRecordsAsync();
WriteRecordsWithErrorEvent();
TearDown();
}
}
}