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-globals

v0.0.2

Published

Exposes custom Errors with HTTP and logging support to the Global scope

Downloads

2

Readme

error-globals NPM version Build Status Coverage Status

This module comes with a whole list of various HTTP Errors that can be exposed to the global scope if needed. Each can be thrown with a cause Error, so you can easily stack errors as you catch them.

Usage

var errors = require('error-globals');

var typeError = new TypeError('Bad type');

throw new errors.InternalServerError(typeError, 'An error occured.');

Internal Server Error: An error occured.: Bad type
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

If you want to send a JSON response to the server you can call toJSON method on the Error.

For typeError we would get something like this:

{ name: 'TypeError',
  message: 'Bad type',
  statusCode: 500,
  logLevel: 'error',
  stack: 
   [{ name: 'Module._compile',
       filename: 'module.js',
       line: 456,
       column: 26 },
     { name: 'Object.Module._extensions..js',
       filename: 'module.js',
       line: 474,
       column: 10 },
     { name: 'Module.load',
       filename: 'module.js',
       line: 356,
       column: 32 },
     { name: 'Function.Module._load',
       filename: 'module.js',
       line: 312,
       column: 12 },
     { name: 'Function.Module.runMain',
       filename: 'module.js',
       line: 497,
       column: 10 },
     { name: 'startup', filename: 'node.js', line: 119, column: 16 },
     { name: undefined, filename: 'node.js', line: 906, column: 3 } ],
  cause: undefined
}

And for internalServerError we would get something like this:

name: 'Internal Server Error',
  message: 'An error occured.: Bad type',
  statusCode: 500,
  logLevel: 'error',
  stack: 
   [{ name: 'Module._compile',
       filename: 'module.js',
       line: 456,
       column: 26 },
     { name: 'Object.Module._extensions..js',
       filename: 'module.js',
       line: 474,
       column: 10 },
     { name: 'Module.load',
       filename: 'module.js',
       line: 356,
       column: 32 },
     { name: 'Function.Module._load',
       filename: 'module.js',
       line: 312,
       column: 12 },
     { name: 'Function.Module.runMain',
       filename: 'module.js',
       line: 497,
       column: 10 },
     { name: 'startup', filename: 'node.js', line: 119, column: 16 },
     { name: undefined, filename: 'node.js', line: 906, column: 3 } ],
  cause: 
   { name: 'TypeError',
     message: 'Bad type',
     statusCode: 500,
     logLevel: 'error',
     stack: ...
     cause: undefined
  }
}

You can also format your Error messages, like so:

var dbUrl = 'some/db/url';
var typeError = new errors.DatabaseError('Could not connect to `%s`.', dbUrl);

typeError.message; // ->'Could not connect to `some/db/url`.'

For more information what these custom Errors contain, please read the documentation below.

API

expose()

Type: Function

A method that exposes all Errors to the Global scope.

var errors = require('error-globals');

// This will throw an exception, as there is no NotFoundError variable
var error = new NotFoundError('I was not found');

errors.expose();

// Now it is exposed, and can be used
var error = new NotFoundError('I was not found');

create(name, *prototype)

Type: Function

name: String

prototype: Object

A method that creates a new Error with a defined name and prototypes, such as statusCode, logLevel, init or any other custom property you want it to have.

var errors = require('error-globals');

var MyCustomError = errors.create('MyCustomError', {
  statusCode: 412,
  logLevel: 'debug',
  foo: 'bar',
  init: function(message) {
    console.log('Initialized: ' + message);
  },
  myCustomFunction: function() {
    return this.name;
  }
});

var error = new MyCustomError('Ooops'); // -> 'Initialized: MyCustomError'

error.statusCode; // -> 412
error.logLevel; // -> 'debug'
error.foo; // -> 'bar'
error.myCustomFunction(); // -> 'MyCustomError'
error instanceof Error; // -> true

Error API documentation

cause()

Type: Function

Returns: Error

A method that returns a cause of the Error.


var myError = new Error(new TypeError("An error occurred"));

myError.cause() --> 'TypeError: An error occured'

stackTrace()

Type: Function

Returns: String

Creates a stack trace for the Error. It uses the Error.stack to build it. If the Error had a cause, the same method will be invoked on it. This way you would get the whole stack trace what happend in your application.


var myError = new Error();

myError.stackTrace();

Error: An error occurred
    at methodName (/your.file.js:13:9);
    at methodName (/your.file.js:23:16);
    at Context.<anonymous> (/your.other.file.js:50:19);

var myError = new Error(new TypeError("An error occurred"), "Error cought");

myError.stackTrace();

Error: Error cought
    at methodName (/your.file.js:13:9);
    at methodName (/your.file.js:23:16);
    at Context.<anonymous> (/your.other.file.js:50:19);
Caused by: TypeError: An error occurred
    at methodName (/your.file.js:42:12);
    at methodName (/your.file.js:24:22);

printStackTrace()

Type: Function

Prints the stack trace to sdterr.

init()

Type: Function

Invoked by the constructor, usefull to do custom argument handling for the Error. This is a private function and it should not be invoked manually.

toJSON()

Type: Function

Returns: Object

A method that returns a json representation of the Error.


var myError = new Error("An error occurred");

myError.toJSON();

{ name: 'Error',
  message: 'An error occurred',
  statusCode: 500,
  logLevel: 'error',
  stack:  [ 
   { name: 'someFunctionName',
     filename: 'location/of/the/file',
     line: 91,
     column: 9
   },
   { 
    name: 'Context.<anonymous>',
    filename: 'location/of/the/file',
    line: 28,
    column: 10
   }
}

The output above is produced only when you not are running the application in production mode. When running in production mode, logLevel and stack properties are ommited from the output.

toString()

Type: Function

Returns: String

Returns a to String representation of the Error in the format of Error.name: Error.message


var myError = new Error("An error occurred");

myError.toString() -> 'Error: An error occurred'

loggerLevel

Type: Property

Returns: String

A logger level for the Error

Error.statusCode

Type: Property

Returns: Number

HTTP status code for the Error

Predefined Errors

This module comes with a predefined set of custom errors. All Errors have a statusCode and loggerLevel properties, so we could easily set the status code of the response, and log the Error apropriatelly.

Here is a list of all defined Errors in the module:

  • TypeError
    • statusCode: 500
    • loggerLevel: error
  • EvalError
    • statusCode: 500
    • loggerLevel: error
  • InternalError
    • statusCode: 500
    • loggerLevel: error
  • RangeError
    • statusCode: 500
    • loggerLevel: error
  • ReferenceError
    • statusCode: 500
    • loggerLevel: error
  • SyntaxError
    • statusCode: 500
    • loggerLevel: error
  • UriError
    • statusCode: 500
    • loggerLevel: error
  • RuntimeError
    • statusCode: 500
    • loggerLevel: error
  • IllegalState
    • statusCode: 500
    • loggerLevel: error
  • DatabaseError
    • statusCode: 500
    • loggerLevel: error
  • WorkerError
    • statusCode: 500
    • loggerLevel: error
  • ValidationError
    • statusCode: 500
    • loggerLevel: warn

And a list off all HTTP Status errors, for instance if you wish to throw Bad Request error status response, the coresponding error class will be BadRequestError that will have a 400 status code and warn as a logger level. Maybe you wish to send Not Implemented error, then the class will be NotImplementedError with a 501 status and error as a logger level.

Licence

MIT