Nodejs Exception Handler
npm install js-exceptions> Error handling is core to any stable and reliable solution mostly a REST API.
> I use this solution in all of my Nodejs - Express projects
> It never disappoints!
``npm`
npm install js-exceptions
`TS
import { HttpException } from "js-exceptions";
throw new HttpException("Internal Server Error", 500);
`
> HttpException is the core exception - all other type of exceptions extends it and you can use it directly
`TS
import {
HttpException,
NotFoundException,
UnauthorizedException,
} from "js-exceptions";
throw new UnauthorizedException("Your are not allowed!");
`
> HttpException can be used to extend your error-handling needs
`TS
import { HttpException, HttpCodes } from "js-exceptions";
class ForbiddenException extends HttpException {
status_code: HttpCodes = 403;
}
throw new ForbiddenException("Private resource");
class ForbiddenException extends HttpException {
status_code: HttpCodes = 403;
constructor(resource: string) {
super(${resource} is private);
}
}
throw new ForbiddenException("Book");
``