@vaemoi/verrors-node
v1.0.1
Published
Custom NodeJS error class
Downloads
10
Readme
verrors-node
Custom NodeJS error class
Usage
// extend the Verror class to your liking to describe errors in your code
const Verror = require(`@vaemoi/verrors-node`);
class BaseError extends Verror {}
class FetchError extends BaseError {
constructor(message, code, url) {
super(`Problem with request -- ${message}: ${code}\n\t${url}`);
}
}
class BadURLError extends FetchError {
constructor(code, url) {
super(`probably a bad url`, code, url);
}
}
class ServerError extends FetchError {
constructor(code, url) {
super(`server error`, code, url);
}
}
// Catch errors and print them using .display()
...
try {
// Fetching code ...
if (!response.ok) {
const err = response.status < 500 ? OrpinError.BadURL : OrpinError.Server;
throw new err(response.status, info.cdnURI);
}
} catch (err) {
if (err instanceof Verror) {
console.log(err.display());
} else {
console.error(err);
}
}