Skip to content

Commit 51facfa

Browse files
committed
Plumb error codes through.
Add basic batch file and test cmdlets for error codes and pipeline aliasing
1 parent 845a20e commit 51facfa

File tree

11 files changed

+104
-21
lines changed

11 files changed

+104
-21
lines changed

src/CLU/Microsoft.CLU.Common/ICommandModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Microsoft.CLU.Common
1+
namespace Microsoft.CLU
22
{
33
public enum CommandModelErrorCode
44
{

src/CLU/Microsoft.CLU.Run/CommandExecMode.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public bool CanHandle(string[] arguments)
2727
/// IRunMode implementation for executing command.
2828
/// </summary>
2929
/// <param name="arguments">The arguments</param>
30-
public Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments)
30+
public Microsoft.CLU.CommandModelErrorCode Run(string[] arguments)
3131
{
3232
if (arguments.Length < 2)
3333
{
3434
CLUEnvironment.Console.WriteErrorLine(Strings.CommandExecMode_Run_MissingCommandConfigFileArgument);
35-
return Microsoft.CLU.Common.CommandModelErrorCode.MissingParameters;
35+
return Microsoft.CLU.CommandModelErrorCode.MissingParameters;
3636
}
3737

3838
try
@@ -52,7 +52,7 @@ public Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments)
5252
arguments[argsBase + 1].StartsWith("-", StringComparison.Ordinal))
5353
{
5454
CLUEnvironment.Console.WriteErrorLine(Strings.CommandExecMode_Run_MissingScriptName);
55-
return Microsoft.CLU.Common.CommandModelErrorCode.MissingParameters;
55+
return Microsoft.CLU.CommandModelErrorCode.MissingParameters;
5656
}
5757

5858
CLUEnvironment.ScriptName = arguments[argsBase + 1];
@@ -65,7 +65,7 @@ public Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments)
6565
arguments[argsBase + 1].StartsWith("-", StringComparison.Ordinal))
6666
{
6767
CLUEnvironment.Console.WriteErrorLine(Strings.CommandExecMode_Run_MissingScriptConfigFileName);
68-
return Microsoft.CLU.Common.CommandModelErrorCode.MissingParameters;
68+
return Microsoft.CLU.CommandModelErrorCode.MissingParameters;
6969
}
7070

7171
commandConfiguration = CommandConfig.Load(arguments[argsBase + 1]);
@@ -90,7 +90,7 @@ public Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments)
9090
CLUEnvironment.Console.WriteDebugLine($"{exc.GetType().FullName}\n{exc.StackTrace}");
9191
}
9292

93-
return Common.CommandModelErrorCode.InternalFailure;
93+
return CommandModelErrorCode.InternalFailure;
9494
}
9595

9696
#endregion

src/CLU/Microsoft.CLU.Run/CommandModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class CommandModel
1919
/// </summary>
2020
/// <param name="commandConfiguration"></param>
2121
/// <param name="modelArguments"></param>
22-
public static Microsoft.CLU.Common.CommandModelErrorCode Run(CommandConfig commandConfiguration, string[] modelArguments)
22+
public static Microsoft.CLU.CommandModelErrorCode Run(CommandConfig commandConfiguration, string[] modelArguments)
2323
{
2424
CommandModel commandModel = new CommandModel()
2525
{

src/CLU/Microsoft.CLU.Run/IRunMode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ internal interface IRunMode
1717
/// IRunMode implementation.
1818
/// </summary>
1919
/// <param name="arguments">The arguments</param>
20-
Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments);
20+
Microsoft.CLU.CommandModelErrorCode Run(string[] arguments);
2121
}
2222
}

src/CLU/Microsoft.CLU.Run/PackageManagementMode.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public bool CanHandle(string[] arguments)
3333
/// IRunMode implementation for package managment.
3434
/// </summary>
3535
/// <param name="arguments">The arguments</param>
36-
public Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments)
36+
public Microsoft.CLU.CommandModelErrorCode Run(string[] arguments)
3737
{
3838
_packagesRootPath = CLUEnvironment.GetPackagesRootPath();
3939
try
@@ -44,13 +44,13 @@ public Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments)
4444
{
4545
CLUEnvironment.Console.WriteErrorLine(tie.InnerException.Message);
4646
CLUEnvironment.Console.WriteDebugLine($"{tie.InnerException.GetType().FullName}\n{tie.InnerException.StackTrace}");
47-
return Microsoft.CLU.Common.CommandModelErrorCode.InternalFailure;
47+
return Microsoft.CLU.CommandModelErrorCode.InternalFailure;
4848
}
4949
catch (Exception exc)
5050
{
5151
CLUEnvironment.Console.WriteErrorLine(exc.Message);
5252
CLUEnvironment.Console.WriteDebugLine($"{exc.GetType().FullName}\n{exc.StackTrace}");
53-
return Microsoft.CLU.Common.CommandModelErrorCode.InternalFailure;
53+
return Microsoft.CLU.CommandModelErrorCode.InternalFailure;
5454
}
5555

5656
try
@@ -77,7 +77,7 @@ public Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments)
7777
arguments[argsBase + 1].StartsWith("-", StringComparison.Ordinal))
7878
{
7979
CLUEnvironment.Console.WriteLine(Strings.PackageManagementMode_Run_VersionIdMissing);
80-
return Microsoft.CLU.Common.CommandModelErrorCode.MissingParameters;
80+
return Microsoft.CLU.CommandModelErrorCode.MissingParameters;
8181
}
8282
version = arguments[argsBase + 1];
8383
argsBase += 2;
@@ -145,13 +145,13 @@ public Microsoft.CLU.Common.CommandModelErrorCode Run(string[] arguments)
145145
{
146146
CLUEnvironment.Console.WriteErrorLine(tie.InnerException.Message);
147147
CLUEnvironment.Console.WriteDebugLine($"{tie.InnerException.GetType().FullName}\n{tie.InnerException.StackTrace}");
148-
return Microsoft.CLU.Common.CommandModelErrorCode.InternalFailure;
148+
return Microsoft.CLU.CommandModelErrorCode.InternalFailure;
149149
}
150150
catch (Exception exc)
151151
{
152152
CLUEnvironment.Console.WriteErrorLine(exc.Message);
153153
CLUEnvironment.Console.WriteDebugLine($"{exc.GetType().FullName}\n{exc.StackTrace}");
154-
return Microsoft.CLU.Common.CommandModelErrorCode.InternalFailure;
154+
return Microsoft.CLU.CommandModelErrorCode.InternalFailure;
155155
}
156156

157157
return CommandModelErrorCode.Success;

src/CLU/Microsoft.CLU.Run/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void Main(string[] args) { }
1717
/// Microsoft.CLU.Run (clurun.exe) main entry point.
1818
/// </summary>
1919
/// <param name="args">The commandline arguments</param>
20-
public static Microsoft.CLU.Common.CommandModelErrorCode Execute(string[] args)
20+
public static Microsoft.CLU.CommandModelErrorCode Execute(string[] args)
2121
{
2222
CLUEnvironment.Console = new ConsoleInputOutput(args);
2323

@@ -36,20 +36,20 @@ public static Microsoft.CLU.Common.CommandModelErrorCode Execute(string[] args)
3636
{
3737
CLUEnvironment.Console.WriteErrorLine(exc.Message);
3838
CLUEnvironment.Console.WriteDebugLine(exc.StackTrace);
39-
return Microsoft.CLU.Common.CommandModelErrorCode.InternalFailure;
39+
return Microsoft.CLU.CommandModelErrorCode.InternalFailure;
4040
}
4141
}
4242

4343
/// <summary>
4444
/// Parse the commandline argument and bootstrap the command execution.
4545
/// </summary>
4646
/// <param name="arguments">The commandline arguments</param>
47-
private Microsoft.CLU.Common.CommandModelErrorCode Parse(string [] arguments)
47+
private Microsoft.CLU.CommandModelErrorCode Parse(string [] arguments)
4848
{
4949
if (arguments.Count() == 0)
5050
{
5151
DisplayHelp();
52-
return Common.CommandModelErrorCode.MissingParameters;
52+
return CommandModelErrorCode.MissingParameters;
5353
}
5454

5555
var mode = GetMode(arguments);

src/CLU/Microsoft.CLU.Test/PipelineAliasTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
using System.Management.Automation;
77

8-
namespace Microsoft.Azure.Commands.Resources.Test
8+
namespace Microsoft.CLU.Test
99
{
1010

1111
public class TestRecord
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
using System.Management.Automation;
7+
8+
namespace Microsoft.CLU.Test
9+
{
10+
11+
[Cmdlet(VerbsCommon.Show, "Success")]
12+
public class ShowSuccess: PSCmdlet
13+
{
14+
[Parameter()]
15+
public bool GenerateNonTerminatingError{ get; set; }
16+
17+
[Parameter()]
18+
public bool GenerateTerminatingError{ get; set; }
19+
20+
protected override void ProcessRecord()
21+
{
22+
base.ProcessRecord();
23+
24+
if (GenerateNonTerminatingError)
25+
{
26+
var errorRecord = new ErrorRecord(
27+
new InvalidOperationException("I was asked to generate a non-terminating error"),
28+
"TestAssert",
29+
ErrorCategory.InvalidOperation,
30+
this);
31+
this.WriteError(errorRecord);
32+
}
33+
if (GenerateTerminatingError)
34+
{
35+
var errorRecord = new ErrorRecord(
36+
new InvalidOperationException("I was asked to generate a terminating error"),
37+
"TestAssert",
38+
ErrorCategory.InvalidOperation,
39+
this);
40+
this.ThrowTerminatingError(errorRecord);
41+
}
42+
43+
}
44+
}
45+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
SETLOCAL EnableExtensions
2+
3+
ECHO Test return code success case
4+
call azure success show
5+
REM We expect that the commands above succeed (return 0)
6+
IF ERRORLEVEL 1 (EXIT /B 1)
7+
8+
ECHO Test return codes non terminating error case
9+
call azure success show --generatenonterminatingerror true
10+
if ERRORLEVEL 1 goto :verify_error_code_2
11+
REM We expect the error code to be 1 - so we shouldn't be here
12+
EXIT /B 1
13+
:verify_error_code_2
14+
IF ERRORLEVEL 2 (EXIT /B 1)
15+
16+
ECHO Test return codes terminating error case
17+
call azure success show --generateterminatingerror true
18+
if ERRORLEVEL 2 goto :verify_error_code_3
19+
REM We expect the error code to be 1 - so we shouldn't be here
20+
EXIT /B 1
21+
:verify_error_code_3
22+
IF ERRORLEVEL 3 (EXIT /B 1)
23+
24+
ECHO Test pipeline aliasing
25+
call azure test record new > testrecordnew.json
26+
call azure test record show < testrecordnew.json
27+
28+
REM We expect that the commands above succeed
29+
IF ERRORLEVEL 1 (EXIT /B 1)
30+

src/CLU/Microsoft.CLU/CommandModel/CmdletCommandModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ public CommandModelErrorCode Run(ConfigurationDictionary commandConfiguration, s
7777
binderAndCommand.Invoke();
7878
}
7979

80-
if (runtimeHost.NonTerminatingErrorReported)
80+
if (runtimeHost.TerminatingErrorReported)
81+
{
82+
return CommandModelErrorCode.TerminatingError;
83+
}
84+
else if (runtimeHost.NonTerminatingErrorReported)
8185
{
8286
return CommandModelErrorCode.NonTerminatingError;
8387
}

src/CLU/Microsoft.CLU/System.Management.Automation/CLUHost.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ public bool ShouldProcess(string verboseDescription, string verboseWarning, stri
315315
/// <param name="errorRecord">An ErrorRecord object that describes the error condition</param>
316316
public void ThrowTerminatingError(ErrorRecord errorRecord)
317317
{
318+
TerminatingErrorReported = true;
318319
throw new CmdletTerminateException(errorRecord);
319320
}
320321

@@ -468,7 +469,10 @@ private void WriteExceptionLine(Exception exc)
468469
/// </summary>
469470
internal bool NonTerminatingErrorReported { get; private set; }
470471

471-
472+
/// <summary>
473+
/// At least one terminating error has been reported...
474+
/// </summary>
475+
internal bool TerminatingErrorReported { get; private set; }
472476

473477
private string InterpretStreamPreference(string variable, string input, string current)
474478
{

0 commit comments

Comments
 (0)