cakephp/cakephp · error · Cake\Console\Exception\StopException

{%message}

Error message

{%message}

What it means

ConsoleIo::abort() prints the given message as an error and then throws Cake\Console\Exception\StopException to terminate command execution. It is the standard way for a CakePHP console command to bail out early with a user-facing error message and a non-zero exit code (default CommandInterface::CODE_ERROR). The exception signals the command runner to stop processing.

Solutions

  1. Inspect the printed error message to identify which precondition failed in your command
  2. Fix the underlying condition (provide the missing argument, file, or correct configuration)
  3. Catch StopException in test code or wrappers that invoke the command programmatically to assert on the exit code/message
  4. Pass a meaningful exit code via the second argument if callers script around exit codes

Example fix

// before
if (empty($this->args[0])) {
    $io->abort('No file given');
}
// after
if (empty($this->args[0])) {
    $io->error('No file given');
    $this->abortWithMessage = true; // or simply keep abort() but handle StopException in tests
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!is_readable($configPath)) {
    throw new \RuntimeException("Config file missing: $configPath"); // validate before the code path that aborts
}

Try / catch

try {
    $io->abort('Cannot continue');
} catch (\Cake\Console\Exception\StopException $e) {
    // handle stop: log, return exit code
    return $e->getCode();
}

Prevention

When it happens

Trigger: Calling $io->abort('message') or $io->abort('message', $code) anywhere inside a console command when a precondition fails (e.g. missing file, invalid input, failed connection). Any custom command code that decides execution cannot continue.

Common situations: Baking tools aborting when a target path is invalid; commands validating required environment/CLI arguments and aborting; data-import scripts stopping when a source is unreachable; plugins using abort() for unrecoverable states.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12). Data as JSON: /api/errors/8419386a0f16bd56. Report an issue: GitHub.

Appendix: source

Thrown at src/Console/ConsoleIo.php:308

        $messageType = 'success';
        $message = $this->wrapMessageWithType($messageType, $message);

        return $this->out($message, $newlines, $level);
    }

    /**
     * Halts the current process with a StopException.
     *
     * @param string $message Error message.
     * @param int $code Error code.
     * @return never
     * @throws \Cake\Console\Exception\StopException
     */
    public function abort(string $message, int $code = CommandInterface::CODE_ERROR): never
    {
        $this->error($message);

        throw new StopException($message, $code);
    }

    /**
     * Wraps a message with a given message type, e.g. <warning>
     *
     * @param string $messageType The message type, e.g. "warning".
     * @param array<string>|string $message The message to wrap.
     * @return array<string>|string The message wrapped with the given message type.
     */
    protected function wrapMessageWithType(string $messageType, array|string $message): array|string
    {
        if (is_array($message)) {
            foreach ($message as $k => $v) {
                $message[$k] = "<{$messageType}>{$v}</{$messageType}>";
            }
        } else {
            $message = "<{$messageType}>{$message}</{$messageType}>";
        }

View on GitHub (pinned to 1128eba9b0)