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
Next

Implement our own error handler for db2_prepare

db2_prepare() may issue a [warning][1] in addition to returning false.
If you have an error handler set (e.g. in the Expressive pipeline) then
the message created is "db2_prepare(): Statement Prepare Failed" which
is not helpful.

Therefore, we create our own error handler and trap this ourselves so
that we can throw an ErrorException with more information from
db2_stmt_errormsg().

[1]: https://github.com/php/pecl-database-ibm_db2/blob/df913feb4dff56366ac4656b0bb6b39200794bde/ibm_db2.c#L4400
  • Loading branch information...
akrabat committed Oct 13, 2017
commit 02313d515ef9781f6d80f94af482ec10e29dd4b3
@@ -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() : callable

This comment has been minimized.

@samsonasik

samsonasik Oct 13, 2017

Contributor

zend-db still use php 5.5/5.6 so the return type won't work

{
/**
* @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 (int $errno, string $errstr, string $errfile, int $errline) : void {

This comment has been minimized.

@samsonasik

samsonasik Oct 13, 2017

Contributor

remove return type as well

This comment has been minimized.

@samsonasik

samsonasik Oct 13, 2017

Contributor

also, int and string type hint not supported in php 5.5/5.6

if (! (error_reporting() & $errno)) {
// error_reporting does not include this error
return;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
};
}
}
ProTip! Use n and p to navigate between commits in a pull request.