0

I have written my javascript method to produce custom error messages for sequelize depending on the error that is caught.

However instanceOf does not work as intended as the switch case always runs the default case. I used the switch case example here https://stackoverflow.com/a/54286277/11464132

Checking the constructor of the error shows the correct subclass of error but instanceOf does not flag it as true.

Doing some research there have been some complaints regarding extending errors in typescript. And i realised that the installation of sequelize using npm defaults to the usage of the module importer written in typescript. as the importer points to the index.ts file in the types folder. Im not sure if this is the issue .

const Sequelize = require('sequelize') hovering over the 'sequelize' shows

module "c:/...../project/node_modules/sequelize/types/index"

where the index file is named index.d.ts

Is the issue with typescript?

switch (true) {
    case error instanceof Sequelize.ConnectionError:
        console.log(',')
        return res.status(400).send('[Sequelize Error] Sequelize could not connect to Database. Please check whether database is running and the appropiate config files')
    default:
        console.log(error.constructor)
        return res.status(500).send('[Sequelize Error] Sequelize Client Error')
}

the console.log(error.constructor) prints out [Function: ConnectionError]. But it doesnt go into the case error instanceof Sequelize.ConnectionError block code.

yrtwo
  • 1

1 Answers1

0

It's probably an issue with typescript. If the target isn't es6 or higher errors inheritance tree won't work correctly. See this issue.

https://github.com/Microsoft/TypeScript/issues/13965

Charlie OConor
  • 857
  • 1
  • 9
  • 18