Express Sweet API Reference - v4.0.0
    Preparing search index...

    Function errorHandler

    Error handling middleware

    • Mount error handling middleware on Express application.

      Catches 404 errors and provides customizable error handling through hooks. Sets up 404 error catching and custom error handling hooks.

      Parameters

      • app: Express

        Express application instance

      Returns Promise<void>

      // This method is called automatically by express-sweet.mount()
      import errorHandler from './middlewares/errorHandler.js';

      await errorHandler(app);
      // Custom error handling in config/config.js
      export default {
      /**
      * Custom error handler hook, defaults to undefined.
      * @type {((error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void)|undefined}
      */
      hook_handle_error: (error, req, res, next) => {
      if (error.status === 404)
      res.render('error/404');
      else
      res.render('error/500');
      }
      };