chocolatey/choco · error · ApplicationException

Too many bad attempts. Stopping before application crash.

Error message

Too many bad attempts. Stopping before application crash.

What it means

Thrown by InteractivePrompt.PromptForConfirmation when the `repeat` counter drops below zero. The method is designed to recurse (or loop) decrementing `repeat` each time the user supplies an invalid/empty answer; once retries are exhausted the count goes negative and this ApplicationException halts further prompting to avoid an unbounded or crash-prone loop.

Source

Thrown at src/chocolatey/infrastructure/commandline/InteractivePrompt.cs:48

        private static Lazy<IConsole> _console = new Lazy<IConsole>(() => new Console());
        private const int TimeoutInSeconds = 30;

        [EditorBrowsable(EditorBrowsableState.Never)]
        public static void InitializeWith(Lazy<IConsole> console)
        {
            _console = console;
        }

        private static IConsole Console
        {
            get { return _console.Value; }
        }

        public static string PromptForConfirmation(string prompt, IEnumerable<string> choices, string defaultChoice, bool requireAnswer, bool allowShortAnswer = true, bool shortPrompt = false, int repeat = 10, int timeoutInSeconds = 0)
        {
            if (repeat < 0)
            {
                throw new ApplicationException("Too many bad attempts. Stopping before application crash.");
            }

            Ensure.That(() => prompt).NotNull();
            Ensure.That(() => choices).NotNull();
            Ensure
                .That(() => choices)
                .Meets(
                    c => c.Count() > 0,
                    (name, value) => { throw new ApplicationException("No choices passed in. Please ensure you pass choices"); });

            if (!string.IsNullOrWhiteSpace(defaultChoice))
            {
                Ensure
                    .That(() => choices)
                    .Meets(
                        c => c.Contains(defaultChoice),
                        (name, value) => { throw new ApplicationException("Default choice value must be one of the given choices."); });
            }

View on GitHub (pinned to 0d5abdd10c)

Solutions

  1. Provide a non-empty defaultChoice that is a member of choices so an empty Enter yields a valid answer instead of burning a retry.
  2. Increase the `repeat` argument if legitimate user mistakes are likely (e.g. pass repeat: 20).
  3. Run in an interactive context with a real TTY, or pre-validate the caller's choices/defaultChoice so bad-input paths cannot recur indefinitely.
  4. Pipe a valid answer into stdin when running non-interactively.

Example fix

// before
InteractivePrompt.PromptForConfirmation(prompt, choices, defaultChoice: null, requireAnswer: true);

// after
InteractivePrompt.PromptForConfirmation(prompt, choices, defaultChoice: choices.First(), requireAnswer: true);
Defensive patterns

Strategy: validation

Validate before calling

if (!choices.Any()) throw new ArgumentException("choices must not be empty");
if (requireAnswer && string.IsNullOrWhiteSpace(defaultChoice))
{
    throw new ArgumentException("requireAnswer requires a non-empty defaultChoice present in choices");
}
if (!string.IsNullOrWhiteSpace(defaultChoice) && !choices.Contains(defaultChoice))
{
    throw new ArgumentException("defaultChoice must be one of choices");
}
var safeRepeat = Math.Max(1, repeat);

Try / catch

try
{
    answer = InteractivePrompt.PromptForConfirmation(prompt, choices, defaultChoice, requireAnswer, repeat: 10);
}
catch (ApplicationException ex) when (ex.Message.Contains("Too many bad attempts"))
{
    logger.Error("User input retries exhausted; falling back to default action.");
    answer = defaultChoice;
}

Prevention

When it happens

Trigger: Calling PromptForConfirmation with a small or exhausted `repeat` value (default 10) and the user repeatedly giving input that matches none of the supplied choices, or pressing Enter with no defaultChoice set while requireAnswer is true. Each invalid iteration decrements repeat until it falls under 0.

Common situations: Non-interactive or scripted runs where stdin is closed/empty, so every prompt attempt is 'bad' and the retry budget is consumed instantly. Misconfigured prompts that supply choices that can never match the expected input format.

Related errors


AI-assisted analysis of chocolatey/choco@0d5abdd10c (2026-08-13). Data as JSON: /api/errors/897d0142b54493b9. Report an issue: GitHub.