How do you import sequelize errors?
I want to use specific errors like SequelizeUniqueConstraintError)
for error handling.
try {
query...
} catch(e){
if (e instanceof SequelizeUniqueConstraintError) {
next(new ResourceError(e.toString(), 401))
} else {
next(new ResourceError(e.toString(), 500))
}
}
I'm getting SequelizeUniqueConstraintError is not defined
, but I can't seem to navigate through the sequelize instance to find any error classes?
Check the source code of SequelizeUniqueConstraintError
. The class named UniqueConstraintError
. The SequelizeUniqueConstraintError
is the value of name
property. It's NOT a JavaScript class. So you should use UniqueConstraintError
.
E.g.
import { UniqueConstraintError } from 'sequelize';
try {
throw new UniqueConstraintError({ message: 'test unique constraint' });
} catch (e) {
if (e instanceof UniqueConstraintError) {
console.log(401);
} else {
console.log(500);
}
}
The execution result:
401
package version: "sequelize": "^5.21.3"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With