Skip to content

UnknownConstraintError#message ends up as an object rather than a string #9505

Closed

Description

@papandreou

What are you doing?

(async () => {
  try {
    await sequelize.query(`DROP INDEX i_do_not_exist`);
  } catch (err) {
    console.log(err.message);
  }
})();

What do you expect to happen?

A string ('Unknown constraint error') is logged.

What is actually happening?

An object is logged.

Output, either JSON or SQL

{ message: 'Unknown constraint error',
  constraint: undefined,
  fields: undefined,
  table: undefined,
  parent: [...]
  id: 1,
  transaction: 
   { finished: undefined,
     id: '43cd28c2-77ed-4b22-84ac-c9783657d75b' } }
}

This is breaking some error handling libraries I'm using because they expect err.message to be a string (which is reasonable).

As far as I can tell from the source code, it is the intention to leave message as a string, but when an object is passed to the constructor here, it's interpreted as a string here. Perhaps the intention was to always take an options object like the error class defined above?

constructor(options) {

Dialect: postgres
Dialect version: ?
Database version: 9.6
Sequelize version: 4.37.10
Tested with latest release: No (If yes, specify that version)

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. sushantdhiman commented on Jun 5, 2018

    @sushantdhiman
    Contributor

    Your observation is correct, UniqueConstraint constructor should be accepting options like all other errors

    SSCCE

    const User = sequelize.define('user', {
      email: Sequelize.STRING
    });
    
    sequelize
      .sync({ force: true })
      .then(() => {
        return sequelize.query('ALTER TABLE users DROP CONSTRAINT i_do_not_exist');
      })
      .catch(e => {
        console.log(JSON.stringify(e, null, 2));
      });
    {
      "name": "SequelizeUnknownConstraintError",
      "parent": {
        "message": {
          "message": "Unknown constraint error",
          "parent": {
            "name": "error",
            "length": 124,
            "severity": "ERROR",
            "code": "42704",
            "file": "tablecmds.c",
            "line": "7748",
            "routine": "ATExecDropConstraint",
            "sql": "ALTER TABLE users DROP CONSTRAINT i_do_not_exist"
          }
        }
      },
      "original": {
        "message": {
          "message": "Unknown constraint error",
          "parent": {
            "name": "error",
            "length": 124,
            "severity": "ERROR",
            "code": "42704",
            "file": "tablecmds.c",
            "line": "7748",
            "routine": "ATExecDropConstraint",
            "sql": "ALTER TABLE users DROP CONSTRAINT i_do_not_exist"
          }
        }
      }
    }
  2. added
    type: bug
    on Jun 5, 2018
  3. Ankit7991 commented on Aug 21, 2020

    @Ankit7991

    Is it possible withour {force: true},

  4. Ankit7991 commented on Aug 21, 2020

    @Ankit7991

    Or I should take backup, then force: true and put backup data back in mysql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions