Skip to content

Commit 33f0c4e

Browse files
authored
Use NotWritableError for ini config (#5404)
Replaced the unused `Zend_Config_Exception` with a more appropriate `NotWritableError` in `IniWriter::write()`. - The old `Zend_Config_Exception` was never caught anywhere in the codebase. - Changed to `NotWritableError` to better reflect file permission and write issues. - Added `@` suppression to the `file_put_contents()` call so that the method can throw `NotWritableError` instead of crashing on PHP warnings.
1 parent ae4e7bf commit 33f0c4e

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

doc/80-Upgrading.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ v2.6 to v2.8 requires to follow the instructions for v2.7 too.
1212
* `service_last_time_unknown`
1313
* `service_last_time_warning`
1414
* `service_last_time_critical`
15+
* The `IniWriter::write()` method now throws `NotWritableError` instead of `Zend_Config_Exception`.
1516

1617
## Upgrading to Icinga Web 2.12.2
1718

library/Icinga/File/Ini/IniWriter.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
namespace Icinga\File\Ini;
55

6+
use Icinga\Application\Config;
67
use Icinga\Application\Logger;
78
use Icinga\Data\ConfigObject;
9+
use Icinga\Exception\NotWritableError;
810
use Icinga\Exception\ProgrammingError;
911
use Icinga\File\Ini\Dom\Directive;
1012
use Icinga\File\Ini\Dom\Document;
1113
use Icinga\File\Ini\Dom\Section;
12-
use Zend_Config_Exception;
13-
use Icinga\Application\Config;
1414

1515
/**
1616
* A INI file adapter that respects the file structure and the comments of already existing ini files
@@ -89,22 +89,21 @@ public function render()
8989
* @param string $filename
9090
* @param bool $exclusiveLock
9191
*
92-
* @throws Zend_Config_Exception
92+
* @throws NotWritableError
9393
*/
9494
public function write($filename = null, $exclusiveLock = false)
9595
{
9696
$filePath = isset($filename) ? $filename : $this->filename;
9797
$setMode = false === file_exists($filePath);
98-
99-
if (file_put_contents($filePath, $this->render(), $exclusiveLock ? LOCK_EX : 0) === false) {
100-
throw new Zend_Config_Exception('Could not write to file "' . $filePath . '"');
98+
if (@file_put_contents($filePath, $this->render(), $exclusiveLock ? LOCK_EX : 0) === false) {
99+
throw new NotWritableError('Could not write to file "' . $filePath . '"');
101100
}
102101

103102
if ($setMode) {
104103
// file was newly created
105104
$mode = $this->fileMode;
106105
if (is_int($this->fileMode) && false === @chmod($filePath, $this->fileMode)) {
107-
throw new Zend_Config_Exception(sprintf('Failed to set file mode "%o" on file "%s"', $mode, $filePath));
106+
throw new NotWritableError(sprintf('Failed to set file mode "%o" on file "%s"', $mode, $filePath));
108107
}
109108
}
110109
}

0 commit comments

Comments
 (0)