There are 2 mistake managing situations in JavaScript. Either a mistake is tossed from a third-party (e.g. collection, data source, API) or you wish to toss a mistake on your own. While you have the mistake at your hands for the previous, for the last you require to brand-new it on your own:
feature throwAnError() {
toss brand-new Mistake(' Something failed.');
}
attempt {
throwAnError();
} catch ( mistake) {
console log( mistake message);
console log( mistake name);
}
In some cases you wish to toss personalized mistakes though. I found out that you can produce personalized mistakes the list below method: If you need to brand-new the mistake on your own, produce a brand-new personalized mistake course for it which prolongs from the indigenous JavaScript mistake. There you can establish the mistake message which is available in as specification, which imitates the indigenous mistake course API, and also you established a customized mistake name:
course BadRequestError prolongs Mistake {
erector( message) {
very( message);
this name = ' BadRequestError';
}
}
feature throwAnError() {
toss brand-new BadRequestError(' Something failed.');
}
attempt {
throwAnError();
} catch ( mistake) {
console log( mistake message);
console log( mistake name);
}
You can overload this brand-new personalized mistake with even more residential or commercial properties. As an example, if the personalized mistake need to occur at a REMAINDER API degree, I might wish to offer it a HTTP standing code which I can go back to my individuals:
course BadRequestError prolongs Mistake {
erector( message) {
very( message);
this name = ' BadRequestError';
this statusCode = 400;
}
}
feature throwAnError() {
toss brand-new BadRequestError(' Something failed.');
}
attempt {
throwAnError();
} catch ( mistake) {
console log( mistake message);
console log( mistake name);
console log( mistake statusCode);
}
Currently what takes place if you do not wish to produce a brand-new mistake however acquire from a mistake things which stems from a 3rd party like a data source or collection? As an example, the following data source demand tosses a mistake:
async feature findUserById( id) {
attempt {
return wait for data source getUserById( id);
} catch ( mistake) {
return mistake;
}
} ;
This might be alright for many situations, however in particular situations, like having this occur for a REMAINDER API, I might wish to personalize the mistake with an HTTP standing code for my web server middleware. However, I produce a customized mistake course for it, expand from the indigenous mistake, and also come on all the residential or commercial properties from the 3rd party mistake plus my various other info:
export course BadRequestError prolongs Mistake {
erector( mistake) {
very( mistake message);
this information = { mistake } ;
this statusCode = 400;
}
}
async feature findUserById( id) {
attempt {
return wait for data source getUserById( id);
} catch ( mistake) {
return brand-new BadRequestError( mistake);
}
} ;
This is exactly how I can expand from a mistake which is currently originating from elsewhere. Nevertheless, the last instances have actually covered both situations: tossing a brand-new personalized mistake from the ground up and also tailoring a mistake which originates from elsewhere.