Skip to content

Commit

Permalink
Merge pull request #474 from WildernessLabs/feature/improve-command-e…
Browse files Browse the repository at this point in the history
…xception

Improve usage of logger and command exception
  • Loading branch information
ctacke committed Feb 14, 2024
2 parents f0ca6f9 + be9f647 commit 75b9330
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 48 deletions.
17 changes: 8 additions & 9 deletions Source/v2/Meadow.Cli/CommandErrors.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Meadow.CLI
namespace Meadow.CLI;

public enum CommandExitCode
{
public enum CommandErrors
{
Success = 0,
GeneralError = 1,
UserCancelled = 2,
FileNotFound = 3,
DirectoryNotFound = 4,
}
Success = 0,
GeneralError = 1,
UserCancelled = 2,
FileNotFound = 3,
DirectoryNotFound = 4,
}
37 changes: 37 additions & 0 deletions Source/v2/Meadow.Cli/CommandException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Meadow.CLI;

/// <inheritdoc/>
public class CommandException : CliFx.Exceptions.CommandException
{
public CommandException(string message)
: base(message, (int)CommandExitCode.GeneralError, showHelp: false, innerException: null)
{ }

public CommandException(string message, bool showHelp)
: base(message, (int)CommandExitCode.GeneralError, showHelp, innerException: null)
{ }

public CommandException(string message, CommandExitCode exitCode)
: base(message, (int)exitCode, showHelp: false, innerException: null)
{ }

public CommandException(string message, CommandExitCode exitCode, bool showHelp)
: base(message, (int)exitCode, showHelp, innerException: null)
{ }

public CommandException(string message, Exception innerException)
: base(message, (int)CommandExitCode.GeneralError, showHelp: false, innerException)
{ }

public CommandException(string message, bool showHelp, Exception innerException)
: base(message, (int)CommandExitCode.GeneralError, showHelp, innerException)
{ }

public CommandException(string message, CommandExitCode exitCode, Exception innerException)
: base(message, (int)exitCode, showHelp: false, innerException: innerException)
{ }

public CommandException(string message, CommandExitCode exitCode, bool showHelp, Exception innerException)
: base(message, (int)exitCode, showHelp, innerException: innerException)
{ }
}
5 changes: 2 additions & 3 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Meadow.Package;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -32,7 +31,7 @@ protected override ValueTask ExecuteCommand()
// is it a valid directory?
if (!Directory.Exists(path))
{
throw new CommandException($"Invalid application path '{path}'", (int)CommandErrors.FileNotFound);
throw new CommandException($"Invalid application path '{path}'", CommandExitCode.FileNotFound);
}
}

Expand All @@ -45,7 +44,7 @@ protected override ValueTask ExecuteCommand()

if (!success)
{
throw new CommandException($"Build failed.", (int)CommandErrors.GeneralError);
throw new CommandException("Build failed", CommandExitCode.GeneralError);
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Meadow.Hcom;
using Meadow.Package;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -37,7 +36,7 @@ protected override async ValueTask ExecuteCommand()
// is it a valid directory?
if (!Directory.Exists(path))
{
throw new CommandException($"Invalid application path '{path}'", (int)CommandErrors.FileNotFound);
throw new CommandException($"Invalid application path '{path}'", CommandExitCode.FileNotFound);
}
}

Expand Down
32 changes: 12 additions & 20 deletions Source/v2/Meadow.Cli/Commands/Current/BaseCommand.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using CliFx;
using CliFx.Exceptions;
using CliFx.Infrastructure;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

public abstract class BaseCommand<T> : ICommand
{
protected ILogger<T>? Logger { get; }
protected ILoggerFactory? LoggerFactory { get; }
protected IConsole? Console { get; private set; }
private IConsole? _console;

protected ILogger<T> Logger { get; }
protected ILoggerFactory LoggerFactory { get; }
protected IConsole Console => _console ?? throw new InvalidOperationException("The Console property has not yet been initialized. It can only be used within in the ExecuteCommand() method.");
protected CancellationToken CancellationToken { get; private set; }

public BaseCommand(ILoggerFactory? loggerFactory)
public BaseCommand(ILoggerFactory loggerFactory)
{
LoggerFactory = loggerFactory;
Logger = loggerFactory?.CreateLogger<T>();
Logger = loggerFactory.CreateLogger<T>();
}

protected abstract ValueTask ExecuteCommand();
Expand All @@ -24,28 +25,19 @@ public async ValueTask ExecuteAsync(IConsole console)
{
try
{
Console = console;
CancellationToken = Console.RegisterCancellationHandler();
_console = console;
CancellationToken = _console.RegisterCancellationHandler();

await ExecuteCommand();
}
catch (CommandException)
{
throw;
}
catch (Exception ex)
catch (Exception ex) when (ex is not CommandException && ex is not CliFx.Exceptions.CommandException)
{
throw new CommandException(
message: ex.Message,
exitCode: (int)CommandErrors.GeneralError,
innerException: ex);
throw new CommandException(ex.Message, ex);
}

if (CancellationToken.IsCancellationRequested)
{
throw new CommandException(
message: "Cancelled",
exitCode: (int)CommandErrors.UserCancelled);
throw new CommandException("Cancelled", CommandExitCode.UserCancelled);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Exceptions;
using CliFx.Extensibility;
using CliFx.Extensibility;
using System.Text.Json;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Meadow.Cloud.Client;
using Meadow.Cloud.Client.Identity;
using Meadow.Package;
Expand Down Expand Up @@ -47,21 +46,21 @@ protected override async ValueTask ExecuteCommand()
ProjectPath = Path.GetFullPath(ProjectPath);
if (!Directory.Exists(ProjectPath))
{
throw new CommandException($"Directory not found '{ProjectPath}'. Check path to project file.", (int)CommandErrors.DirectoryNotFound);
throw new CommandException($"Directory not found '{ProjectPath}'. Check path to project file.", CommandExitCode.DirectoryNotFound);
}

// build
Logger?.LogInformation($"Building {Configuration} version of application...");
if (!_packageManager.BuildApplication(ProjectPath, Configuration, true, CancellationToken))
{
throw new CommandException($"Build failed.", (int)CommandErrors.GeneralError);
throw new CommandException($"Build failed");
}

var candidates = PackageManager.GetAvailableBuiltConfigurations(ProjectPath, "App.dll");

if (candidates.Length == 0)
{
throw new CommandException($"Cannot find a compiled application at '{ProjectPath}'", (int)CommandErrors.FileNotFound);
throw new CommandException($"Cannot find a compiled application at '{ProjectPath}'", CommandExitCode.FileNotFound);
}

var store = _fileManager.Firmware["Meadow F7"];
Expand All @@ -86,7 +85,7 @@ protected override async ValueTask ExecuteCommand()
}
else
{
throw new CommandException($"Package assembly failed.", (int)CommandErrors.GeneralError);
throw new CommandException($"Package assembly failed");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down
1 change: 0 additions & 1 deletion Source/v2/Meadow.Cli/Commands/Legacy/FlashOsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CliFx.Attributes;
using CliFx.Exceptions;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand Down

0 comments on commit 75b9330

Please sign in to comment.