Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Aspire.Cli/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Aspire.Cli.Commands;

internal abstract class BaseCommand : Command
{
private static readonly int[] s_suppressErrorLogsMessageExitCodes = [CliExitCodes.Cancelled, CliExitCodes.MissingRequiredArgument];

protected virtual bool UpdateNotificationsEnabled { get; } = true;

/// <summary>
Expand Down Expand Up @@ -60,6 +62,16 @@ protected BaseCommand(string name, string description, IFeatures features, ICliU
// Error messages have already been displayed by the interaction service.
result = CommandResult.Failure(CliExitCodes.MissingRequiredArgument);
}
catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested || ex is ExtensionOperationCanceledException)
{
result = CommandResult.Cancelled();
}
catch (Exception ex)
{
var errorMessage = string.Format(CultureInfo.CurrentCulture, InteractionServiceStrings.UnexpectedErrorOccurred, ex.Message);
telemetry.RecordError(errorMessage, ex);
result = CommandResult.Failure(CliExitCodes.InvalidCommand, errorMessage);
}

var isErrorExitCode = result.ExitCode != CliExitCodes.Success;

Expand All @@ -82,7 +94,7 @@ protected BaseCommand(string name, string description, IFeatures features, ICliU
// Display the CLI log file path on non-zero exit codes so the user knows
// where to find diagnostic details. Suppress for user-input errors where
// the log wouldn't contain useful context (e.g., missing required arguments).
if (isErrorExitCode && result.ExitCode != CliExitCodes.MissingRequiredArgument)
if (isErrorExitCode && !s_suppressErrorLogsMessageExitCodes.Contains(result.ExitCode))
{
interactionService.DisplayMessage(
KnownEmojis.PageFacingUp,
Expand Down
12 changes: 0 additions & 12 deletions src/Aspire.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -865,18 +865,6 @@ public static async Task<int> Main(string[] args)
// Log exit code for debugging
logger.LogInformation("Exit code: {ExitCode}", exitCode);
}
Comment thread
JamesNK marked this conversation as resolved.
catch (OperationCanceledException ex) when (cancellationManager.IsCancellationRequested || ex is ExtensionOperationCanceledException)
{
exitCode = CliExitCodes.Cancelled;
}
catch (Exception ex)
{
exitCode = CliExitCodes.InvalidCommand;

logger.LogError(ex, "An unexpected error occurred.");
telemetry.RecordError("An unexpected error occurred.", ex);
errorWriter.WriteLine(string.Format(CultureInfo.CurrentCulture, InteractionServiceStrings.UnexpectedErrorOccurred, ex.Message));
}
finally
{
mainActivity?.SetTag(TelemetryConstants.Tags.ProcessExitCode, exitCode);
Expand Down
Loading