Skip to content

Commit

Permalink
Better handling of path not found and uncaught exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rgregg committed Feb 16, 2016
1 parent 0482bbf commit 684f9e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
30 changes: 26 additions & 4 deletions ApiDocs.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ static void Main(string[] args)
FancyConsole.LogFileName = verbOptions.LogFile;

var task = Task.Run(() => RunInvokedMethodAsync(options, verbName, verbOptions));
task.Wait();
try
{
task.Wait();
}
catch (Exception ex)
{
FancyConsole.WriteLine(FancyConsole.ConsoleErrorColor, "Uncaught exception is causing a crash: {0}", ex);
Exit(failure: true, customExitCode: 40);
}
}

public static void LoadCurrentConfiguration(DocSetOptions options)
Expand Down Expand Up @@ -204,7 +212,17 @@ private static void PrintAboutMessage()
private static async Task<DocSet> GetDocSetAsync(DocSetOptions options)
{
FancyConsole.VerboseWriteLine("Opening documentation from {0}", options.DocumentationSetPath);
DocSet set = new DocSet(options.DocumentationSetPath);
DocSet set = null;
try
{
set = new DocSet(options.DocumentationSetPath);
}
catch (System.IO.FileNotFoundException ex)
{
FancyConsole.WriteLine(FancyConsole.ConsoleErrorColor, ex.Message);
Exit(failure: true);
return null;
}

FancyConsole.VerboseWriteLine("Scanning documentation files...");
ValidationError[] loadErrors;
Expand Down Expand Up @@ -964,9 +982,13 @@ private static void ConfigureAdditionalHeadersForAccount(CheckServiceOptions opt
}
}

private static void Exit(bool failure)
private static void Exit(bool failure, int? customExitCode = null)
{
var exitCode = failure ? ExitCodeFailure : ExitCodeSuccess;
int exitCode = failure ? ExitCodeFailure : ExitCodeSuccess;
if (customExitCode.HasValue)
{
exitCode = customExitCode.Value;
}
#if DEBUG
Console.WriteLine("Exit code: " + exitCode);
if (Debugger.IsAttached)
Expand Down
3 changes: 3 additions & 0 deletions ApiDocs.Validation/DocSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ public static T[] TryLoadConfigurationFiles<T>(string path) where T : ConfigFile
List<T> validConfigurationFiles = new List<T>();

DirectoryInfo docSetDir = new DirectoryInfo(path);
if (!docSetDir.Exists)
return new T[0];

var jsonFiles = docSetDir.GetFiles("*.json", SearchOption.AllDirectories);
foreach (var file in jsonFiles)
{
Expand Down

0 comments on commit 684f9e7

Please sign in to comment.