error-lib
About
The error-lib project helps developers having a unified error structure in their NodeJS/Browser (JavaScript/TypeScript) projects.
Installation
To install this package, run the command below.
npm install error-lib
yarn add error-lib
pnpm add error-lib
Diagram
Usage
To use any of the custom error libraries you need to simply import them in your typescript/javascript application.
const {
ApplicationError,
NotFoundError,
ForbiddenError,
} = require('error-lib');
const checkIfFileExist = (path) => {
if (fs.exists(path) === false) {
throw new NotFoundError(`${path} was not found.`);
}
return true;
};
const readFileContent = (path) => {
if (fs.hasAccess(path) === false) {
throw new ForbiddenError(`User does not have access to '${path}'`);
}
return 'dummy content';
};
try {
checkIfFileExist('/path/to/file');
const fileContent = readFileContent('/path/to/file');
} catch (err) {
if (err instanceof NotFoundError) {
console.error('File not found!');
} else if (err instanceof ForbiddenError) {
console.error('No access to the file');
} else {
console.error('Something went wrong!');
}
}
import { ApplicationError, NotFoundError } from 'error-lib';
const checkIfFileExist = (path) => {
if (fs.exists(path) === false) {
throw new NotFoundError(`${path} was not found.`);
}
return true;
};
const readFileContent = (path) => {
if (fs.hasAccess(path) === false) {
throw new ForbiddenError(`User does not have access to '${path}'`);
}
return 'dummy content';
};
try {
checkIfFileExist('/path/to/file');
const fileContent = readFileContent('/path/to/file');
} catch (err) {
if (err instanceof NotFoundError) {
console.error('File not found!');
} else if (err instanceof ForbiddenError) {
console.error('No access to the file');
} else {
console.error('Something went wrong!');
}
}
Extend / Custom errors
Not the errors created in this package supports all the scenarios. It's not even possible 😁.
To add a new type of error that suits your needs, follow the instruction below.
It's always a good idea to extend errors from one of the main error types in this package. Unless you have your own reasons not to do so 😁.
const { BadRequestError } = require('error-lib');
class InvalidUsernamePassword extends BadRequestError {
constructor(message, opts) {
message = message ?? 'InvalidUsernamePasswordError';
super(message, {
cause: opts?.cause,
code: opts?.code ?? 'E_INVALID_USERNAME_PASSWORD',
});
Error.captureStackTrace(this, InvalidUsernamePassword);
Object.setPrototypeOf(this, InvalidUsernamePassword.prototype);
}
}
module.exports = {
InvalidUsernamePassword,
};
if (user !== 'user1' && pass !== 'p4$sw0rd!') {
throw new InvalidUsernamePassword();
}
import { BadRequestError, BadRequestErrorConstructorOptions } from 'error-lib';
export interface InvalidUsernamePasswordConstructorOptions<
TCauseError extends Error = Error,
> extends BadRequestErrorConstructorOptions<TCauseError> {}
export class InvalidUsernamePassword<
TCause extends Error = Error,
> extends BadRequestError<TCause> {
constructor(
message?: string,
opts?: InvalidUsernamePasswordConstructorOptions<TCause>,
) {
message = message ?? 'InvalidUsernamePasswordError';
super(message, {
cause: opts?.cause,
code: opts?.code ?? 'E_INVALID_USERNAME_PASSWORD',
});
Error.captureStackTrace(this, InvalidUsernamePassword);
Object.setPrototypeOf(this, InvalidUsernamePassword.prototype);
}
}
if (user !== 'user1' && pass !== 'p4$sw0rd!') {
throw new InvalidUsernamePassword();
}
And you're good to go!
License
MIT