Skip to content

Commit

Permalink
Changes to throw unhandled exceptions to the caller (#233)
Browse files Browse the repository at this point in the history
* Changes to throw unhandled exceptions to the caller

* Refactoring

* Cathing only DcoumentException in wrapper
  • Loading branch information
suskumar-MSFT authored Jun 2, 2021
1 parent 84ccf3c commit 27a241f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/PAModel/CanvasDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private static CanvasDocument Wrapper(Func<CanvasDocument> worker, ErrorContaine
}
return document;
}
catch (Exception e)
catch (DocumentException e)
{
if (!errors.HasErrors)
{
Expand All @@ -196,7 +196,7 @@ private static void Wrapper(Action worker, ErrorContainer errors)
{
worker();
}
catch (Exception e)
catch (DocumentException e)
{
if (!errors.HasErrors)
{
Expand Down
59 changes: 45 additions & 14 deletions src/PASopa/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void Main(string[] args)
Console.WriteLine($"MsApp/Source converter. Version: {SourceSerializer.CurrentSourceVersion}");

var mode = args.Length > 0 ? args[0]?.ToLower() : null;
if (mode =="-test")
if (mode == "-test")
{
if (args.Length < 2)
{
Expand All @@ -46,15 +46,15 @@ static void Main(string[] args)
int countTotal = 0;
int countPass = 0;
Console.WriteLine("Test roundtripping all .msapps in : " + msAppPathDir);
foreach(var msAppPath in Directory.EnumerateFiles(msAppPathDir, "*.msapp", SearchOption.TopDirectoryOnly))
foreach (var msAppPath in Directory.EnumerateFiles(msAppPathDir, "*.msapp", SearchOption.TopDirectoryOnly))
{
Stopwatch sw = Stopwatch.StartNew();
bool ok = MsAppTest.StressTest(msAppPath);
var str = ok ? "Pass" : "FAIL";
countTotal++;
if (ok) { countPass++; }
sw.Stop();
Console.WriteLine($"Test: {Path.GetFileName(msAppPath)}: {str} ({sw.ElapsedMilliseconds/1000}s)");
Console.WriteLine($"Test: {Path.GetFileName(msAppPath)}: {str} ({sw.ElapsedMilliseconds / 1000}s)");
}
Console.WriteLine($"{countPass}/{countTotal} ({countPass * 100 / countTotal}% passed");
}
Expand Down Expand Up @@ -86,15 +86,15 @@ static void Main(string[] args)

Console.WriteLine($"Unpack: {msAppPath} --> {outDir} ");

(CanvasDocument msApp, ErrorContainer errors) = CanvasDocument.LoadFromMsapp(msAppPath);
(CanvasDocument msApp, ErrorContainer errors) = TryOperation(() => CanvasDocument.LoadFromMsapp(msAppPath));
errors.Write(Console.Error);

if (errors.HasErrors)
{
return;
}

errors = msApp.SaveToSources(outDir);
errors = TryOperation(() => msApp.SaveToSources(outDir));
errors.Write(Console.Error);
if (errors.HasErrors)
{
Expand All @@ -103,7 +103,7 @@ static void Main(string[] args)

// Test that we can repack
{
(CanvasDocument msApp2, ErrorContainer errors2) = CanvasDocument.LoadFromSources(outDir);
(CanvasDocument msApp2, ErrorContainer errors2) = TryOperation(() => CanvasDocument.LoadFromSources(outDir));
errors2.Write(Console.Error);
if (errors2.HasErrors)
{
Expand All @@ -112,7 +112,7 @@ static void Main(string[] args)

using (var temp = new TempFile())
{
errors2 = msApp2.SaveToMsAppValidation(temp.FullPath);
errors2 = TryOperation(() => msApp2.SaveToMsAppValidation(temp.FullPath));
errors2.Write(Console.Error);
if (errors2.HasErrors)
{
Expand All @@ -136,13 +136,13 @@ static void Main(string[] args)

Console.WriteLine($"Pack: {inputDir} --> {msAppPath} ");

(CanvasDocument msApp, ErrorContainer errors) = CanvasDocument.LoadFromSources(inputDir);
(CanvasDocument msApp, ErrorContainer errors) = TryOperation(() => CanvasDocument.LoadFromSources(inputDir));
errors.Write(Console.Error);
if (errors.HasErrors)
{
return;
}
errors = msApp.SaveToMsApp(msAppPath);
errors = TryOperation(() => msApp.SaveToMsApp(msAppPath));
errors.Write(Console.Error);
if (errors.HasErrors)
{
Expand All @@ -165,13 +165,13 @@ static void Main(string[] args)

var appName = Path.GetFileName(msAppPath);

(var app, var errors) = CanvasDocument.MakeFromSources(appName, pkgsPath, new List<string>() { inputPA });
(var app, var errors) = TryOperation(() => CanvasDocument.MakeFromSources(appName, pkgsPath, new List<string>() { inputPA }));
errors.Write(Console.Error);
if (errors.HasErrors)
{
return;
}
errors = app.SaveToMsApp(msAppPath);
errors = TryOperation(() => app.SaveToMsApp(msAppPath));
errors.Write(Console.Error);
if (errors.HasErrors)
{
Expand All @@ -195,21 +195,21 @@ static void Main(string[] args)
Console.WriteLine($"Merge: {path1}, {path2} --> {pathresult} ");


(var app1, var errors1) = CanvasDocument.LoadFromSources(path1);
(var app1, var errors1) = TryOperation(() => CanvasDocument.LoadFromSources(path1));
errors1.Write(Console.Error);
if (errors1.HasErrors)
{
return;
}

(var app2, var errors2) = CanvasDocument.LoadFromSources(path2);
(var app2, var errors2) = TryOperation(() => CanvasDocument.LoadFromSources(path2));
errors2.Write(Console.Error);
if (errors2.HasErrors)
{
return;
}

(var parentApp, var errors3) = CanvasDocument.LoadFromSources(parent);
(var parentApp, var errors3) = TryOperation(() => CanvasDocument.LoadFromSources(parent));
errors3.Write(Console.Error);
if (errors3.HasErrors)
{
Expand Down Expand Up @@ -244,5 +244,36 @@ private static void Usage()
");
}

private static (CanvasDocument, ErrorContainer) TryOperation(Func<(CanvasDocument, ErrorContainer)> operation)
{
CanvasDocument app = null;
ErrorContainer errors = new ErrorContainer();
try
{
(app, errors) = operation();
}
catch (Exception e)
{
// Add unhandled exception to the error container.
errors.InternalError(e);
}
return (app, errors);
}

private static ErrorContainer TryOperation(Func<ErrorContainer> operation)
{
ErrorContainer errors = new ErrorContainer();
try
{
errors = operation();
}
catch (Exception e)
{
// Add unhandled exception to the error container.
errors.InternalError(e);
}
return errors;
}
}
}

0 comments on commit 27a241f

Please sign in to comment.