Skip to content

Commit 174f4f8

Browse files
committed
Adds timeout handling
1 parent f155a70 commit 174f4f8

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/https/Program.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ public async Task<int> RunAsync(string[] args)
222222
)
223223
: new HttpClient();
224224

225+
if (options.Timeout.HasValue)
226+
{
227+
http.Timeout = options.Timeout.Value;
228+
}
229+
225230
var request = new HttpRequestMessage(
226231
command.Method ?? HttpMethod.Get,
227232
command.Uri
@@ -265,6 +270,11 @@ public async Task<int> RunAsync(string[] args)
265270

266271
await renderer.WriteResponse(response);
267272
}
273+
catch (TaskCanceledException ex)
274+
{
275+
renderer.WriteException(ex);
276+
return 1;
277+
}
268278
catch (HttpRequestException ex)
269279
{
270280
renderer.WriteException(ex);
@@ -291,18 +301,21 @@ class Options
291301
public ContentType RequestContentType { get; }
292302
public string XmlRootName { get; }
293303
public bool IgnoreCertificate { get; }
304+
public TimeSpan? Timeout { get; }
294305

295-
public Options(ContentType requestContentType, string xmlRootName, bool ignoreCertificate)
306+
public Options(ContentType requestContentType, string xmlRootName, bool ignoreCertificate, TimeSpan? timeout)
296307
{
297308
RequestContentType = requestContentType;
298309
XmlRootName = xmlRootName;
299310
IgnoreCertificate = ignoreCertificate;
311+
Timeout = timeout;
300312
}
301313
public static Options Parse(IEnumerable<string> args)
302314
{
303315
var requestContentType = ContentType.Json;
304316
var xmlRootName = default(string);
305317
var ignoreCertificate = false;
318+
var timeout = default(TimeSpan?);
306319
foreach (var arg in args)
307320
{
308321
if (arg.StartsWith("--json"))
@@ -334,8 +347,20 @@ public static Options Parse(IEnumerable<string> args)
334347
{
335348
ignoreCertificate = true;
336349
}
350+
else if (arg.StartsWith("--timeout"))
351+
{
352+
var index = arg.IndexOf('=');
353+
if (index > -1)
354+
{
355+
var s = arg.Substring(index + 1).Trim();
356+
if (TimeSpan.TryParse(s, out var to) && to > TimeSpan.Zero)
357+
{
358+
timeout = to;
359+
}
360+
}
361+
}
337362
}
338-
return new Options(requestContentType, xmlRootName, ignoreCertificate);
363+
return new Options(requestContentType, xmlRootName, ignoreCertificate, timeout);
339364
}
340365
}
341366

@@ -450,6 +475,9 @@ public void WriteException(Exception ex)
450475
var help = WriteException(ex, 0);
451476
switch (help)
452477
{
478+
case ExceptionHelp.Timeout:
479+
_info.WriteLine("Request failed to complete within timeout. Try increasing the timeout with the --timeout flag");
480+
break;
453481
case ExceptionHelp.IgnoreCertificate:
454482
_info.WriteLine("Ensure you trust the server certificate or try using the --ignore-certificate flag");
455483
break;
@@ -465,11 +493,18 @@ ExceptionHelp WriteException(Exception ex, int depth)
465493
_info.WriteLine(ex.Message);
466494

467495
var exceptionHelp = ExceptionHelp.None;
468-
switch (ex.Message)
496+
if (ex is TaskCanceledException)
469497
{
470-
case "The SSL connection could not be established, see inner exception.":
471-
exceptionHelp = ExceptionHelp.IgnoreCertificate;
472-
break;
498+
return ExceptionHelp.Timeout;
499+
}
500+
else
501+
{
502+
switch (ex.Message)
503+
{
504+
case "The SSL connection could not be established, see inner exception.":
505+
exceptionHelp = ExceptionHelp.IgnoreCertificate;
506+
break;
507+
}
473508
}
474509

475510
if (ex.InnerException != null)
@@ -488,6 +523,7 @@ enum ExceptionHelp
488523
{
489524
None = 0,
490525
IgnoreCertificate = 1,
526+
Timeout = 2
491527
}
492528
}
493529

0 commit comments

Comments
 (0)