Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Protocol\AbstractProtocol: verbose errors #116

Merged
merged 2 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Protocol/AbstractProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ protected function _connect($remote)
$errorStr = '';

// open connection
$this->socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
set_error_handler(
function ($error, $message = '') {
throw new Exception\RuntimeException(sprintf('Could not open socket: %s', $message), $error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't such code faulty that error handler is not restored when exception is thrown?

point being that throwing exception not necessarily terminate the program, it may resume working with this error handler being still set.

},
E_WARNING
);
$this->socket = stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
restore_error_handler();

if ($this->socket === false) {
if ($errorNum == 0) {
Expand Down
9 changes: 9 additions & 0 deletions test/Protocol/SmtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ public function testDisconnectResetsAuthFlag()
$this->connection->disconnect();
$this->assertFalse($this->connection->getAuth());
}

public function testConnectHasVerboseErrors()
{
$smtp = new TestAsset\ErroneousSmtp();

$this->setExpectedExceptionRegExp('Zend\Mail\Protocol\Exception\RuntimeException', '/nonexistentremote/');

$smtp->connect('nonexistentremote');
}
}
23 changes: 23 additions & 0 deletions test/Protocol/TestAsset/ErroneousSmtp.php
Original file line number Diff line number Diff line change
@@ -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-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Mail\Protocol\TestAsset;

use Zend\Mail\Protocol\AbstractProtocol;

/**
* Expose AbstractProtocol behaviour
*/
final class ErroneousSmtp extends AbstractProtocol
{
public function connect($customRemote = null)
{
return $this->_connect($customRemote);
}
}