http-error-handler.js 580 B

12345678910111213141516171819202122232425
  1. import { isHttpError } from 'http-errors';
  2. import loggerFactory from '~/utils/logger';
  3. const logger = loggerFactory('growi:middleware:htto-error-handler');
  4. module.exports = async(err, req, res, next) => {
  5. // handle if the err is a HttpError instance
  6. if (isHttpError(err)) {
  7. const httpError = err;
  8. try {
  9. return res
  10. .status(httpError.status)
  11. .send({
  12. status: httpError.status,
  13. message: httpError.message,
  14. });
  15. }
  16. catch (err) {
  17. logger.error('Cannot call res.send() twice:', err);
  18. }
  19. }
  20. next(err);
  21. };