New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

error-provider

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

error-provider

Manages errors for third-party modules

  • 0.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

error-provider

Node.js project

Manages errors for third-party modules

Version: 0.0.6

When you're writing a third-party module you'll probably deal with errors. If you want to send custom errors to your users you should send a code and then in your documentation explain these codes.

This module eases the error management, you only need to give a number, a code and a description and when you need them just get and throw them.

The built-in errors are also available so if you need to manually throw a EEXIST you can do so.

The error description accepts variables, that is, you can create an error with a variable and when you get it you can set the variable value:

ep.create (1000, "MY_CUSTOM_ERROR", "This is a {which} error");
console.log (ep.get ("MY_CUSTOM_ERROR", { which: "custom" }));

/*
Prints:

{ [MY_CUSTOM_ERROR: This is a custom error] errno: 1000, code: "MY_CUSTOM_ERROR" }
*/

If you want to add another property different from errno, code and description you can do it, just pass an object with the new properties (if the property value is a string it can also have variables):

ep.create (ep.next (), "MY_CUSTOM_ERROR", "This is a {which} error", {
	path: "{path}",
	fn: function (){
		console.log ("custom error");
	}
});
var error = ep.get ("MY_CUSTOM_ERROR", { which: "custom", path: "some/path" });
error.fn ();
console.log (error);

/*
Prints:

custom error
{ [MY_CUSTOM_ERROR: This is a custom error]
  errno: 100,
  code: "MY_CUSTOM_ERROR",
  path: "some/path",
  fn: [Function] }
*/
Installation
npm install error-provider
Example
var ep = require ("error-provider");

console.log (ep.get (ep.ENOENT));

var n = ep.next ();
ep.create (n, "TEST1", "test 1");
console.log (ep.get (ep.TEST1));
console.log (ep.get ("TEST1"));
console.log (ep.get (n));

/*
Prints:

{ [ENOENT: no such file or directory] errno: 34, code: "ENOENT" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
*/
Methods

There's a special method called local() that returns a local error provider. If you need a local storage with custom errors that you don't need to make public, local() can help you. If another third-party module uses the error-provider module you won't see its errors, the local error provider is local to your module. The first available errno is 0 and the set of default codes is empty, that is, you can't return a built-in error like EEXIST. You can create all the storages you want.

var ep = require ("error-provider").local ();
ep.create (ep.next (), ...);
ep.get (...);
  • ep.create(errno, code, description[, properties])
  • ep.get(id[, vars])
  • ep.next()

ep.create(errno, code, description[, properties])
Creates an error with an id, code, description and additional properties.

The strings can contain variables that can be set later. A variable is a name encapsulated inside curly braces:

ep.create (ep.next (), "TEST1", "test 1", {
	path: "{p}"
});
console.log (ep.get ("TEST1", { p: "some/path" }));

/*
Prints:

{ [TEST1: test 1] errno: 100, code: "TEST1", path: "some/path" }
*/

ep.get(id[, vars])
Returns an error. It can be a Node.js built-in error such as ENOENT or one that was created previously.

The id can be a Number, String or Object:

ep.create (ep.next (), "TEST1", "Test 1");
console.log (ep.get (ep.TEST1));
console.log (ep.get ("TEST1"));
console.log (ep.get (100));

If vars is provided, the variables found in the error properties that are String will be set with the given value.

ep.create (ep.next (), "TEST1", "test {number}", {
	n: "{number}",
	p: "{value}"
});
console.log (ep.get ("TEST1", { number: 1, value: "ok" }));

/*
Prints:

{ [TEST1: test 1]
  errno: 100,
  code: "TEST1",
  n: "1",
  p: "ok" }
*/

ep.next()
Returns the next available error number and increments the counter. This number is an identifier to create errors.

The first available number starts at 100.

The following example creates three errors with identified with 100, 101 and 102.

ep.create (ep.next (), ...);
ep.create (ep.next (), ...);
ep.create (ep.next (), ...);

Keywords

FAQs

Package last updated on 10 Mar 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc