Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Error handling in AbstractWriter::write using Zend\Stdlib\ErrorHandler #2370

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+88 −10
Split
@@ -13,6 +13,7 @@
use Zend\Log\Exception;
use Zend\Log\Filter;
use Zend\Log\Formatter\FormatterInterface as Formatter;
+use Zend\Stdlib\ErrorHandler;
/**
* @category Zend
@@ -41,6 +42,20 @@
* @var Formatter
*/
protected $formatter;
+
+ /**
+ * Use Zend\Stdlib\ErrorHandler to report errors during calls to write
+ *
+ * @var bool
+ */
+ protected $convertWriteErrorsToExceptions = true;
+
+ /**
+ * Error level passed to Zend\Stdlib\ErrorHandler::start for errors reported during calls to write
+ *
+ * @var bool
+ */
+ protected $errorsToExceptionsConversionLevel = E_WARNING;
/**
* Add a filter specific to this writer.
@@ -134,8 +149,32 @@ public function write(array $event)
}
}
- // exception occurs on error
- $this->doWrite($event);
+ $errorHandlerStarted = false;
+
+ if ($this->convertWriteErrorsToExceptions && !ErrorHandler::started()) {
+ ErrorHandler::start($this->errorsToExceptionsConversionLevel);
+ $errorHandlerStarted = true;
+ }
+
+ try {
+ $this->doWrite($event);
+ }
+ catch (\Exception $e)
@b-durand

b-durand Sep 17, 2012

Contributor

Have you seen the Travis build indicates that your commit has an error? The error can be related to a test or coding standards.

Here, it's an CS issue :

tests/ZendTest/Log/Writer/AbstractTest.php (trailing_spaces)
library/Zend/Log/Writer/AbstractWriter.php (trailing_spaces, braces)

} catch (Exception $e) {

You should add import (e.g. use) for the Exception class, and configure your editor to remove trailing spaces auto.

EDIT: ErrorHandler raise an ErrorException instead of Exception, if we have an exception, the cause is not linked to fail on write in a file.

@ghost

ghost Sep 18, 2012

OK thanks for the info - I use Zend Studio 9, Is the formatter shipped with Studio 9 completely up-to-date o?

+ {
+ if ($errorHandlerStarted) {
+ ErrorHandler::stop();
+ $errorHandlerStarted = false;
+ }
+ throw $e;
+ }
+
+ if ($errorHandlerStarted) {
+ $error = ErrorHandler::stop();
+ $errorHandlerStarted = false;
+ if ($error) {
+ throw new Exception\RuntimeException("Unable to write", 0, $error);
+ }
+ }
}
/**
@@ -149,6 +188,16 @@ public function setFormatter(Formatter $formatter)
$this->formatter = $formatter;
return $this;
}
+
+ /**
+ * Set convert write errors to exception flag
+ *
+ * @param bool $ignoreWriteErrors
+ */
+ public function setConvertWriteErrorsToExceptions($convertErrors)
+ {
+ $this->convertWriteErrorsToExceptions = $convertErrors;
+ }
/**
* Perform shutdown activities such as closing open resources
@@ -109,13 +109,7 @@ public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
protected function doWrite(array $event)
{
$line = $this->formatter->format($event) . $this->logSeparator;
-
- ErrorHandler::start(E_WARNING);
- $result = fwrite($this->stream, $line);
- $error = ErrorHandler::stop();
- if (false === $result) {
- throw new Exception\RuntimeException("Unable to write to stream", 0, $error);
- }
+ fwrite($this->stream, $line);
}
/**
@@ -0,0 +1,23 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Log
+ */
+
+namespace ZendTest\Log\TestAsset;
+
+use Zend\Log\Writer\AbstractWriter;
+
+class ErrorGeneratingWriter extends AbstractWriter
+{
+ protected function doWrite(array $event)
+ {
+ $stream = fopen("php://memory", "r");
+ fclose($stream);
+ fwrite($stream, "test");
+ }
+}
@@ -11,6 +11,7 @@
namespace ZendTest\Log\Writer;
use ZendTest\Log\TestAsset\ConcreteWriter;
+use ZendTest\Log\TestAsset\ErrorGeneratingWriter;
use Zend\Log\Formatter\Simple as SimpleFormatter;
use Zend\Log\Filter\Regex as RegexFilter;
@@ -69,4 +70,15 @@ public function testFluentInterface()
$this->assertTrue($instance instanceof ConcreteWriter);
}
-}
+
+ public function testConvertErrorsToException()
+ {
+ $writer = new ErrorGeneratingWriter();
+ $this->setExpectedException('Zend\Log\Exception\RuntimeException');
+ $writer->write(array('message' => 'test'));
+
+ $writer->setConvertWriteErrorsToExceptions(false);
+ $this->setExpectedException('PHPUnit_Framework_Error_Warning');
+ $writer->write(array('message' => 'test'));
+ }
+}