UNPKG

@misterzik/espressojs

Version:

EspressoJS Introducing Espresso.JS, your ultimate Express configuration starting point and boilerplate. With its simplicity and lack of opinionation, EspressoJS offers plug-and-play configurations built on top of Express.

74 lines (64 loc) 1.67 kB
/* * _| _| _| _| _|_|_| * _| _| _|_| _|_| _| _| * _| _| _| _| _| _| _| * _| _| _| _| _| _| * _| _| _| _|_|_| * EspressoJS - Error Handler Middleware */ const logger = require("../utils/logger"); class AppError extends Error { constructor(message, statusCode) { super(message); this.statusCode = statusCode; this.status = `${statusCode}`.startsWith("4") ? "fail" : "error"; this.isOperational = true; Error.captureStackTrace(this, this.constructor); } } const errorHandler = (err, req, res, next) => { err.statusCode = err.statusCode || 500; err.status = err.status || "error"; logger.error( `${err.statusCode} - ${err.message} - ${req.originalUrl} - ${req.method} - ${req.ip}` ); if (process.env.NODE_ENV === "development") { res.status(err.statusCode).json({ status: err.status, error: err, message: err.message, stack: err.stack, }); } else { if (err.isOperational) { res.status(err.statusCode).json({ status: err.status, message: err.message, }); } else { logger.error("ERROR 💥", err); res.status(500).json({ status: "error", message: "Something went wrong!", }); } } }; const notFoundHandler = (req, res, next) => { const err = new AppError( `Cannot find ${req.originalUrl} on this server!`, 404 ); next(err); }; const asyncHandler = (fn) => { return (req, res, next) => { Promise.resolve(fn(req, res, next)).catch(next); }; }; module.exports = { AppError, errorHandler, notFoundHandler, asyncHandler, };