
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
http-exception-transformer
Advanced tools
Always wanted to ensure that your express application to have extensive error handling and expose a structured and reliable API at the same time? This package is written to ensure the same.
Always wanted to ensure that your express application to have extensive error handling and expose a structured and reliable API at the same time? This package is written to ensure the same.
To add the package to your application, use
yarn -> yarn add http-exception-transformer
npm -> npm install http-exception-transformer
Now in your express.js application, to trigger an exception, throw the error anywhere. This even works when you are calling functions (often called controllers) from the routes handler and throwing exceptions in those functions.
import { HttpExceptionTransformer } from 'http-exception-transformer'
import { BadRequestException } from 'http-exception-transformer/exceptions'
app.get("/id",(req, res)=>{
if(req.params.id == 0){
throw new BadRequestException("Only +ve ids accepted")
}
})
Now add this line when you have attached all the route handlers. Notice that it is very important to place these after adding all routes as hierarchy in which express.js propagates errors.
app.use(HttpExceptionTransformer)
Now the exceptions will return a response like these
{
"code": 400,
"error": true,
"message": "Only +ve ids accepted",
"reference":"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400"
}
import { HttpExceptionTransformer } from 'http-exception-transformer'
import { BadRequestException } from 'http-exception-transformer/exceptions'
app.get("/id",(req, res)=>{
if(req.params.id == 0){
throw new BadRequestException("Only +ve ids accepted", {}, () => {
console.log("This function will run after the error is thrown");
})
}
})
Optionally, you can configure the error handler to run a callback function that runs after the error is thrown.
Now the exceptions will return a response like these
{
"code": 400,
"error": true,
"message": "Only +ve ids accepted",
"reference":"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
"payload": {},
}
And, will also run the function that you defined earlier.
Note: In order to pass in a function, it is mandatory to pass a payload along with it. If you don't want to pass in any payload, simply pass {} as the payload.
The middleware and exceptions are designed to give user the flexibility to send data alongwith exceptions, and also the option to set a custom error message if required.
The signature of the exception is the following.
() => null.SomeException(message?: string, payload?: any, callback: () => void = () => null) {}
The following are completely valid implementations
app.get(......, ()=>{
if(...){
throw new NotFoundException();
}else if(...){
throw new BadRequestException("Invalid DATA format !")
}else {
throw new UnAuthorizedException("Not allowed !", {userID: 101})
}
})
Since the codebase is redundant, and almost all derived classes have the same structure, we're geneating the exceptions from a json file. Therefore if you wish to make changes in the exception body, please change the codebase in the generators directory.
The data source has the following structure. Feel free to send PRs for adding support for more exception types.
[
{
code: 400,
name: 'BadRequestException',
message: 'The server could not understand the request due to invalid syntax.',
reference: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400',
},
{
code: 401,
name: 'UnAuthorizedException',
message: 'The client must authenticate itself to get the requested response.',
reference: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401',
},
{
code: 404,
name: 'NotFoundException',
message: 'The server can not find the requested resource',
reference: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404',
}
]
FAQs
Always wanted to ensure that your express application to have extensive error handling and expose a structured and reliable API at the same time? This package is written to ensure the same.
We found that http-exception-transformer 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
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.