Enhance error management

This commit is contained in:
Laurent Destailleur 2019-09-04 19:55:15 +02:00
parent 7aaf3ac629
commit 9666200d6b
2 changed files with 29 additions and 11 deletions

View File

@ -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 $inputfile Source file name
* @param string $outputfile Target file name * @param string $outputfile Target file name
* @param string $mode 'gz' or 'bz' or 'zip' * @param string $mode 'gz' or 'bz' or 'zip'
* @param string $errorstring Error string
* @return int <0 if KO, >0 if OK * @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; global $conf;
@ -1916,8 +1918,12 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz")
$zip = new ZipArchive; $zip = new ZipArchive;
if ($zip->open($outputfile, ZipArchive::CREATE) !== true) { if ($zip->open($outputfile, ZipArchive::CREATE) !== true) {
$errormsg="Failed to open file ".$outputfile."\n"; $errorstring="dol_compress_file failure - Failed to open file ".$outputfile."\n";
dol_syslog("dol_compress_file failure - ".$errormsg, LOG_ERR); dol_syslog($errorstring, LOG_ERR);
global $errormsg;
$errormsg = $errorstring;
return -6; return -6;
} }
@ -1961,12 +1967,16 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz")
{ {
global $errormsg; global $errormsg;
$errormsg=$archive->errorInfo(true); $errormsg=$archive->errorInfo(true);
dol_syslog("dol_compress_file failure - ".$errormsg, LOG_ERR);
if ($archive->errorCode() == PCLZIP_ERR_WRITE_OPEN_FAIL) 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; return -4;
} }
$errorstring = "dol_compress_file error archive->errorCode = ".$archive->errorCode()." errormsg=".$errormsg;
dol_syslog("dol_compress_file failure - ".$errormsg, LOG_ERR);
return -3; return -3;
} }
else else
@ -1986,7 +1996,11 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz")
} }
else 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; return -2;
} }
} }
@ -1994,8 +2008,10 @@ function dol_compress_file($inputfile, $outputfile, $mode = "gz")
{ {
global $langs, $errormsg; global $langs, $errormsg;
$langs->load("errors"); $langs->load("errors");
dol_syslog("Failed to open file ".$outputfile, LOG_ERR);
$errormsg=$langs->trans("ErrorFailedToWriteInDir"); $errormsg=$langs->trans("ErrorFailedToWriteInDir");
$errorstring = "Failed to open file ".$outputfile;
dol_syslog($errorstring, LOG_ERR);
return -1; return -1;
} }
} }

View File

@ -409,13 +409,15 @@ class FilesLibTest extends PHPUnit\Framework\TestCase
$count=0; $count=0;
dol_delete_dir_recursive($dirout, $count, 1); 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"; print __METHOD__." result=".$result."\n";
$this->assertGreaterThanOrEqual(1, $result); $this->assertGreaterThanOrEqual(1, $result, "Pb with dol_compress_file ".$errorstring);
$result=dol_uncompress($fileout, $dirout); $result=dol_uncompress($fileout, $dirout);
print __METHOD__." result=".join(',', $result)."\n"; print __METHOD__." result=".join(',', $result)."\n";
$this->assertEquals(0, count($result)); $this->assertEquals(0, count($result), "Pb with dol_uncompress_file");
} }
/** /**