Skip to content

Commit

Permalink
feat: Better handling of unhandled errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-mrzyglod committed Feb 13, 2024
1 parent 1c2c710 commit 57424f9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
35 changes: 35 additions & 0 deletions ace-tests/Reworked/ErrorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using ACE;

namespace ACE_Tests.Reworked;

[Parallelizable(ParallelScope.Self)]
public class ErrorTests
{
[Test]
public async Task Error_WhenThereIsExceptionThrownBecauseOfInvalidTemplatePath_ACEShouldNotCrash()
{
var exitCode = await Program.Main([
"templates/reworked/key-vault/usage-pattern-1.bicep",
"cf70b558-b930-45e4-9048-ebcefb926adf",
"arm-estimator-tests-rg",
"--inline",
"parLocation=northeurope"
]);

Assert.That(exitCode, Is.EqualTo(1));
}

[Test]
public async Task Error_WhenThereIsExceptionThrownBecauseOfWrongParameter_ACEShouldNotCrash()
{
var exitCode = await Program.Main([
"templates/reworked/key-vault/usage-patterns-1.bicep",
"cf70b558-b930-45e4-9048-ebcefb926adf",
"arm-estimator-tests-rg",
"--inline",
"parLocation2=northeurope"
]);

Assert.That(exitCode, Is.EqualTo(1));
}
}
16 changes: 15 additions & 1 deletion ace/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using ACE.WhatIf;
using Microsoft.Extensions.Logging;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
using System.Reflection;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -267,7 +269,19 @@ public static async Task<int> Main(string[] args)
rootCommand.AddCommand(managementGroupCommand);
rootCommand.AddCommand(tenantCommand);

return await rootCommand.InvokeAsync(args);
try
{
var parser = new CommandLineBuilder(rootCommand)
.UseDefaults()
.Build();

return parser.Invoke(args);
}
catch(Exception ex)
{
Console.Error.WriteLine("An error occurred: {0}", ex.Message);
return 1;
}
}

private static async Task<int> Estimate(FileInfo templateFile,
Expand Down

0 comments on commit 57424f9

Please sign in to comment.