New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fi-errors

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fi-errors

Error manangement middleware for ExpressJS

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Fi Errors

An ExpressJS middleware to handle custom errors.

Requirements:

  • NodeJS 6.x.x

Installation

npm install --save fi-errors

Configuration

The first step is to create your configuration file. Here you can define your custom errors, redirection urls and excluded requests.

This package comes with a default configuration that you can extend or overwrite.

Sample configuration file
const config = {
  // Custom application errors
  errors: [
    // This error will be added to the component errors list
    { 
      name: 'myCustomError',
      message: 'My custom error default message'
      code: 418
    },
    
    // This error will overwrite the default BadRequestError
    { 
      name: 'BadRequestError',
      message: 'The request could not be understood by the server due to malformed syntax'
      code: 400
    }
  ],

  // Every failed HTTP request to this urls will be terminated 
  exclude: /^\/(assets|api)\//i,

  // Redirection urls
  redirect: {
    error: '/error?err=',
    lost: '/lost?url='
  }
};

Usage

To use the package you must configure it and then bind it to the express application.

Binding the component
const errors = require('fi-errors');
const express = require('express');
const app = express();

// First load the component
errors.configure(config)

// Somehow register your middlewares
registerMiddlewares(app);

// Lastly bind the errors component
errors.bind(app);
Using the component

const errors = require('fi-errors');

const { BadRequestError } = errors.list();
  
  module.exports = (router, db) => {

  const User = db.model('user');

  /**
   * Creates a user.
   */
  router.post('/', (req, res, next) => {

    User.create(req.body)

      .then((user) => {
        if (!user) {
          throw new BadRequestError('The user could not be created');
        }

        res.status(HTTP_CODE_CREATED).json(user._id);
      })

      .catch(next);

  });

});

Every error triggered in a middleware will be catched inside the component, including not found routes and unknown errors.

Documentation

Read the library docs for the methods specification.

Keywords

FAQs

Package last updated on 07 Jun 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc