Permalink
Browse files

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

Generalizes the use of Zend\Stdlib\ErrorHandler in all concrete writers
while at the same allowing to turn off that feature by calling
setConvertWriteErrorsToExceptions on any instance (which makes it safe
to use any Writer inside a custom error handler).
  • Loading branch information...
1 parent 2b394bc commit c277fa420f8562a814decee87e74642619b639a3 @franckstauffer franckstauffer committed Sep 16, 2012
@@ -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)
+ {
+ 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'));
+ }
+}

0 comments on commit c277fa4

Please sign in to comment.