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

cf-errors

v0.1.17

Published

Extensible error library

Downloads

68,236

Readme

cf-errors

Coverage Status

Extensible error library.

##Installation

$ npm install cf-errors

##Creating an error

var CFError = require('cf-errors');
var error   = new CFError("error message");

###Extending the error

var error = new CFError({field: "value", message: `error message`});

###Setting the error name

var error = new CFError({name: "ErrorType", message: "my error name"});

###Passing multiple objects will extend the previous objects

var error = new CFError({field: "value", message: `error message`}, {field2: "value"}, {field: "override first value"});

###Last argument passed to the constructor can be a string, which will populate the message field automatically

var error = new CFError({field: "value", message: `error message`}, {field2: "value"}, "my error message");

##Extending with a previous error

var extendedError = new CFError({
    message: `extended error message`,
    cause: error
});

##Printing the stack will print the stack of all previous errors too

console.log(extendedError.stack);

##toString() Will print the whole chain of errors in a nice way. You can always override it if you want.

CFError.prototype.toString = function(){
    //your implementation
}

##Predefined Error Types

var CFError    = require('cf-errors');
var Errors     = CFError.Errors;

All predefined errors are exposed on 'CFError.Errors' object. They are actually just simple objects so using the extension capability allows us to use them easily and extend them when needed. ####Http Errors All http errors are available. They will contain a field name 'statusCode' for your use.

var error = new CFError(Errors.Http.BadRequest, {
    message: `failed to validate your request`
});

If you are using express.js then your error middleware can look something like this:

app.use(function(err, request, response, next){
    console.error(err.stack);
    var statusCode = 400;
    if (err.constructor && err.constructor.name === "CFError") {
        statusCode = err.statusCode || statusCode;
    }
    return response.status(statusCode).send(err.message);
});

####Node Errors All node.js core errors are also available using the Errors.Node object.

##Inheriting the previous error type Creating an error with the same name as its cause can be achieved using 'Inherit' as the error name.

var extendedError = new CFError(Errors.Inherit, {
    message: `extended error message`,
    cause: error
});

This will also work

var extendedError = new CFError({
    name: "Inherit",
    message: `extended error message`,
    cause: error
});

##Getting the value of the first occurrence of a field in the chain Sometimes you will populate an error with a field and wrap it with an additional error. In order to get the value of the field you will need to recursively go over the whole chain. In order to get the first value of a field in the chain use 'getFirstValue' function.

var error = new CFError({field: "value", field1: "firstValue"});
var extendedError = new CFError({cause: error, field1: "newValue"});
extendedError.getFirstValue('field') // "value"
extendedError.getFirstValue('field1') // "newValue"
extendedError.getFirstValue('field2') // undefined

##Running the tests 'npm test' or 'gulp unit_test'