Create new Class
class TypedError extends Error {
constructor(message,_type,_severity = 0) {
super(message);
this.type = _type;
this.severity = _severity;
}
};
unlike the standard Error that you can throw like so
throw Error("Wunderbar")
the extended class should be called with ** new ** keyword
throw new TypedError(error.message,"CustomerOnboardFailed",2)
and if you want to rethrow the CustomError in the trycatch block you should pass the customError object in the rethrow statement
function gonnaThrow(){
throw new TypedError("CustomError message","InsufficientBalance",9);
};
function gonnaRethrow(){
try {
gonnaThrow();
} catch (error) {
throw error;
};
};
function main(){
try {
gonnaRethrow();
} catch (error) {
const {name,message,stack,type,severity} = error;
console.log({name,message,stack,type,severity});
};
};
Top comments (0)