Skip to content

Commit

Permalink
[ADD] middlewares | validator and error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
raa-dev committed Sep 7, 2022
1 parent a2f7e03 commit ab8a43e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions middlewares/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function logErrors (err, req, res, next) {
console.error(err);
next(err);
}

function errorHandler(err, req, res, next) {
res.status(500).json({
message: err.message,
stack: err.stack,
});
next();
}

function boomErrorHandler(err, req, res, next) {
if (err.isBoom) {
const { output } = err;
res.status(output.statusCode).json(output.payload);
}
next(err);
}


export { logErrors, errorHandler, boomErrorHandler };
14 changes: 14 additions & 0 deletions middlewares/validatorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Boom from "@hapi/boom";

function validatorHandler(schema, property) {
return (req, res, next) => {
const data = req[property];
const { error } = schema.validate(data, { abortEarly: false });
if (error) {
next(Boom.badRequest(error));
}
next();
}
}

export { validatorHandler };

0 comments on commit ab8a43e

Please sign in to comment.