vellotis / knex-generic-errors

General errors capability for the awesome Knex (http://knexjs.org), multi RDBMS query builder.
MIT License
3 stars 3 forks source link

More fine grained errors? #2

Open fcarreiro opened 7 years ago

fcarreiro commented 7 years ago

Hi all! I got here looking for a way to uniformly handle Bookshelf (and knex) uniqueness and other constraints errors. It is something that knex is certainly missing and I like were this lib is going.

My question/comment/suggestion is: is there a plan to be more fine-grained in the handling of errors? For example, as they do in Sequelize: http://docs.sequelizejs.com/en/v3/api/errors/

Cheers!

UPDATE: I realize now that Sequelize validations are at the model level, not the DB level, if I understood correctly (more like what can be acchieved with Joi). In any case, I hope what I am referring to is clear: to uniformly parse errors triggered by uniqueness and other constraints in the DB.

vellotis commented 7 years ago

Hi @fcarreiro

Actually my initial intention was to have similar exceptions as in Sequelize. But I couldn't "squeeze" all these errors out for my needed MySQL. The only thing that would be meaningful to have general Errors classes that all dialect handlers utilize.

The code can be improved to determine by the error message if it was a ForeignKeyError etc.

fcarreiro commented 7 years ago

It took me a while but I found how ActiveRecord does it. Maybe it's a good place to start?

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L658

vellotis commented 7 years ago

Its a good find! I will take a look

fcarreiro commented 7 years ago

Great :) However, I think ActiveRecord does most checks (I mean, validations) manually to avoid relying on the DB. If I'm not mistaken, it does extra queries to check for uniqueness and does type-checks before inserting/updating :/