This is going to be short and sweet.
class MyError extends Error {
constructor(message) {
super(message);
this.name = 'MyError';
}
}
module.exports = {
MyError,
};
You can throw the error like so.
const { MyError } = require('./errors');
try {
throw new MyError('This is my error that threw.');
} catch (err) {
if (err instanceof MyError) {
console.error('Instance of MyError found.');
}
console.error(err);
}
Top comments (0)