More specific subclasses for ActiveRecord::StatementInvalid

Raising ActiveRecord::StatementInvalid as the single exception for
anything that might go wrong is rather unspecific. There are
differences between exceptions, some signify plain programming errors,
others are caused by potentially recoverable faults. Specifically among
the later are violations of uniqueness constraints.

Differentiating among the original causes in application or plugin code
is rather nasty and potentially brittle as most developers only care
about one or two DBMS. I suggest to push this functionality into the
connection adapters and have them at least handle uniqueness violations
specially by raising RecordNotUnique, subclassed from StatementInvalid,
with a suitable hint as to which constraint has been violated.

Are there any general concerns against adding this functionality?
Otherwise, I'll start work on a patch.

Michael

Man, I'd *love* that. Specially considering the possible race
conditions of validates_uniqueness_of.

Raising ActiveRecord::StatementInvalid as the single exception for
anything that might go wrong is rather unspecific. There are
differences between exceptions, some signify plain programming errors,
others are caused by potentially recoverable faults. Specifically among
the later are violations of uniqueness constraints.

Differentiating among the original causes in application or plugin code
is rather nasty and potentially brittle as most developers only care
about one or two DBMS. I suggest to push this functionality into the
connection adapters and have them at least handle uniqueness violations
specially by raising RecordNotUnique, subclassed from StatementInvalid,
with a suitable hint as to which constraint has been violated.

Are there any general concerns against adding this functionality?
Otherwise, I'll start work on a patch.

I'd love this, it's one of the things I really liked about spring from
my java days. rescuing ForeignKeyContraintViolation or
DeadlockLoserException etc would be great.

http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/dao/DataAccessException.html

Their approach is quite nice with a parallel set of classes for the translation:

public class MySQLErrorCodesTranslator extends
SQLErrorCodeSQLExceptionTranslator {
...

Definitely have a go and see what you can come up with.

[...]

> Are there any general concerns against adding this functionality?
> Otherwise, I'll start work on a patch.

I'd love this, it's one of the things I really liked about spring
from my java days. rescuing ForeignKeyContraintViolation or
DeadlockLoserException etc would be great.

There you go

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2419

For the time being, only violated uniqueness constraints and invalid
foreign keys are handled specially. I don't know how to detect or
test(!) things like DeadlockLoserException. Anyone, please fill in any
blanks you perceive.

Michael

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2419

Are there any further suggestions and opinions regarding this patch? For
my purposes, it does all I need, right now. A major improvement would
be enabled if the pg gem (PostgreSQL driver) passed on error codes --
is there any chance of this happening?

Michael