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

node-error-formatter

v1.0.4

Published

Node.js error formatter module capable of interpreting any kind of error into a well formatted Error object

Downloads

6

Readme

Node Error Formatter

Node.js error handling module capable of interpreting any kind of error into a well formatted Error object.

Parses error messages of unknown format into native JavaScript Error objects.

This is useful when receiving error responses from 3rd party APIs, modules or services where you have no control over what error messages contain, their data type and format.

This helper guarantees to return a well formatted Error object by analysing the data type and structure of the error object and promises to return error.message and error.statusCode.

If an error cannot be analysed a 500 Internal Server Error is returned.

Example

var error = require('node-error-formatter')  
    , someService = require('someService');  
    
someService.doSomething()
    .then(function(data){
        // Handle success
    })
    
    .catch(function(err){
        
        // We can't tell for sure what data type err is or what it contains
        // It might be an object containing a message key, a string, an array or an instance of Error
        // By using the error parser you are guaranteed an instance of Error
        var error = error.create(err); 
        
        console.log(error.statusCode); // 403
        console.log(error.message); // "Forbidden"
    });

Usage examples and acceptable parameters

All error.create calls return a JavaScript Error object containing the properties message and statusCode.

// Passing a string:  
var e = error.create('Bad robot!');  
console.log(e.message); // 'Bad robot!'  
console.log(e.statusCode); // 500  

// Passing a http status code and a message  
var e = error.create(503, 'Bad robot!');  
console.log(e.message); // 'Bad robot!'  
console.log(e.statusCode); // 503  

// Passing an http status code but no message default to standard http codes
var e = error.create(503);  
console.log(e.message); // 'Service Unavailable'  
console.log(e.statusCode); // 503  

// Not passing any arguments results in 500 Internal Server Error
var e = error.create();  
console.log(e.message); // 'Internal Server Error'  
console.log(e.statusCode); // 500  

// Object containing key statusCode
var e = error.create({statusCode: 403});  
console.log(e.message); // 'Forbidden'  
console.log(e.statusCode); // 403  

// Object containing key code
var e = error.create({code: 403});  
console.log(e.message); // 'Forbidden'  
console.log(e.statusCode); // 403  

// Object containing key status
var e = error.create({status: 403});  
console.log(e.message); // 'Forbidden'  
console.log(e.statusCode); // 403  

// Object containing key errorCode
var e = error.create({errorCode: 403});  
console.log(e.message); // 'Forbidden'  
console.log(e.statusCode); // 403 

install

npm install node-error-formatter --save

test

npm test