Task #72254
closed
Throw sql exceptions with sql error number
Added by Carsten Bleicker almost 9 years ago.
Updated almost 9 years ago.
Description
typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php:862
Current:
throw new \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Exception\SqlErrorException($error, 1247602160);
Better:
throw new \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Exception\SqlErrorException($error, 1247602160, $this->databaseHandle->sql_errno());
Passing the sql_errno() to exception is usefull f.e. for API Endpoints having unique constraints.
Currently its not possible to handline Duplicate Key db errors.
With sql_errno() you could do:
try{
// Create Record ...
}catch(SqlErrorException $e){
if($e->getSqlErrorNumber() === 1062){
throw new ThrowableException('Duplicate Entry ...', ....);
}
}
Also errno never appears in log entries of catched exceptions.
- Status changed from New to Under Review
- Status changed from Under Review to Rejected
While I understand your use case the proposed solution violates the interface definition of Exceptions where the third parameter generally is the previous exception. Deviating from this pattern in a single specific circumstance is something we should avoid in the core. Implementing something like this in your application is easy to accomplish as you can catch the SqlErrorException an parse the message of the exception that starts with the error number or you could extend the Typo3DbBackend class and adjust the thrown Exception there.
If you think that this is the wrong decision please write to the mailing list typo3.teams.bugs with issue number and an explanation or open a new ticket and add a relation to this ticket number.
You want me to extract the errnum from the message?
This means, that any sql error must have an equal structure of error message to parse the errno out of it.
Sure that this is proof? I think its not.
There is an error and TYPO3 strips useful informations.
So either we need to
1. build useful exceptions or
2. return the error without stripping informations
what currently happens is:
1. get error
2. stripping infos
3. building string
4. my app needs to recover 3. to get infos of 1.
wired, if you ask me
maybe like this:
throw new \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Exception\SqlErrorException($msq, $errNum);
Or SqlErrorException accepts a 4th param holding the sql error object.
Also available in: Atom
PDF