Skip to content

Commit

Permalink
Use exception chaining
Browse files Browse the repository at this point in the history
Refs #9824
  • Loading branch information
chinpei215 committed Oct 7, 2017
1 parent 45eeb56 commit 0c34205
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Cache/Cache.php
Expand Up @@ -182,7 +182,7 @@ protected static function _buildEngine($name)

if ($config['fallback'] === $name) {
throw new InvalidArgumentException(
sprintf('"%s" cache configuration cannot fallback to itself.', $name)
sprintf('"%s" cache configuration cannot fallback to itself.', $name), null, $e
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Component/PaginatorComponent.php
Expand Up @@ -205,7 +205,7 @@ public function paginate($object, array $settings = [])
} catch (PageOutOfBoundsException $e) {
$this->_setPagingParams();

throw new NotFoundException();
throw new NotFoundException(null, null, $e);
}

return $results;
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Connection.php
Expand Up @@ -224,7 +224,7 @@ public function connect()
try {
return $this->_driver->connect();
} catch (\Exception $e) {
throw new MissingConnectionException(['reason' => $e->getMessage()]);
throw new MissingConnectionException(['reason' => $e->getMessage()], null, $e);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Mailer/Transport/SmtpTransport.php
Expand Up @@ -228,12 +228,12 @@ protected function _connect()
}
} catch (SocketException $e) {
if ($config['tls']) {
throw new SocketException('SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.');
throw new SocketException('SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.', null, $e);
}
try {
$this->_smtpSend("HELO {$host}", '250');
} catch (SocketException $e2) {
throw new SocketException('SMTP server did not accept the connection.');
throw new SocketException('SMTP server did not accept the connection.', null, $e2);
}
}
}
Expand All @@ -252,12 +252,12 @@ protected function _auth()
try {
$this->_smtpSend(base64_encode($this->_config['username']), '334');
} catch (SocketException $e) {
throw new SocketException('SMTP server did not accept the username.');
throw new SocketException('SMTP server did not accept the username.', null, $e);
}
try {
$this->_smtpSend(base64_encode($this->_config['password']), '235');
} catch (SocketException $e) {
throw new SocketException('SMTP server did not accept the password.');
throw new SocketException('SMTP server did not accept the password.', null, $e);
}
} elseif ($replyCode === '504') {
throw new SocketException('SMTP authentication method not allowed, check if SMTP server requires TLS.');
Expand Down
2 changes: 1 addition & 1 deletion src/Network/Socket.php
Expand Up @@ -435,7 +435,7 @@ public function enableCrypto($type, $clientOrServer = 'client', $enable = true)
$enableCryptoResult = stream_socket_enable_crypto($this->connection, $enable, $this->_encryptMethods[$type . '_' . $clientOrServer]);
} catch (Exception $e) {
$this->setLastError(null, $e->getMessage());
throw new SocketException($e->getMessage());
throw new SocketException($e->getMessage(), null, $e);
}
if ($enableCryptoResult === true) {
$this->encrypted = $enable;
Expand Down
8 changes: 4 additions & 4 deletions src/TestSuite/Fixture/FixtureManager.php
Expand Up @@ -303,7 +303,7 @@ public function load($test)
get_class($test),
$e->getMessage()
);
throw new Exception($msg);
throw new Exception($msg, null, $e);
}
}
}
Expand All @@ -326,7 +326,7 @@ public function load($test)
get_class($test),
$e->getMessage()
);
throw new Exception($msg);
throw new Exception($msg, null, $e);
}
}
};
Expand All @@ -344,7 +344,7 @@ public function load($test)
get_class($test),
$e->getMessage()
);
throw new Exception($msg);
throw new Exception($msg, null, $e);
}
}
};
Expand All @@ -355,7 +355,7 @@ public function load($test)
get_class($test),
$e->getMessage()
);
throw new Exception($msg);
throw new Exception($msg, null, $e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/View/Cell.php
Expand Up @@ -215,7 +215,7 @@ public function render($template = null)
try {
return $this->View->render($template);
} catch (MissingTemplateException $e) {
throw new MissingCellViewException(['file' => $template, 'name' => $name]);
throw new MissingCellViewException(['file' => $template, 'name' => $name], null, $e);
}
};

Expand Down
13 changes: 9 additions & 4 deletions tests/TestCase/Cache/CacheTest.php
Expand Up @@ -106,20 +106,25 @@ public function testCacheEngineFallbackToSelf()
{
$filename = tempnam(TMP, 'tmp_');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('cannot fallback to itself');

Cache::setConfig('tests', [
'engine' => 'File',
'path' => $filename,
'prefix' => 'test_',
'fallback' => 'tests'
]);

Cache::engine('tests');
$e = null;
try {
Cache::engine('tests');
} catch (InvalidArgumentException $e) {
}

Cache::drop('tests');
unlink($filename);

$this->assertNotNull($e);
$this->assertStringEndsWith('cannot fallback to itself.', $e->getMessage());
$this->assertInstanceOf('RunTimeException', $e->getPrevious());
}

/**
Expand Down
35 changes: 23 additions & 12 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Controller\Controller;
use Cake\Datasource\ConnectionManager;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\PageOutOfBoundsException;
use Cake\Datasource\Paginator;
use Cake\Http\ServerRequest;
use Cake\Network\Exception\NotFoundException;
Expand Down Expand Up @@ -720,16 +721,21 @@ public function testOutOfRangePageNumberGetsClamped()
$this->request->query['page'] = 3000;

$table = TableRegistry::get('PaginatorPosts');

$e = null;
try {
$this->Paginator->paginate($table);
$this->fail('No exception raised');
} catch (NotFoundException $e) {
$this->assertEquals(
1,
$this->request->params['paging']['PaginatorPosts']['page'],
'Page number should not be 0'
);
}

$this->assertEquals(
1,
$this->request->params['paging']['PaginatorPosts']['page'],
'Page number should not be 0'
);

$this->assertNotNull($e);
$this->assertInstanceOf(PageOutOfBoundsException::class, $e->getPrevious());
}

/**
Expand All @@ -744,16 +750,21 @@ public function testOutOfRangePageNumberStillProvidesPageCount()
$this->request->query['page'] = 4;

$table = TableRegistry::get('PaginatorPosts');

$e = null;
try {
$this->Paginator->paginate($table);
$this->fail('No exception raised');
} catch (NotFoundException $e) {
$this->assertEquals(
3,
$this->request->params['paging']['PaginatorPosts']['pageCount'],
'Page count number should not be 0'
);
}

$this->assertEquals(
3,
$this->request->params['paging']['PaginatorPosts']['pageCount'],
'Page count number should not be 0'
);

$this->assertNotNull($e);
$this->assertInstanceOf(PageOutOfBoundsException::class, $e->getPrevious());
}

/**
Expand Down
13 changes: 11 additions & 2 deletions tests/TestCase/Database/ConnectionTest.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\Database\Exception\MissingConnectionException;
use Cake\Database\Exception\NestedTransactionRollbackException;
use Cake\Database\Log\LoggingStatement;
use Cake\Database\Log\QueryLogger;
Expand Down Expand Up @@ -161,15 +162,23 @@ public function testDriverOptionClassNameSupport()
/**
* Tests that connecting with invalid credentials or database name throws an exception
*
* @expectedException \Cake\Database\Exception\MissingConnectionException
* @return void
*/
public function testWrongCredentials()
{
$config = ConnectionManager::getConfig('test');
$this->skipIf(isset($config['url']), 'Datasource has dsn, skipping.');
$connection = new Connection(['database' => '/dev/nonexistent'] + ConnectionManager::getConfig('test'));
$connection->connect();

$e = null;
try {
$connection->connect();
} catch (MissingConnectionException $e) {
}

$this->assertNotNull($e);
$this->assertStringStartsWith('Connection to database could not be established:', $e->getMessage());
$this->assertInstanceOf('PDOException', $e->getPrevious());
}

/**
Expand Down
57 changes: 45 additions & 12 deletions tests/TestCase/Mailer/Transport/SmtpTransportTest.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Mailer\Email;
use Cake\Mailer\Transport\SmtpTransport;
use Cake\Network\Exception\SocketException;
use Cake\Network\Socket;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -124,8 +125,6 @@ public function testConnectEhloTls()
/**
* testConnectEhloTlsOnNonTlsServer method
*
* @expectedException \Cake\Network\Exception\SocketException
* @expectedExceptionMessage SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.
* @return void
*/
public function testConnectEhloTlsOnNonTlsServer()
Expand All @@ -138,7 +137,17 @@ public function testConnectEhloTlsOnNonTlsServer()
$this->socket->expects($this->at(4))->method('write')->with("STARTTLS\r\n");
$this->socket->expects($this->at(5))->method('read')
->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
$this->SmtpTransport->connect();

$e = null;
try {
$this->SmtpTransport->connect();
} catch (SocketException $e) {
}

$this->assertNotNull($e);
$this->assertEquals('SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.', $e->getMessage());
$this->assertInstanceOf(SocketException::class, $e->getPrevious());
$this->assertContains('500 5.3.3 Unrecognized command', $e->getPrevious()->getMessage());
}

/**
Expand Down Expand Up @@ -180,8 +189,6 @@ public function testConnectHelo()
/**
* testConnectFail method
*
* @expectedException \Cake\Network\Exception\SocketException
* @expectedExceptionMessage SMTP server did not accept the connection.
* @return void
*/
public function testConnectFail()
Expand All @@ -192,7 +199,17 @@ public function testConnectFail()
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("HELO localhost\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->SmtpTransport->connect();

$e = null;
try {
$this->SmtpTransport->connect();
} catch (SocketException $e) {
}

$this->assertNotNull($e);
$this->assertEquals('SMTP server did not accept the connection.', $e->getMessage());
$this->assertInstanceOf(SocketException::class, $e->getPrevious());
$this->assertContains('200 Not Accepted', $e->getPrevious()->getMessage());
}

/**
Expand Down Expand Up @@ -263,8 +280,6 @@ public function testAuthBadSequence()
/**
* testAuthBadUsername method
*
* @expectedException \Cake\Network\Exception\SocketException
* @expectedExceptionMessage SMTP server did not accept the username.
* @return void
*/
public function testAuthBadUsername()
Expand All @@ -275,14 +290,22 @@ public function testAuthBadUsername()
$this->socket->expects($this->at(3))->method('read')
->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
$this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
$this->SmtpTransport->auth();

$e = null;
try {
$this->SmtpTransport->auth();
} catch (SocketException $e) {
}

$this->assertNotNull($e);
$this->assertEquals('SMTP server did not accept the username.', $e->getMessage());
$this->assertInstanceOf(SocketException::class, $e->getPrevious());
$this->assertContains('535 5.7.8 Authentication failed', $e->getPrevious()->getMessage());
}

/**
* testAuthBadPassword method
*
* @expectedException \Cake\Network\Exception\SocketException
* @expectedExceptionMessage SMTP server did not accept the password.
* @return void
*/
public function testAuthBadPassword()
Expand All @@ -294,7 +317,17 @@ public function testAuthBadPassword()
$this->socket->expects($this->at(4))->method('write')->with("c3Rvcnk=\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
$this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
$this->SmtpTransport->auth();

$e = null;
try {
$this->SmtpTransport->auth();
} catch (SocketException $e) {
}

$this->assertNotNull($e);
$this->assertEquals('SMTP server did not accept the password.', $e->getMessage());
$this->assertInstanceOf(SocketException::class, $e->getPrevious());
$this->assertContains('535 5.7.8 Authentication failed', $e->getPrevious()->getMessage());
}

/**
Expand Down
11 changes: 9 additions & 2 deletions tests/TestCase/Network/SocketTest.php
Expand Up @@ -277,7 +277,6 @@ public function testEnableCryptoSocketExceptionNoSsl()
/**
* testEnableCryptoSocketExceptionNoTls
*
* @expectedException \Cake\Network\Exception\SocketException
* @return void
*/
public function testEnableCryptoSocketExceptionNoTls()
Expand All @@ -287,7 +286,15 @@ public function testEnableCryptoSocketExceptionNoTls()
// testing exception on no ssl socket server for ssl and tls methods
$this->Socket = new Socket($configNoSslOrTls);
$this->Socket->connect();
$this->Socket->enableCrypto('tls', 'client');

$e = null;
try {
$this->Socket->enableCrypto('tls', 'client');
} catch (SocketException $e) {
}

$this->assertNotNull($e);
$this->assertInstanceOf('Exception', $e->getPrevious());
}

/**
Expand Down

0 comments on commit 0c34205

Please sign in to comment.