Socket
Socket
Sign inDemoInstall

https-error

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

https-error

Provides the HttpsError class and associated factory methods.


Version published
Weekly downloads
8
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

https-error

Provides the HttpsError class and associated factory methods.

Version 1.0.2

Usage

$ npm install --save https-error
const HttpsError = require('https-error');

function sqrt(val) {
	if (val < 0) {
	    throw HttpsError.badRequest('Value %d cannot be negative.', val);
	} else {
	    return Math.sqrt(val);
    }
}

let err = HttpsError.internalServerError('Cannot connect to the database.');

console.log(err.toString());
// Error: 500 (Internal Server Error) Cannot connect to the database.

console.log(err.toJson());
// Outputs an object.

console.log(err.toHtml());
// Outputs an HTML string.

The badRequest method and internalServerError method are factory methods. No new keyword is required. There is one factory method for each of the 400- and 500-series errors.

If you want to call the constructor yourself, you can:

function sqrt(val) {
	if (val < 0) {
	    throw new HttpsError(400, util.format('Value %d cannot be negative.', val));
	} else {
	    return Math.sqrt(val);
    }
}

As you can see, using the factory method...

  • is more readable,
  • does not require the new keyword,
  • handles util.format arguments.

You can also pass in an Error object...

let err = new Error('That record already exists.');

let conflict = HttpsError.conflict(err);

console.log(conflict.toString());
// Error: 409 (Conflict) That record already exists.

This is useful when wrapping a library error into an error for your REST API.

Keywords

FAQs

Package last updated on 23 Apr 2016

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