sequelize: Sequelize4 throw ValidationError even with catch

const User = sequelize.define('User', {
    password: { type: DataTypes.STRING, allowNull: false,
                validate: {
                            len: { args: [ 8, Infinity ], msg: "Password must be at least 8 characters." },
                            not: { args: [/\s+/ig], msg: "Password must not have blank spaces." }
                          }
              }
     currentUser.updateAttributes({password: attributes.new_password})
          .then( () => {
            return res.renderJson( { password: 'Password Updated' }, 200);
          }).catch(error => {
            let errorMessage = { error: Utils.formatError(error) };
            return res.renderJson(errorMessage, 422);
          });

Running in Node6, Postgres7 and Sequelize 4.7.3.

The catch works collecting the error but in the console I can still see:


Unhandled rejection SequelizeValidationError: Validation error: Password must be at least 8 characters.
    at Promise.all.then (/Users/blanchma/workspace/backend/node_modules/sequelize/lib/instance-validator.js:77:15)
    at tryCatcher (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise.js:693:18)
    at Promise._fulfill (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise.js:638:18)
    at PromiseArray._resolve (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise_array.js:126:19)
    at PromiseArray._promiseFulfilled (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise_array.js:144:14)
    at Promise._settlePromise (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise.js:574:26)
    at Promise._settlePromise0 (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/Users/blanchma/workspace/backend/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (1 by maintainers)

Most upvoted comments

For everyone reaches here, I think this issue happens when you incorrectly overload a sequelize function in your model, for example if you’re inheriting Car from sequelize.Model and you’re overloading create method, your create method may look like this:

class Car extends Model {
    public static create(
        this: { new (): M } & typeof Model,
        values?: object,
        options?: CreateOptions,
    ): Bluebird<M> {
        const create = super.create.bind(this);
        return new Bluebird<M>(async (
            resolve: (instance: Bluebird<M>) => void,
        ): Promise<void> => {
            create(...);
            resolve();
        });
    }
}

It’s ignoring any exception that happens in create(...); the correct form would be:

class Car extends Model {
    public static create(
        this: { new (): M } & typeof Model,
        values?: object,
        options?: CreateOptions,
    ): Bluebird<M> {
        const create = super.create.bind(this);
        return new Bluebird<M>(async (
            resolve: (instance: Bluebird<M>) => void,
            reject: (error: Error) => void,
        ): Promise<void> => {
            try {
                create(...);
                resolve();
            }
            catch(exception) {
                reject(exception);
            }
        });
    }
}