Skip to content

Commit

Permalink
Merge pull request #1078 from M0rgan01/Improve-zip-error-log
Browse files Browse the repository at this point in the history
[LOGS] Improve zip error logs
  • Loading branch information
nicosomb authored Dec 19, 2024
2 parents a4d1511 + 5d3dd1c commit 29657fb
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions classes/ZipAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,16 @@ public function compress(Backlog $backlog, string $toFile): bool
}

if (!$zip->addFile($file, $archiveFilename)) {
$error = $zip->getStatusString();
// if an error occur, it's more safe to delete the corrupted backup
$zip->close();
(new Filesystem())->remove($toFile);
$this->logger->error($this->translator->trans(
'Error when trying to add %filename% to archive %archive%.',
'Unable to add %filename% to archive %archive%: %error%',
[
'%filename%' => $file,
'%archive%' => $archiveFilename,
'%error%' => $error,
]
));

Expand All @@ -116,9 +118,14 @@ public function compress(Backlog $backlog, string $toFile): bool
}

if (!$zip->close()) {
$error = $zip->getStatusString();

$this->logger->error($this->translator->trans(
'Could not close the Zip file: %toFile% properly. Check you are allowed to write on the disk and there is available space on it.',
['%toFile%' => $toFile]
'Could not close the Zip file %toFile%: %error%',
[
'%toFile%' => $toFile,
'%error%' => $error,
]
));

return false;
Expand Down Expand Up @@ -159,10 +166,15 @@ public function extract(string $from_file, string $to_dir): bool

for ($i = 0; $i < $zip->numFiles; ++$i) {
if (!$zip->extractTo($to_dir, [$zip->getNameIndex($i)])) {
$error = $zip->getStatusString();

$this->logger->error(
$this->translator->trans(
'Could not extract %file% from backup, the destination might not be writable.',
['%file%' => $zip->statIndex($i)['name']]
'Could not extract %file% from archive: %error%',
[
'%file%' => $zip->statIndex($i)['name'],
'%error%' => $error,
]
)
);
$zip->close();
Expand Down Expand Up @@ -193,7 +205,7 @@ public function listContent(string $zipFile): array
try {
$zip = $this->open($zipFile);
} catch (ZipActionException $e) {
$this->logger->error($this->translator->trans('[ERROR] Unable to list archived files'));
$this->logger->error($this->translator->trans('Unable to list archived files'));

return [];
}
Expand Down Expand Up @@ -257,8 +269,10 @@ public function open(string $zipFile, ?int $flags = null): ZipArchive
$flags = 0;
}
if ($zip->open($zipFile, $flags) !== true || empty($zip->filename)) {
$this->logger->error($this->translator->trans('Unable to open zipFile %s', [$zipFile]));
throw new ZipActionException('Unable to open zipFile ' . $zipFile);
$error = $zip->getStatusString();

$this->logger->error($this->translator->trans('Unable to open archive %s: %s', [$zipFile, $error]));
throw new ZipActionException('Unable to open archive ' . $zipFile . ': ' . $error);
}

return $zip;
Expand Down

0 comments on commit 29657fb

Please sign in to comment.