Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 38 additions & 3 deletions core/Command/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use OCP\Defaults;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\ServerVersion;
use OCP\Util;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -20,6 +21,7 @@ public function __construct(
private IConfig $config,
private Defaults $themingDefaults,
private ServerVersion $serverVersion,
private IDBConnection $connection,
) {
parent::__construct('status');
}
Expand All @@ -33,26 +35,56 @@ protected function configure() {
'exit-code',
'e',
InputOption::VALUE_NONE,
'exit with 0 if running in normal mode, 1 when in maintenance mode, 2 when `./occ upgrade` is needed. Does not write any output to STDOUT.'
'exit with 0 if running in normal mode, 1 when in maintenance mode, 2 when `./occ upgrade` is needed, 3 when the database connection failed. Does not write any output to STDOUT.'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$maintenanceMode = $this->config->getSystemValueBool('maintenance', false);
$needUpgrade = Util::needUpgrade();
$installed = $this->config->getSystemValueBool('installed', false);

// Test database connection if Nextcloud is installed
$databaseStatus = 'not_configured';
$databaseError = null;
if ($installed) {
try {
$result = $this->connection->executeQuery('SELECT 1');
$result->closeCursor();
$databaseStatus = 'connected';
} catch (\Exception $e) {
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching the generic \Exception is too broad. Consider catching more specific database-related exceptions (e.g., \Doctrine\DBAL\Exception) to avoid masking unexpected errors.

Suggested change
} catch (\Exception $e) {
} catch (\Doctrine\DBAL\Exception $e) {

Copilot uses AI. Check for mistakes.
$databaseStatus = 'connection_failed';
$databaseError = $e->getMessage();
}
}

$values = [
'installed' => $this->config->getSystemValueBool('installed', false),
'installed' => $installed,
'version' => implode('.', $this->serverVersion->getVersion()),
'versionstring' => $this->serverVersion->getVersionString(),
'edition' => '',
'maintenance' => $maintenanceMode,
'needsDbUpgrade' => $needUpgrade,
'productname' => $this->themingDefaults->getProductName(),
'extendedSupport' => Util::hasExtendedSupport()
'extendedSupport' => Util::hasExtendedSupport(),
'database' => $databaseStatus
];

if ($databaseError) {
$values['database_error'] = $databaseError;
}

if ($input->getOption('verbose') || !$input->getOption('exit-code')) {
$this->writeArrayInOutputFormat($input, $output, $values);

// Add friendly status messages
if (!$installed) {
$output->writeln('<comment>Nextcloud is not installed yet</comment>');
} elseif ($maintenanceMode) {
$output->writeln('<comment>Nextcloud is in maintenance mode</comment>');
} elseif ($databaseStatus === 'connection_failed') {
$output->writeln('<error>Database connection failed: ' . $databaseError . '</error>');
}
}

if ($input->getOption('exit-code')) {
Expand All @@ -62,6 +94,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($needUpgrade === true) {
return 2;
}
if ($databaseStatus === 'connection_failed') {
return 3;
}
}
return 0;
}
Expand Down
Loading
Loading