Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

es6-error

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es6-error

Easily-extendable error for use with ES6 classes

  • 4.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.4M
decreased by-27.17%
Maintainers
1
Weekly downloads
 
Created

What is es6-error?

The es6-error package provides a simple and effective way to create custom error classes in JavaScript using ES6 syntax. It extends the native Error class, making it easier to create and manage custom error types with proper stack traces.

What are es6-error's main functionalities?

Creating Custom Error Classes

This feature allows you to create custom error classes by extending the ExtendableError class provided by es6-error. The custom error class can then be used to throw and catch specific error types, with proper stack traces.

class CustomError extends ExtendableError {
  constructor(message) {
    super(message);
    this.name = 'CustomError';
  }
}

try {
  throw new CustomError('This is a custom error');
} catch (err) {
  console.error(err.name); // CustomError
  console.error(err.message); // This is a custom error
  console.error(err.stack); // Stack trace
}

Handling Custom Errors

This feature demonstrates how to handle custom errors by creating a specific error class (NotFoundError) and using it in a function. When the error is thrown, it can be caught and handled specifically based on its type.

class NotFoundError extends ExtendableError {
  constructor(resource) {
    super(`${resource} not found`);
    this.name = 'NotFoundError';
  }
}

function findResource(resource) {
  if (!resource) {
    throw new NotFoundError('Resource');
  }
  return resource;
}

try {
  findResource(null);
} catch (err) {
  if (err instanceof NotFoundError) {
    console.error('Specific handling for NotFoundError');
  } else {
    console.error('General error handling');
  }
}

Other packages similar to es6-error

Keywords

FAQs

Package last updated on 03 Jan 2018

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