Skip to content

Commit 524a9a4

Browse files
committed
Error handling
1 parent df978bb commit 524a9a4

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

Command/EasyBackupBackupCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
namespace KimaiPlugin\EasyBackupBundle\Command;
1111

1212
use Symfony\Component\Console\Command\Command;
13-
use Symfony\Component\Console\Attribute\AsCommand;
14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
1513
use Symfony\Component\Console\Input\InputInterface;
1614
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Security\Core\Exception\RuntimeException;
1716

1817
use KimaiPlugin\EasyBackupBundle\Service\EasyBackupService;
1918

2019
// the name of the command is what users type after "php bin/console"
2120

22-
class EasyBackupBackupCommand extends ContainerAwareCommand
21+
class EasyBackupBackupCommand extends Command
2322
{
2423
protected static $defaultName = 'EasyBackup:backup';
2524
// the command description shown when running "php bin/console list"

Controller/EasyBackupController.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Routing\Annotation\Route;
2222

2323
use KimaiPlugin\EasyBackupBundle\Service\EasyBackupService;
24+
use Symfony\Component\Security\Core\Exception\RuntimeException;
2425

2526
#[IsGranted('easy_backup')]
2627
#[Route('/admin/easy-backup')]
@@ -124,15 +125,15 @@ public function indexAction(): Response
124125
#[Route('/create_backup', name: 'create_backup', methods: ['GET', 'POST'])]
125126
public function createBackupAction(): Response
126127
{
127-
$log = $this->easyBackupService->createBackup();
128+
try {
129+
$this->easyBackupService->createBackup();
130+
} catch (RuntimeException $e) {
131+
$this->flashError($e->getMessage());
128132

129-
if (preg_match('/ERROR/i', $log)) {
130-
$this->flashError('backup.action.create.error');
131-
} else {
133+
} finally {
132134
$this->flashSuccess('backup.action.create.success');
135+
return $this->redirectToRoute('easy_backup');
133136
}
134-
135-
return $this->redirectToRoute('easy_backup');
136137
}
137138

138139
/**

Service/EasyBackupService.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
19+
use Symfony\Component\Security\Core\Exception\RuntimeException;
1920

2021
/**
2122
* @Service
@@ -75,7 +76,7 @@ private function log(string $logLevel, string $message): void
7576
$dateTime = date('Y-m-d H:i:s');
7677
$this->filesystem->appendToFile($logFile, "[$dateTime] $logLevel: $message" . PHP_EOL);
7778
} catch (\Exception $e) {
78-
$this->flashError('filesystem.mkdir.error.backupDir');
79+
throw new RuntimeException('filesystem.mkdir.error.backupDir');
7980
}
8081
}
8182

@@ -262,8 +263,8 @@ public function backupDatabase(string $sqlDumpName): void
262263
$errorsStr = trim($errorsStr, PHP_EOL);
263264

264265
if (!empty($errorsStr)) {
265-
$this->flashError($errorsStr);
266266
$this->log(self::LOG_ERROR_PREFIX, $errorsStr);
267+
throw new RuntimeException($errorsStr);
267268
}
268269
}
269270
}
@@ -298,18 +299,18 @@ public function zipData(string $source, string $destination): bool
298299
$zip->addFromString(basename($source), file_get_contents($source) ?: '');
299300
}
300301
} else {
301-
$this->flashError('backup.action.zip.error.destination');
302302
$this->log(self::LOG_ERROR_PREFIX, "Couldn't open '$destination'.");
303+
throw new RuntimeException('backup.action.zip.error.destination');
303304
}
304305

305306
return $zip->close();
306307
} else {
307-
$this->flashError('backup.action.zip.error.source');
308308
$this->log(self::LOG_ERROR_PREFIX, "Source file not found: '$source'.");
309+
throw new RuntimeException('backup.action.zip.error.source');
309310
}
310311
} else {
311-
$this->flashError('backup.action.zip.error.extension');
312312
$this->log(self::LOG_ERROR_PREFIX, "Extension 'zip' not found!");
313+
throw new RuntimeException('backup.action.zip.error.extension');
313314
}
314315

315316
return false;
@@ -345,10 +346,10 @@ public function execute(string $cmd, string $workdir = null): array
345346
public function getKimaiVersion(bool $full = false): string
346347
{
347348
if ($full) {
348-
return Constants::SOFTWARE . ' - ' . Constants::VERSION; // . ' ' . Constants::STATUS;
349+
return Constants::SOFTWARE . ' - ' . Constants::VERSION; // . ' ' . Constants::STATUS; TODO
349350
}
350351

351-
return Constants::VERSION; // . ' ' . Constants::STATUS;
352+
return Constants::VERSION; // . ' ' . Constants::STATUS; // TODO
352353
}
353354

354355
public function getExistingBackups(): array

0 commit comments

Comments
 (0)