
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
custom-error-exceptions
Advanced tools
Custom error exceptions are made to make it easier to handling errors. You can also make custom errors as needed.
$ npm install custom-error-exceptions
Using Template Error (BAD_REQUEST)
const express = require('express');
const {
handlers: { errorHandler },
errors: { BadRequestError }
} = require('custom-error-exceptions');
const app = express();
const port = 3044;
app.use(express.json());
//TEMPLATE BAD REQUEST ERROR
app.post('/', (req, res) => {
const name = req.body.name;
if (typeof name !== 'string') {
throw new BadRequestError();
}
res.send(
req.body
);
});
//Error Handler
app.use(errorHandler);
app.listen(port, () => console.log(`app listen on port ${port}`));
Using Template Error (BAD_REQUEST) With Custom Message
//TEMPLATE BAD REQUEST ERROR WITH CUSTOM MESSAGE
app.post('/custom-message', (req, res) => {
const name = req.body.name;
if (typeof name !== 'string') {
throw new BadRequestError('Name type must be string');
}
res.send(req.body);
});
Using Custom Error
const {
handlers: { errorHandler },
errors: { CustomError }
} = require('custom-error-exceptions');
//CUSTOM ERROR
app.post('/custom-error', (req, res) => {
const name = req.body.name;
if (typeof name !== 'string') {
throw new CustomError('Custom Message', 'CUSTOM_CODE', 700);
}
res.send(req.body);
});
Handling NOT_FOUND Route
const {
handlers: { errorHandler, notFoundHandler },
} = require('custom-error-exceptions');
------------------------------------
YOUR ROUTE
------------------------------------
app.use(notFoundHandler);
app.use(errorHandler);
How to handle UnhandledRejection for Async Function in Express
const {
handlers: { createHandler }
} = require('custom-error-exceptions');
const getName = async (req, res) => {
/*some asynchronous logic here
example :
const nameFromDb = await getDataFromDb(req.body.id);
*/
const id = req.body.id;
res.send({
id,
nameFromDb: 'John Doe'
})
}
//Implementing in Router
app.post(
'/unhandled-rejection',
createHandler(getName)
);
To further explore you can see an example this repo.
The list above is based on the client error list and server error list as on this page.
FAQs
Custom error exceptions for microservices
We found that custom-error-exceptions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.