Commit c9883bf0 authored by Carsten  Rose's avatar Carsten Rose
Browse files

B7455 php unit tests fail: catch exception on trying to open a non existing logfile

parent 454d7fb3
......@@ -8,6 +8,8 @@
namespace qfq;
use Complex\Exception;
require_once(__DIR__ . '/../Constants.php');
require_once(__DIR__ . '/../exceptions/UserFormException.php');
require_once(__DIR__ . '/../helper/Support.php');
......@@ -25,9 +27,12 @@ class Logger {
* @param $filename
*
* @param string $mode
* @param bool $recursion
* @throws UserFormException
*/
public static function logMessage($msg, $filename, $mode = FILE_MODE_APPEND) {
public static function logMessage($msg, $filename, $mode = FILE_MODE_APPEND, $recursion = false) {
$handle = false;
if ($filename == '') {
return;
......@@ -35,19 +40,25 @@ class Logger {
$filename = self::relativeToT3Dir($filename);
if (!$handle = fopen($filename, $mode)) {
try {
$handle = fopen($filename, $mode);
} catch (\Exception $e) {
$dummy=1;
}
// If open fails, maybe the directory does not exist. Create it:
HelperFile::mkDirParent($filename);
if($handle===false) {
// Try to open the file a second time.
if (!$handle = fopen($filename, $mode)) {
if($recursion){
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Error: cannot open file',
ERROR_MESSAGE_SUPPORT => "Error - cannot open. File: " . $filename .
" ( CWD: " . getcwd() . ") - " . HelperFile::errorGetLastAsString()]),
ERROR_IO_OPEN);
}
// If open fails, maybe the directory does not exist. Create it:
HelperFile::mkDirParent($filename);
self::logMessage($msg, $filename, $mode, true);
}
if (fwrite($handle, $msg . PHP_EOL) === false) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment