Skip to content

Commit

Permalink
app: fix exception handling/printing
Browse files Browse the repository at this point in the history
When we moved to use the System.CommandLine library for command line
parsing, we neglected to update the exception handling to match the new
model.
  • Loading branch information
mjcheetham committed Feb 23, 2021
1 parent fed10a6 commit ab8d1f9
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/shared/Microsoft.Git.CredentialManager/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -79,23 +82,12 @@ protected override async Task<int> RunInternalAsync(string[] args)
// Trace the current version and program arguments
Context.Trace.WriteLine($"{Constants.GetProgramHeader()} '{string.Join(" ", args)}'");

try
{
return await rootCommand.InvokeAsync(args);
}
catch (Exception e)
{
if (e is AggregateException ae)
{
ae.Handle(WriteException);
}
else
{
WriteException(e);
}
var parser = new CommandLineBuilder(rootCommand)
.UseDefaults()
.UseExceptionHandler(OnException)
.Build();

return -1;
}
return await parser.InvokeAsync(args);
}

protected override void Dispose(bool disposing)
Expand All @@ -108,6 +100,18 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

private void OnException(Exception ex, InvocationContext invocationContext)
{
if (ex is AggregateException aex)
{
aex.Handle(WriteException);
}
else
{
WriteException(ex);
}
}

private bool WriteException(Exception ex)
{
// Try and use a nicer format for some well-known exception types
Expand Down

0 comments on commit ab8d1f9

Please sign in to comment.