db-errors plugin for objection
This plugin maps database errors using the db-errors library. By default objection throws whatever the database client throws. The default errors are usually difficult to reason with. This plugin along with the db-errors library try to provide more manageable errors.
Every error class found in require('db-errors')
can also be found in require('objection-db-errors')
. See
db-errors for detailed documentation.
Error handling is something that should not need a plugin. The goal is to eventually merge this into objection core if people find this plugin useful.
Example usage
const Model = ;const DBErrors = ; // Usually you want to map each model class's errors. Easiest way to do this// is to create a common superclass for all your models.Model moduleexports = BaseModel;
const BaseModel = static { return 'Person'; } moduleexports = Person;
const UniqueViolationError = ; { await Person; try // Try to insert a model with an existing primary key. await Person; catch err console; // true }