ZF-3684: Exception error codes are NULL in Zend_Db

Description


const ERR_ALREADY_EXISTS = NULL;
const ERR_CANT_MAP = NULL;
const ERR_CONSTRAINT = NULL;
const ERR_DISCONNECTED = NULL;
const ERR_MISMATCH = NULL;
const ERR_NO_PERM = NULL;
const ERR_NONE = '00000';
const ERR_NOT_FOUND = NULL;
const ERR_NOT_IMPLEMENTED = NULL;
const ERR_SYNTAX = NULL;
const ERR_TRUNCATED = NULL;

Why are all those constants null? Parsing the error message is what this leads to and that is a pretty expensive operation. E.g. I rely on constraints set in the DB (table) on some insert operations and would like to disregard some "errors" returned. Impossible if the error code is NULL.

At least you could pass through the real error codes which the designated backend provides and allow people to match based on that. I am using Zend_Db (with PDO mysql).

I remember I had an issue open about a year or maybe 1.5 years ago describing exactly the same issues. I thought it had been fixed before.

It's only an improvement, but also a major showstopper for me.

Comments

Just adding the relation.

Hey Ralph,

can you comment what the hold up is?

I'd love to help if I know how. Otherwise the fix seems trivial, e.g.:


const ERR_ALREADY_EXISTS  = "ZDx00";
const ERR_CANT_MAP        = "ZDx01";
const ERR_CONSTRAINT      = "ZDx02";
const ERR_DISCONNECTED    = "ZDx03";
const ERR_MISMATCH        = "ZDx04";
const ERR_NO_PERM         = "ZDx05";
const ERR_NOT_FOUND       = "ZDx06";
const ERR_NOT_IMPLEMENTED = "ZDx07";
const ERR_SYNTAX          = "ZDx08";
const ERR_TRUNCATED       = "ZDx09";

Thanks!

The constants are null because it seems they were generated from the constants defined by the PHP PDO extension in source file php-src/ext/pdo/pdo_dbh.c. The constants were not defined as enums like the other PDO constants so their values were all set to null when defined at the PHP level.

Since they're all NULL, and I don't see them being referenced anywhere in the C source except for where they are defined (as null), these can probably be safely removed from Zend_Db. They appear to serve no purpose since it doesn't appear that PDO is using these constants, and if it were, you couldn't differentiate between them anyway since they are all NULL. Comments?

Patch is attached.

Patch applied to trunk in r23404, merged to 1.11 release branch in r23405

Thanks, Marc! Appreciate it!

Hi,

the ERR constants are gone?

fgrep -r 'const ERR_' trunk/library/Zend/*
trunk/library/Zend/Captcha/ReCaptcha.php:    const ERR_CAPTCHA   = 'errCaptcha';
trunk/library/Zend/Captcha/.svn/text-base/ReCaptcha.php.svn-base:    const ERR_CAPTCHA   = 'errCaptcha';
trunk/library/Zend/Db.php:    const ERR_NONE = '00000';
till@till-laptop:~/workspaces/imagineeasy_v5$ 

Isn't that a BC break?

Till, is there a real use case where you find this has caused an actual BC break?

Per my earlier comments - they're all defined as NULL, just like in the underlying PDO driver. Therefore these constants cannot be used to differentiate between error conditions... they appear to serve no purpose at all either at the PDO driver level or at the Zend_Db level. I can't see how anyone is actually using these where their removal would cause a BC break.

This patch went in a number of versions ago and no one has raised any issues about this.

Hey Marc,

for some some reason, I don't see the constants at all though? They are not defined -- not even null.

That's my issue. I used them in my code, and now it fatals because the error constant is not there anymore.

My real use-case for 'values' of these constants was originally that parsing an error message is not very feasible. If I wanted to use PDO, I'd use it, but I use Zend_Db instead (and MySQLi). How do I tell errors apart? But using strstr, strpos or substr on the exception's message?

Does that make sense?

Till

I also mis-understood your original comment -- even if they are null, you cannot just remove them.

That's a clear BC break. Cause now they are not there and code like this will fatal:


if (Zend_Db::ERR_ALREADY_EXISTS) {

}

Hi Till, if you were using the removed ERR_ constants, your code was already (silently) failing or otherwise broken because these constants are all defined with the same value.

Yes, removing causes a BC break but it also fixes a 'bug' as these constants are NOT able to be used to differentiate error conditions and it is clearly misleading.

The ideal solution would be to assign values to these constants in Zend/Db.php as per the original bug description, however this won't work because of the underlying PDO extension defining these as NULL. Removing the constants alerts you to the fact that these constants should not be used as they won't behave as you expect.

Doesn't matter, it's a BC break. ZF doesn't do them in minor release for this just this reason: you break working code.

Reverted removal of constants. BC breaks are not allowed in ZF1 unless for a critical or security issue where approval is agreed in advance with Zend.

I am additionally marking the issue as Won't Fix as the constants, while NULL, are nonetheless accurate for PDO as weird as it seems. Use at your own risk ;).