objection.js: Improved (database) errors
I would love to add a cross-db error API but as I have mentioned before it would be best implemented as a separate library. Then it could possibly be moved to knex at some point.
Currently errors thrown from db for example in case of foreign key constraint violation differ from db to db and are usually difficult to reason with. I would love to have something like:
class DbError extends Error { ... }
class ConstraintViolationError extends DbError {
get column() { ... }
get table() { ... }
get constraintName() { ... }
}
class ForeignKeyConstraintViolationError extends ConstraintViolationError {
...
}
// etc.
If someone is looking for an open source project, here’s one that I think would benefit the whole node.js DB scene.
EDIT Check out db-errors library I’m working on. EDIT 2 There’s now a db-errors plugin for objection
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 17
- Comments: 26 (8 by maintainers)
Commits related to this issue
- fix couple of bugs with upsertGraph + composite keys. a fix attempt of #501 — committed to Vincit/objection.js by koskimas 7 years ago
@jtlapp I’m currently refactoring ValidationErrors for the 1.0 release. I’ve changed the error messages to a more human readable format. You probably shouldn’t show the ValidationError messages to the users though.
ValidationErrors will also have a
type
field for distinquishing between model validation errors and other kinds of validation errors.I’ll continue writing db-errors to see how big of a mess it becomes. I’ll start with the most useful errors. What errors would you like to catch? I’m quessing unique constraint violation is one of them, but what else?
I think this can be closed for now. I won’t add db-errors to objection yet. Let’s wait until it matures a bit and even then we should try to get it merged into knex instead. For now, the objection-db-errors plugin can be used.
Validation errors have now been refactored and can be tested by installing the most recent 1.0 RC using
npm install objection@next
.I was able to setup travis to run the tests on multiple versions of mysql and postgres. I’ll add multiple versions of the node db drivers also just to make sure.
@kblomster I also added
DataError
but I’m not able to parse any useful data from those errors (like table or column that failed). The only way to parse that info would be to parse SQL, and we would need a full-blown SQL parser for that. I don’t know how useful that error is. The error message contains the info, but as SQL.@koskimas Once it’s more mature, would you accept a PR implementing support for MSSQL?
The most important thing I need to do before I can consider releasing the db-errors library is a test matrix that runs the test suite with a bunch of driver and db versions. Some of the stuff is parsed from error messages and even though they are documented, I don’t trust that they will stay the same.
Foreign key constraint violation would be nice, I think. Maybe data format violations as well (that is, string too long, integer too big, etc), but that one is somewhat less useful since you probably want to handle that with validators in js anyway.