Catch and display project validation exceptions correctly

This commit is contained in:
Pavel Krymets 2015-12-11 12:36:30 -08:00
parent 59fb1ad3b7
commit e0d3d7e85d
1 changed files with 37 additions and 5 deletions

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using Microsoft.Extensions.CompilationAbstractions; using Microsoft.Extensions.CompilationAbstractions;
namespace Microsoft.Dnx.Watcher.Core namespace Microsoft.Dnx.Watcher.Core
@ -12,19 +13,28 @@ namespace Microsoft.Dnx.Watcher.Core
{ {
public bool TryReadProject(string projectFile, out IProject project, out string errors) public bool TryReadProject(string projectFile, out IProject project, out string errors)
{ {
errors = null;
project = null;
Runtime.Project runtimeProject; Runtime.Project runtimeProject;
if (!TryGetProject(projectFile, out runtimeProject, out errors)) if (!TryGetProject(projectFile, out runtimeProject, out errors))
{ {
project = null;
return false; return false;
} }
errors = null; try
project = new Project(runtimeProject); {
project = new Project(runtimeProject);
}
catch (Exception ex)
{
errors = CollectMessages(ex);
return false;
}
return true; return true;
} }
// Same as TryGetProject but it doesn't throw // Same as TryGetProject but it doesn't throw
private bool TryGetProject(string projectFile, out Runtime.Project project, out string errorMessage) private bool TryGetProject(string projectFile, out Runtime.Project project, out string errorMessage)
{ {
@ -43,11 +53,33 @@ namespace Microsoft.Dnx.Watcher.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
errorMessage = ex.Message; errorMessage = CollectMessages(ex);
} }
project = null; project = null;
return false; return false;
} }
private string CollectMessages(Exception exception)
{
var builder = new StringBuilder();
builder.AppendLine(exception.Message);
var aggregateException = exception as AggregateException;
if (aggregateException != null)
{
foreach (var message in aggregateException.Flatten().InnerExceptions.Select(CollectMessages))
{
builder.AppendLine(message);
}
}
while (exception.InnerException != null)
{
builder.AppendLine(CollectMessages(exception.InnerException));
exception = exception.InnerException;
}
return builder.ToString();
}
} }
} }