New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement our own error handler for db2_prepare #273

Merged
merged 10 commits into from Nov 23, 2017
@@ -9,6 +9,8 @@
namespace Zend\Db\Adapter\Driver\IbmDb2;
use ErrorException;
use Throwable;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\ParameterContainer;
@@ -172,7 +174,14 @@ public function prepare($sql = null)
$sql = $this->sql;
}
$this->resource = db2_prepare($this->db2, $sql);
try {
set_error_handler($this->createErrorHandler());
$this->resource = db2_prepare($this->db2, $sql);
} catch (Throwable $e) {

This comment has been minimized.

@samsonasik

samsonasik Oct 13, 2017

Contributor

should catch Exception for php 5.5/5.6 fallback as well ?

This comment has been minimized.

@akrabat

akrabat Oct 13, 2017

Member

No - in 5.6, nothing is thrown.

throw new Exception\RuntimeException($e->getMessage() . '. ' . db2_stmt_errormsg(), db2_stmt_error(), $e);
} finally {
restore_error_handler();
}
if ($this->resource === false) {
throw new Exception\RuntimeException(db2_stmt_errormsg(), db2_stmt_error());
@@ -238,4 +247,31 @@ public function execute($parameters = null)
$result = $this->driver->createResult($this->resource);
return $result;
}
/**
* Creates and returns a callable error handler that raises exceptions.
*
* Only raises exceptions for errors that are within the error_reporting mask.
*
* @return callable
*/
private function createErrorHandler()
{
/**
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
* @throws ErrorException if error is not within the error_reporting mask.
*/
return function ($errno, $errstr, $errfile, $errline) {
if (! (error_reporting() & $errno)) {
// error_reporting does not include this error
return;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
};
}
}
@@ -11,6 +11,7 @@
use Zend\Db\Adapter\Driver\IbmDb2\IbmDb2;
use Zend\Db\Adapter\Driver\IbmDb2\Statement;
use Zend\Db\Adapter\Exception\RuntimeException;
/**
* @group integration
@@ -63,7 +64,7 @@ public function testGetResource()
$statement = new Statement;
$statement->initialize($db2Resource);
$statement->prepare("SELECT 'foo'");
$statement->prepare("SELECT 'foo' FROM sysibm.sysdummy1");
$resource = $statement->getResource();
$this->assertEquals('DB2 Statement', get_resource_type($resource));
unset($resource, $db2Resource);
@@ -85,6 +86,23 @@ public function testPrepare()
unset($resource, $db2Resource);
}
/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::prepare
* @expectedException RuntimeException

This comment has been minimized.

@webimpress

webimpress Oct 14, 2017

Contributor

I would move it to function use, because right now it's not clear, which line should thrown that exception.

This comment has been minimized.

@akrabat

akrabat Oct 14, 2017

Member

@webimpress I can't - it doesn't work on the AS/400 when I do that. I have no idea why.

This comment has been minimized.

@webimpress

webimpress Oct 14, 2017

Contributor

Hm... so maybe exception is thrown not where you expecting it?

This comment has been minimized.

@akrabat

akrabat Oct 14, 2017

Member

It was a PHP error. I'll have another look when I'm next connected to that computer.

*/
public function testPrepareThrowsAnExceptionOnFailure()
{
$db2Resource = db2_connect(
$this->variables['database'],
$this->variables['username'],
$this->variables['password']
);
$statement = new Statement;
$statement->initialize($db2Resource);
$statement->prepare("SELECT");
unset($resource, $db2Resource);

This comment has been minimized.

@webimpress

webimpress Oct 16, 2017

Contributor

I think this line will be not even hit.

This comment has been minimized.

@akrabat

akrabat Oct 17, 2017

Member

It will if the test fails :)

}
/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::execute
*/
ProTip! Use n and p to navigate between commits in a pull request.