From 00c439c6a59db57ca430f4fa87766555c0764ac3 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 21 Aug 2012 11:13:05 -0500 Subject: [PATCH] [zendframework/zf2#2210] Pass ErrorHandler::stop() result as previous exception - Per @mark-mabe - Any place where an exception is throw immediately following an ErrorHandler::stop() call should pass the result of that call as the previous exception. --- src/Protocol/Imap.php | 10 ++++----- src/Protocol/Pop3.php | 18 +++++++-------- src/Storage/Folder/Maildir.php | 6 ++--- src/Storage/Maildir.php | 12 +++++----- src/Storage/Mbox.php | 12 +++++----- src/Storage/Message.php | 4 ++-- src/Storage/Writable/Maildir.php | 38 ++++++++++++++++---------------- 7 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/Protocol/Imap.php b/src/Protocol/Imap.php index 2cb4bc74..23caa519 100644 --- a/src/Protocol/Imap.php +++ b/src/Protocol/Imap.php @@ -78,14 +78,14 @@ public function connect($host, $port = null, $ssl = false) $port = $ssl === 'SSL' ? 993 : 143; } - $errno = 0; - $errstr = ''; ErrorHandler::start(); $this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); - ErrorHandler::stop(); + $error = ErrorHandler::stop(); if (!$this->socket) { - throw new Exception\RuntimeException('cannot connect to host; error = ' . $errstr . - ' (errno = ' . $errno . ' )'); + throw new Exception\RuntimeException(sprintf( + 'cannot connect to host%s', + ($error ? sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : '') + ), 0, $error); } if (!$this->_assumedNextLine('* OK')) { diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index e625fc62..92e41169 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -86,14 +86,14 @@ public function connect($host, $port = null, $ssl = false) $port = $ssl == 'SSL' ? 995 : 110; } - $errno = 0; - $errstr = ''; ErrorHandler::start(); $this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); - ErrorHandler::stop(); + $error = ErrorHandler::stop(); if (!$this->socket) { - throw new Exception\RuntimeException('cannot connect to host; error = ' . $errstr - . ' (errno = ' . $errno . ' )'); + throw new Exception\RuntimeException(sprintf( + 'cannot connect to host%s', + ($error ? sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : '') + ), 0, $error); } $welcome = $this->readResponse(); @@ -128,9 +128,9 @@ public function sendRequest($request) { ErrorHandler::start(); $result = fputs($this->socket, $request . "\r\n"); - ErrorHandler::stop(); + $error = ErrorHandler::stop(); if (!$result) { - throw new Exception\RuntimeException('send failed - connection closed?'); + throw new Exception\RuntimeException('send failed - connection closed?', 0, $error); } } @@ -146,9 +146,9 @@ public function readResponse($multiline = false) { ErrorHandler::start(); $result = fgets($this->socket); - ErrorHandler::stop(); + $error = ErrorHandler::stop(); if (!is_string($result)) { - throw new Exception\RuntimeException('read failed - connection closed?'); + throw new Exception\RuntimeException('read failed - connection closed?', 0, $error); } $result = trim($result); diff --git a/src/Storage/Folder/Maildir.php b/src/Storage/Folder/Maildir.php index d5d96560..7155bd33 100644 --- a/src/Storage/Folder/Maildir.php +++ b/src/Storage/Folder/Maildir.php @@ -89,10 +89,10 @@ protected function _buildFolderTree() $this->rootFolder->INBOX = new Storage\Folder('INBOX', 'INBOX', true); ErrorHandler::start(E_WARNING); - $dh = opendir($this->rootdir); - ErrorHandler::stop(); + $dh = opendir($this->rootdir); + $error = ErrorHandler::stop(); if (!$dh) { - throw new Exception\RuntimeException("can't read folders in maildir"); + throw new Exception\RuntimeException("can't read folders in maildir", 0, $error); } $dirs = array(); diff --git a/src/Storage/Maildir.php b/src/Storage/Maildir.php index efc56bd1..8c569c0c 100644 --- a/src/Storage/Maildir.php +++ b/src/Storage/Maildir.php @@ -267,22 +267,22 @@ protected function _openMaildir($dirname) } ErrorHandler::start(E_WARNING); - $dh = opendir($dirname . '/cur/'); - ErrorHandler::stop(); + $dh = opendir($dirname . '/cur/'); + $error = ErrorHandler::stop(); if (!$dh) { - throw new Exception\RuntimeException('cannot open maildir'); + throw new Exception\RuntimeException('cannot open maildir', 0, $error); } $this->_getMaildirFiles($dh, $dirname . '/cur/'); closedir($dh); ErrorHandler::start(E_WARNING); - $dh = opendir($dirname . '/new/'); - ErrorHandler::stop(); + $dh = opendir($dirname . '/new/'); + $error = ErrorHandler::stop(); if ($dh) { $this->_getMaildirFiles($dh, $dirname . '/new/', array(Mail\Storage::FLAG_RECENT)); closedir($dh); } elseif (file_exists($dirname . '/new/')) { - throw new Exception\RuntimeException('cannot read recent mails in maildir'); + throw new Exception\RuntimeException('cannot read recent mails in maildir', 0, $error); } } diff --git a/src/Storage/Mbox.php b/src/Storage/Mbox.php index 8d802f48..196da7c4 100644 --- a/src/Storage/Mbox.php +++ b/src/Storage/Mbox.php @@ -248,9 +248,9 @@ protected function openMboxFile($filename) ErrorHandler::start(); $this->fh = fopen($filename, 'r'); - ErrorHandler::stop(); + $error = ErrorHandler::stop(); if (!$this->fh) { - throw new Exception\RuntimeException('cannot open mbox file'); + throw new Exception\RuntimeException('cannot open mbox file', 0, $error); } $this->filename = $filename; $this->filemtime = filemtime($this->filename); @@ -258,8 +258,8 @@ protected function openMboxFile($filename) if (!$this->isMboxFile($this->fh, false)) { ErrorHandler::start(E_WARNING); fclose($this->fh); - ErrorHandler::stop(); - throw new Exception\InvalidArgumentException('file is not a valid mbox format'); + $error = ErrorHandler::stop(); + throw new Exception\InvalidArgumentException('file is not a valid mbox format', 0, $error); } $messagePos = array('start' => ftell($this->fh), 'separator' => 0, 'end' => 0); @@ -391,9 +391,9 @@ public function __wakeup() } else { ErrorHandler::start(); $this->fh = fopen($this->filename, 'r'); - ErrorHandler::stop(); + $error = ErrorHandler::stop(); if (!$this->fh) { - throw new Exception\RuntimeException('cannot open mbox file'); + throw new Exception\RuntimeException('cannot open mbox file', 0, $error); } } } diff --git a/src/Storage/Message.php b/src/Storage/Message.php index 849d8652..85a747b6 100644 --- a/src/Storage/Message.php +++ b/src/Storage/Message.php @@ -40,9 +40,9 @@ public function __construct(array $params) if (!is_resource($params['file'])) { ErrorHandler::start(); $params['raw'] = file_get_contents($params['file']); - ErrorHandler::stop(); + $error = ErrorHandler::stop(); if ($params['raw'] === false) { - throw new Exception\RuntimeException('could not open file'); + throw new Exception\RuntimeException('could not open file', 0, $error); } } else { $params['raw'] = stream_get_contents($params['file']); diff --git a/src/Storage/Writable/Maildir.php b/src/Storage/Writable/Maildir.php index d962c205..a4b4c373 100644 --- a/src/Storage/Writable/Maildir.php +++ b/src/Storage/Writable/Maildir.php @@ -49,28 +49,28 @@ public static function initMaildir($dir) } } else { ErrorHandler::start(); - $test = mkdir($dir); - ErrorHandler::stop(); + $test = mkdir($dir); + $error = ErrorHandler::stop(); if (!$test) { $dir = dirname($dir); if (!file_exists($dir)) { - throw new StorageException\InvalidArgumentException("parent $dir not found"); + throw new StorageException\InvalidArgumentException("parent $dir not found", 0, $error); } elseif (!is_dir($dir)) { - throw new StorageException\InvalidArgumentException("parent $dir not a directory"); + throw new StorageException\InvalidArgumentException("parent $dir not a directory", 0, $error); } else { - throw new StorageException\RuntimeException('cannot create maildir'); + throw new StorageException\RuntimeException('cannot create maildir', 0, $error); } } } foreach (array('cur', 'tmp', 'new') as $subdir) { ErrorHandler::start(); - $test = mkdir($dir . DIRECTORY_SEPARATOR . $subdir); - ErrorHandler::stop(); + $test = mkdir($dir . DIRECTORY_SEPARATOR . $subdir); + $error = ErrorHandler::stop(); if (!$test) { // ignore if dir exists (i.e. was already valid maildir or two processes try to create one) if (!file_exists($dir . DIRECTORY_SEPARATOR . $subdir)) { - throw new StorageException\RuntimeException('could not create subdir ' . $subdir); + throw new StorageException\RuntimeException('could not create subdir ' . $subdir, 0, $error); } } } @@ -163,8 +163,8 @@ public function createFolder($name, $parentFolder = null) ErrorHandler::start(); if (!mkdir($fulldir) || !mkdir($fulldir . DIRECTORY_SEPARATOR . 'cur')) { - ErrorHandler::stop(); - throw new StorageException\RuntimeException('error while creating new folder, may be created incompletely'); + $error = ErrorHandler::stop(); + throw new StorageException\RuntimeException('error while creating new folder, may be created incompletely', 0, $error); } ErrorHandler::stop(); @@ -649,10 +649,10 @@ public function setFlags($id, $flags) $new_filename = dirname(dirname($filedata['filename'])) . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . "$filedata[uniq]$info"; ErrorHandler::start(); - $test = rename($filedata['filename'], $new_filename); - ErrorHandler::stop(); + $test = rename($filedata['filename'], $new_filename); + $error = ErrorHandler::stop(); if (!$test) { - throw new StorageException\RuntimeException('cannot rename file'); + throw new StorageException\RuntimeException('cannot rename file', 0, $error); } $filedata['flags'] = $flags; @@ -677,10 +677,10 @@ public function removeMessage($id) } ErrorHandler::start(); - $test = unlink($filename); - ErrorHandler::stop(); + $test = unlink($filename); + $error = ErrorHandler::stop(); if (!$test) { - throw new StorageException\RuntimeException('cannot remove message'); + throw new StorageException\RuntimeException('cannot remove message', 0, $error); } unset($this->files[$id - 1]); // remove the gap @@ -717,10 +717,10 @@ public function getQuota($fromStorage = false) { if ($fromStorage) { ErrorHandler::start(E_WARNING); - $fh = fopen($this->rootdir . 'maildirsize', 'r'); - ErrorHandler::stop(); + $fh = fopen($this->rootdir . 'maildirsize', 'r'); + $error = ErrorHandler::stop(); if (!$fh) { - throw new StorageException\RuntimeException('cannot open maildirsize'); + throw new StorageException\RuntimeException('cannot open maildirsize', 0, $error); } $definition = fgets($fh); fclose($fh);