From 9666200d6b94a6b959d5c0f287f9fafd877404bc Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 4 Sep 2019 19:55:15 +0200 Subject: [PATCH] Enhance error management --- htdocs/core/lib/files.lib.php | 32 ++++++++++++++++++++++++-------- test/phpunit/FilesLibTest.php | 8 +++++--- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index 83d7f6f7614..a5f97b3fa3a 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -1884,14 +1884,16 @@ function dol_convert_file($fileinput, $ext = 'png', $fileoutput = '', $page = '' /** - * Compress a file + * Compress a file. + * An error string may be returned into parameters. * * @param string $inputfile Source file name * @param string $outputfile Target file name * @param string $mode 'gz' or 'bz' or 'zip' + * @param string $errorstring Error string * @return int <0 if KO, >0 if OK */ -function dol_compress_file($inputfile, $outputfile, $mode = "gz") +function dol_compress_file($inputfile, $outputfile, $mode = "gz", &$errorstring = null) { global $conf; @@ -1916,8 +1918,12 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz") $zip = new ZipArchive; if ($zip->open($outputfile, ZipArchive::CREATE) !== true) { - $errormsg="Failed to open file ".$outputfile."\n"; - dol_syslog("dol_compress_file failure - ".$errormsg, LOG_ERR); + $errorstring="dol_compress_file failure - Failed to open file ".$outputfile."\n"; + dol_syslog($errorstring, LOG_ERR); + + global $errormsg; + $errormsg = $errorstring; + return -6; } @@ -1961,12 +1967,16 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz") { global $errormsg; $errormsg=$archive->errorInfo(true); - dol_syslog("dol_compress_file failure - ".$errormsg, LOG_ERR); + if ($archive->errorCode() == PCLZIP_ERR_WRITE_OPEN_FAIL) { - dol_syslog("dol_compress_file error PCLZIP_ERR_WRITE_OPEN_FAIL", LOG_ERR); + $errorstring = "PCLZIP_ERR_WRITE_OPEN_FAIL"; + dol_syslog("dol_compress_file error - archive->errorCode() = PCLZIP_ERR_WRITE_OPEN_FAIL", LOG_ERR); return -4; } + + $errorstring = "dol_compress_file error archive->errorCode = ".$archive->errorCode()." errormsg=".$errormsg; + dol_syslog("dol_compress_file failure - ".$errormsg, LOG_ERR); return -3; } else @@ -1986,7 +1996,11 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz") } else { - dol_syslog("Try to zip with format ".$mode." with no handler for this format", LOG_ERR); + $errorstring = "Try to zip with format ".$mode." with no handler for this format"; + dol_syslog($errorstring, LOG_ERR); + + global $errormsg; + $errormsg = $errorstring; return -2; } } @@ -1994,8 +2008,10 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz") { global $langs, $errormsg; $langs->load("errors"); - dol_syslog("Failed to open file ".$outputfile, LOG_ERR); $errormsg=$langs->trans("ErrorFailedToWriteInDir"); + + $errorstring = "Failed to open file ".$outputfile; + dol_syslog($errorstring, LOG_ERR); return -1; } } diff --git a/test/phpunit/FilesLibTest.php b/test/phpunit/FilesLibTest.php index cbe6ad948d8..f52b7d21304 100644 --- a/test/phpunit/FilesLibTest.php +++ b/test/phpunit/FilesLibTest.php @@ -409,13 +409,15 @@ class FilesLibTest extends PHPUnit\Framework\TestCase $count=0; dol_delete_dir_recursive($dirout, $count, 1); - $result=dol_compress_file($filein, $fileout, $format); + $errorstring = ''; + + $result=dol_compress_file($filein, $fileout, $format, $errorstring); print __METHOD__." result=".$result."\n"; - $this->assertGreaterThanOrEqual(1, $result); + $this->assertGreaterThanOrEqual(1, $result, "Pb with dol_compress_file ".$errorstring); $result=dol_uncompress($fileout, $dirout); print __METHOD__." result=".join(',', $result)."\n"; - $this->assertEquals(0, count($result)); + $this->assertEquals(0, count($result), "Pb with dol_uncompress_file"); } /**