npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-merror

v1.0.0

Published

A REST-friendly HTTP error handler middleware for Express.js

Downloads

140

Readme

Merror Build Status

Merror

Developing REST APIs with Express, I was always looking for a clean way to send HTTP error responses along with the data I wanted, not only to allow clients to know which error has happened but to provide the information I wanted.

Express allows you to handle errors but doesn't return Error objects yet renders pages containing error information which is not so useful when developing REST APIs. Error information should be sent to the clients in JSON format so that they can process it and show it to users. So....

Here's Merror (/ˈmɪrə/). A simple wrapper around JavaScript Error objects with a middleware for Expressjs. Merror allows you to define a new HttpError object when you need in your controller. The object then will be passed to the middleware to be processed and sent to the client.

Usage

Using Merror is easy and straight-forward. Install express-merror using NPM or Yarn and import Merror and MerrorMiddlware in your application.

$ npm install --save express-merror
// If using TypeScript and ES6:
import { Merror, MerrorMiddleware } from 'express-merror';

// If using JS/RequireJS
const MerrorModule = require('express-merror');
const Merror = MerrorModule.Merror;
const MerrorMiddleware = MerrorModule.MerrorMiddleware;

Register MerrorMiddleware in your Expressjs application as a middleware after registering router module and start constructing Merror Objects whenever you hit an error in your controllers. Let's see an example:

const express = require("express");
const cors = require("cors");
const app = express();
const router = express.Router();

// Some other middleware
app.use(cors());

// Register Routing Module
app.use("/", router);

// Router
router.post("/profile", function(req, res) {
  // Pay attention that we should wrap our error object
  // with next() in order to make it travel to our middleware
  // which is registered after router module
    return next(new Merror(401, "Unauthorized Access! Custom Message!", {code: 1001,status: "REFRESH_TOKEN"} ));
});


// Merror Middleware
app.use(MerrorMiddleware());

// Router Module
app.listen(3000, () => console.log("Example app listening on port 3000!"));

If we make a request to http://localhost:3000/profile we will see this in the body of the response:

{
  "success": false,
  "statusCode": 401,
  "error": "Unauthorized",
  "message": "Unauthorized Access!",
  "properties": {
    "code": 1001,
    "status": "REFRESH_TOKEN"
  }
}

NOTE: Please pay attention that you should register the middleware after registering routes and also wrap constructed error with next() function and return it in order for Merror to work properly.

API

new Merror(statusCode, message, [properties])

Creates a new Merror object with given statusCode and message. You're free to take control of what you pass as properties it will be included in the response.

app.post("/login", function(req, res) {
    return next(new Merror(500, "Internal Server Error", {code: 1021,status: "USER_NOT_FOUND"} ));
});

Assuming we have express-validator middleware in our application we can do this:

app.post("/login", function(req, res) {
  req.assert("email", "Provided email is not correct").isEmail().notEmpty();
  req.assert("password", "Password is not valid").isLength({ min: 8 }).notEmpty();

  const errors = req.validationErrors(true);

  if (errors) {
    return next(new Merror(403, "Authentication Failed. Invalid Email or Password.", errors));
  }

  // Rest of our code to find user, etc.
});

Now if we POST an invalid email to this route we can see this in the response:

{
    "success": false,
    "statusCode": 403,
    "error": "Forbidden",
    "message": "Authentication Failed. Invalid Email or Password.",
    "properties": {
        "email": {
            "location": "body",
            "param": "email",
            "msg": "Provided email is not correct",
            "value": false
        }
    }
}

MerrorMiddleware()

Returns a simple handler that takes errors constructed in our router and sends a response with corresponding statusCode, message and properties if provided.

// Router Module
app.get('/', (req, res) => next(new Merror(404, "Not Found!")))

// Register Middleware here after all routes
app.use(MerrorMiddleware());

License

This project is licensed under MIT. Feel free to fork, change and use it however you like.

Contribution

If you feel there's something that could be better in this module make sure to fork, make changes and make a new PR with full description of what you've changed. I'll make sure to review it.