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

nanoerror

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanoerror - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

4

CHANGELOG.md

@@ -9,3 +9,5 @@ # Changelog

## 1.2.0 - 2020-08-12 [YANKED]
## 1.1.0 - 2020-03-12 [YANKED]
[Unreleased]: https://github.com/geut/nanoerror/compare/v1.1.0...HEAD
[Unreleased]: https://github.com/geut/nanoerror/compare/v1.2.0...HEAD
const format = require('quick-format-unescaped')
module.exports = function createError (code, message = '%s') {
return class extends Error {
static equals (err) {
return err instanceof Error && err.isNanoerror && err.code === code
class Nanoerror extends Error {
/**
* @readonly
* @static
* @returns {string}
*/
static get code () {
return this.name
}
/**
* @static
* @param {Nanoerror} err
* @returns {boolean}
*/
static equals (err) {
return err && typeof err === 'object' && err.isNanoerror && err.code === this.code
}
/**
* Creates a new Error
* @param {...any} [args]
*/
constructor (...args) {
super()
const code = this.constructor.code
const unformatMessage = this.constructor.message
/** @type {string} */
this.message = format(unformatMessage, args)
/** @type {string} */
this.name = code
/** @type {string} */
this.code = this.name
/** @type {Array<any>} */
this.args = args
/** @type {string} */
this.unformatMessage = unformatMessage
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor)
} else {
this.stack = (new Error(this.message)).stack
}
}
constructor (...args) {
super(format(message, args))
/**
* @readonly
* @returns {boolean}
*/
get isNanoerror () {
return true
}
}
this.name = code
this.code = code
this.args = args
this.unformatMessage = message
/**
* @type {string}
* @static
* @memberof Nanoerror
*/
Nanoerror.message = ''
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor)
} else {
this.stack = (new Error(this.message)).stack
/**
* Creates a new Error class
*
* @param {string} code
* @param {string} message
*/
function createError (code, message = '%s') {
const obj = {
[code]: class extends Nanoerror {
/**
* @static
* @param {Error} err
* @returns {Nanoerror}
*/
static from (err) {
if (!(err instanceof Error)) throw new Error('invalid error instance')
const newErr = new obj[code](err.message)
newErr.stack = err.stack || newErr.stack
return newErr
}
}
}
get isNanoerror () {
return true
}
}
obj[code].message = message
return obj[code]
}
module.exports = createError
{
"name": "nanoerror",
"version": "1.1.0",
"version": "1.2.0",
"description": "Small module to create code errors with format support.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -36,10 +36,14 @@ # nanoerror

#### `err.isNanoerror -> boolean`
#### `err.isNanoerror => boolean`
Returns true if the err is a nanoerror instance.
#### `ERR.equals(err) -> boolean`
#### `ERR.equals(err) => boolean`
Returns true if the err belongs to a specific nanoerror class.
#### `ERR.from(err) => ERR`
Creates an error based on the options of another one.
## <a name="issues"></a> Issues

@@ -46,0 +50,0 @@

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