objection.js: Distinguish errors caused by model validation from other validation errors

At the moment, Objection uses modelClass.createValidationError() for a lot of different errors, among them the ones caused by failed AjvValidator validations.

Those validation errors contain a data property with an object listing propertyName: [...errors], where errors is a list of validation errors for the given property.

The other validation errors, e.g. caused when using a faulty eager() statement, also contain properties, but those can mean very different things, making handling of such errors currently difficult:

modelClass.createValidationError({
  eager: 'eager expression not allowed'
})
throw modelClass.createValidationError({
  message: 'Invalid relation expression "' + expr + '"',
  cause: err.message
});
throw modelClass.createValidationError({
  cyclic: 'the object graph contains cyclic references'
});
throw modelClass.createValidationError({
  notModel: 'not a model'
});
throw modelClass.createValidationError({
  allowedRelations: 'trying to insert an unallowed relation'
});

etc.

I think it would be good to reconsider the current handling and formatting of such errors, and facilitate easier handling on the receiving end of the errors.

This could either be by introducing different sub-classes, or by adding a more standardised property that can be check against, not different property names for each type of error, e.g.:

throw modelClass.createValidationError({
  type: 'relation-error',
  message: 'trying to insert an unallowed relation'
});

I don’t have the answer what the best solution is here, but I believe the current state is a bit too difficult to facilitate good exception handling practises.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 24

Commits related to this issue

Most upvoted comments

I don’t know how common unallowed is but it’s a word https://www.merriam-webster.com/dictionary/unallowed

Yeah, the names could have been thought out better, but it’s too late now. Maybe in 2.0 😄