Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
express-simple-errors
Advanced tools
import express from 'express';
import ErrorHandler, { errors } from 'express-simple-errors';
const app = express();
const errorHandler = new ErrorHandler();
app.use(errorHandler.middleware());
The middleware
method will handle NotFound
errors, all errors from routing, and then send the response. You can use these as one simple function, or you can use them individually.
import express from 'express';
import ErrorHandler, { errors } from 'express-simple-errors';
const app = express();
const errorHandler = new ErrorHandler();
app.use(errorHandler.handleNotFound()); //creates new NotFound() error
app.use(errorHandler.handleError()); // builds error response and stores it in res.locals.errors
app.use(errorHandler.sendResponse()); //sends error code and response
You could inject middleware after the handleError
method. The original err
object will be intact for you to consume before passing to sendResponse
.
The stackTrace is included with the default handler method. You can turn this off when you initialize.
const errorHandler = new ErrorHandler({stackTrace: false});
You can modify the default handler, or create a custom one for more specific needs. The handlers are called based on the errors name
property. All error classes included with this module are "Error", and the handler used will be "Default".
const defaultHandler = (err, stack) => {
const res = {};
res.status= err.name;
res.message= err.message;
res.code= err.code;
if (stack) res.stackTrace= err.stack;
return res;
};
Each handler will be passed two parameters - the err
object, and the stackTrace
flag.
To modify the Default
handler, or create a custom handler, you can use the setHandler
method.
errors.setHandler('Default', (err, stack) => {
return {
status: 'error',
data: err.message
}
});
errors.setHandler('Custom', (err, stack) => ('You have encountered an error.')});
import { errors } from 'express-simple-errors';
// basic example
function foo(bar) {
if !(bar) throw new errors.Conflict();
}
//custom message
function foo(bar) {
if !(bar) throw new errors.Conflict('You are missing something!');
}
//used as route middleware functions
function checkUser(req, res, next) {
try {
const user = //do stuff to check for unser;
if (!user) return next(new errors.NotFound('This user does not exist'));
next();
} catch(e) {
next(e);
}
}
router.route(/)
.get(checkUser)
FAQs
Simple error handling middleware for NodeJS
We found that express-simple-errors 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
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.