Skip to content
Browse files

Improve error reporting on failed streams, refs #85

  • Loading branch information...
1 parent ae31045 commit 0ce44f1526c438ac1c4996762c93e18daad7b4e1 @Seldaek committed Aug 18, 2012
Showing with 18 additions and 2 deletions.
  1. +7 −2 src/Monolog/Handler/StreamHandler.php
  2. +11 −0 tests/Monolog/Handler/StreamHandlerTest.php
View
9 src/Monolog/Handler/StreamHandler.php
@@ -60,10 +60,15 @@ protected function write(array $record)
if (!$this->url) {
throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
}
- $this->stream = @fopen($this->url, 'a');
+ $errorMessage = null;
+ set_error_handler(function ($code, $msg) use (&$errorMessage) {
+ $errorMessage = preg_replace('{^fopen\(.*?\): }', '', $msg);
+ });
+ $this->stream = fopen($this->url, 'a');
+ restore_error_handler();
if (!is_resource($this->stream)) {
$this->stream = null;
- throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened; it may be invalid or not writable.', $this->url));
+ throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$errorMessage, $this->url));
}
}
fwrite($this->stream, (string) $record['formatted']);
View
11 tests/Monolog/Handler/StreamHandlerTest.php
@@ -74,4 +74,15 @@ public function testWriteInvalidResource()
$handler = new StreamHandler('bogus://url');
$handler->handle($this->getRecord());
}
+
+ /**
+ * @expectedException UnexpectedValueException
+ * @covers Monolog\Handler\StreamHandler::__construct
+ * @covers Monolog\Handler\StreamHandler::write
+ */
+ public function testWriteNonExistingResource()
+ {
+ $handler = new StreamHandler('/foo/bar/baz/'.rand(0, 10000));
+ $handler->handle($this->getRecord());
+ }
}

0 comments on commit 0ce44f1

Please sign in to comment.
Something went wrong with that request. Please try again.