Skip to content
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

Add error as well as exception handling to Mysqli drivers #2704

Merged
merged 1 commit into from
May 2, 2017
Merged

Add error as well as exception handling to Mysqli drivers #2704

merged 1 commit into from
May 2, 2017

Conversation

develancer
Copy link
Contributor

This patch restores the error handler if mysqli_real_connect throws an exception.

@Ocramius
Copy link
Member

Overall, patch looks good, but the try-catch error handler restoring should be tested.

A test would do something like:

$handler = function () { self::fail('Never expected this to be called');}
set_error_handler($handler);

try {
    $doTheThingThatFails();
    self::fail('An exception was supposed to be raised');
} catch (MysqliException $e) {
    // assert on the exception message
}

self::assertSame($handler, restore_error_handler(), 'handler stack was restored');

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Tests needed, as per #2704 (comment)

@develancer
Copy link
Contributor Author

@Ocramius I have added the missing test.

@Ocramius
Copy link
Member

A bunch of failures popped up - see https://travis-ci.org/doctrine/dbal/jobs/225133976#L276-L303

@develancer
Copy link
Contributor Author

@Ocramius Right, sorry about that. It seems to be OK now.

Copy link
Contributor

@phansys phansys left a comment

Choose a reason for hiding this comment

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

Status: Needs work.

if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
try {
if ( ! $this->_conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
throw new MysqliException($this->_conn->connect_error, @$this->_conn->sqlstate ?: 'HY000', $this->_conn->connect_errno);
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use $this->_conn->sqlstate ?? 'HY000' instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could, but it would break compatibility with PHP 5.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're targeting master branch, which already have dropped the support for PHP 5.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, done.

Copy link
Member

Choose a reason for hiding this comment

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

Replacing ?: with ?? in this case doesn't make sense. Why do we suddenly need to ignore missing property sqlstate inside $this->_conn? I for one would like code generate error and not silently continue in such a case.

@@ -28,11 +28,31 @@ protected function setUp()
->getMockForAbstractClass();
}

protected function tearDown()
{
restore_error_handler();
Copy link
Member

Choose a reason for hiding this comment

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

This will cause restore_error_handler() to be called for every test in this class - that is a mistake, since any error handler registered by phpunit may be replaced. Only the actual test overriding the error handler should restore the error handler (by calling restore_error_handler() at the end)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you can see, at the end of the test there is a call to set_error_handler($default_handler), which should essentially restore the error handler to the pre-test state. However, without restore_error_handler in tearDown, it causes 7 other tests to fail from some reason.

I have changed the tearDown to restore the error handler only for this test.

@@ -28,11 +28,33 @@ protected function setUp()
->getMockForAbstractClass();
}

protected function tearDown()
{
if ($this->getName() == "testRestoresErrorHandlerOnException") {
Copy link
Member

Choose a reason for hiding this comment

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

The issue here is that you have to do restore_error_handler() twice in the testRestoresErrorHandlerOnException test :-)

Copy link
Member

Choose a reason for hiding this comment

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

I'll apply this change locally

@Ocramius Ocramius modified the milestones: 2.5.13, 2.6 May 2, 2017
@Ocramius Ocramius merged commit 663cb65 into doctrine:master May 2, 2017
Ocramius added a commit that referenced this pull request May 2, 2017
Ocramius added a commit that referenced this pull request May 2, 2017
@Ocramius
Copy link
Member

Ocramius commented May 2, 2017

@develancer I manually merged this into master: won't be backported to 2.5, since it uses finally, which isn't available in older PHP versions.

@Ocramius Ocramius self-assigned this May 2, 2017
@develancer develancer deleted the better-exception-handling branch May 3, 2017 09:17
@Ocramius Ocramius changed the title Better exception handling in mysqli connect Add error as well as exception handling to Mysqli drivers Jul 22, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants