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

error-coder

v0.3.0

Published

A node.js module that generates unique error codes.

Downloads

9

Readme

error-coder

A node.js module that generates unique error codes.

Build Status

example

app.js

var ErrorCoder = require('error-coder');

var errorsMap = {
  400: {
    15: 'invalid request object - %s',
    25: 'horrible error number %d'
  },
  422: {
    15: 'missing %s parameter',
    25: 'missing %s parameter'
  },
  503: {
    15: 'database error',
    25: 'api server error'
  }
 
// create new instance
var EC = new ErrorCoder(errorsMap, { namespace: 'APP' });

function handleRequest(req, res) {
  // validations: set status for all potential 422 errors
  EC.setStatus(422);
  if(!req.body.username && !req.body.url) {
    EC
    .add(15, 'username')
    .add(25, 'url')
    .send(res);  // auto: `res.status(this.currentStatus).json(generatedErrorsObject)`
  }
  
  EC.setStatus(503);
  db.connect('foo.bar', function(err, connection) {
    if(err) {
      EC.add(15);
      var errObj = EC.send(); // do not send auto response
      res.send(errObj.status).json(errObj);
    }
  });
}

the generated error object

EC.setStatus(400).add(15).add(25, 10025).send();

will return:

{
  status: 400,
  errorCode: 'APP400_15_25',
  errorMessages: 'invalid request object<br>horrible error number 10025'
}

API

var ErrorCoder = require('error-coder');
var EC = new ErrorCoder(errorsMap, options);

Create new instance and passes the options object to the constructor class

errorsMap {Object}: Required. An object that define possible errors and their related messages (see example above).

options include:

  • namespace {String}: The name that will prefix the unique errorCode. error-coder enforces 3 chars long and uppercase standard. If omitted it will be created automatically for you, first by trying to read the name attribute from your package.json file, if the name includes '-' it takes the first letter of each separated word, otherwise it will just take the first ? 3 letters. If for some reason the name could be generated from package.json file, the name space will be 'APP'.
  • errorDelimiter {String}: A character for separating the error codes. defaults to _.
  • messageDelimiter {String}: A character for separating the errors messages. defaults to <br>.
  • distinctCodes {Boolean}: remove duplicate codes from the generated errorCode. defaults to false.

example using options

var EC = new ErrorCoder(errorsMap, {
  namespace: 'WAT', 
  errorDelimiter: '-', 
  messageDelimiter: ', ', 
  distinctCodes: true
 });
 
EC.setStatus(400).add(15).add(15).add(25, 1).send();
/*
   will return:
   {
    status: 400,
    errorCode: 'WAT400-15-25,
    errorMessages: 'invalid request object - foo , 'invalid request object - bar, horrible error number 1'   
   }
 */

EC.setStatus(statusCode)

Set a new group of errors. This is the actual status code of the http response. The statusCode should match to one of your errorsMap entries. After setting new status adding new error(EC.add) and sending (EC.send) will are related to the current status until you set a new statusCode

EC.add(errorCode, [messageVariables])

Add new error to the list of current status code errors. Needles to say that he errorCode should match one of the entries in the errorsMap

The error messages supports util.format for message formatting so in messageVariables you can pass any variables that will replace your errorsMap messages placeholders.

EC.send([response])

Send the current errors object.

If response is omitted it will return an object. Otherwise it will automatically send http.response

The response could be "native" node.js http.ServerResponse or express.js response object.

EC.getErrorsMap()

returns the current errorsMap.

install

npm install error-coder

test

npm test

license

(The MIT License)

Copyright (c) 2015 Ofer Itzhaki & the Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.