Comparing version 1.1.0 to 1.2.0
@@ -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 |
102
index.js
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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6309
82
59