Comparing version 0.3.0 to 0.4.0
@@ -0,1 +1,25 @@ | ||
# [0.4.0](https://github.com/rafamel/errorish/compare/v0.3.0...v0.4.0) (2019-05-13) | ||
### Bug Fixes | ||
* **deps:** updates promist to v0.6.0 ([910b0c5](https://github.com/rafamel/errorish/commit/910b0c5)) | ||
* **deps:** updates promist to v0.7.0 ([fa8b936](https://github.com/rafamel/errorish/commit/fa8b936)) | ||
### Features | ||
* **Errorish:** adds set and assign methods ([844fa77](https://github.com/rafamel/errorish/commit/844fa77)) | ||
* **Errorish:** allows for message to be null ([feb020d](https://github.com/rafamel/errorish/commit/feb020d)) | ||
* **Errorish:** recursively obtains root ([f74fdb7](https://github.com/rafamel/errorish/commit/f74fdb7)) | ||
* **Errorish:** reverses source and data params order ([3b17f3b](https://github.com/rafamel/errorish/commit/3b17f3b)) | ||
### BREAKING CHANGES | ||
* **Errorish:** While previously `data` was the second argument and `source` the third for the | ||
Errorish class constructor, now `source` is the second and `data` the third | ||
# [0.3.0](https://github.com/rafamel/errorish/compare/v0.2.1...v0.3.0) (2019-04-27) | ||
@@ -2,0 +26,0 @@ |
@@ -30,4 +30,4 @@ 'use strict'; | ||
*/ | ||
constructor(message, data, source) { | ||
super(message); | ||
constructor(message, source, data) { | ||
super(message || undefined); | ||
@@ -41,3 +41,7 @@ _defineProperty(this, "source", void 0); | ||
} | ||
/** | ||
* Custom Error name: 'Errorish' | ||
*/ | ||
get name() { | ||
@@ -47,3 +51,3 @@ return 'Errorish'; | ||
/** | ||
* References `source` if it's an instance of Error, otherwise it references itself. | ||
* References `source.root` is an instance of `Errorish`; references `source` if it is an instance of `Error`; otherwise it references itself. | ||
*/ | ||
@@ -53,5 +57,24 @@ | ||
get root() { | ||
if (this.source instanceof Errorish) return this.source.root; | ||
return this.source instanceof Error ? this.source : this; | ||
} | ||
/** | ||
* Sets the `data` field and returns itself. | ||
*/ | ||
set(data) { | ||
this.data = data; | ||
return this; | ||
} | ||
/** | ||
* Assigns `data` to the `data` field and returns itself. | ||
*/ | ||
assign(data) { | ||
Object.assign(this.data, data); | ||
return this; | ||
} | ||
} | ||
@@ -103,3 +126,3 @@ | ||
message = options.allow.includes(typeof message) ? stringify(message, options) : options.message; | ||
error = new options.Errorish(message, data, error); | ||
error = new options.Errorish(message, error, data); | ||
@@ -114,3 +137,3 @@ if (options.capture && Error.captureStackTrace) { | ||
/** | ||
* Will return `error` if it is an instance of `options.Error` -by default, and instance of `Error`. Otherwise, it will instantiate and return an `Error` of class `options.Errorish` -`Errorish` by default. This newly created error will have: | ||
* Will return `error` if it is an instance of `options.Error` -by default, and instance of `Error`. Otherwise, it will instantiate and return an `Error` of class `options.Errorish` -`Errorish` by default. This newly created error -if created- would have: | ||
* - whatever you passed as an `error` as its `source` field. | ||
@@ -126,2 +149,11 @@ * - whatever you passed as `data` as its `data` field. | ||
/** | ||
* Returns a promise rejection with `error`, having called `ensure` on it. | ||
*/ | ||
async function rejects(error, options, data) { | ||
const condition = options && options.hasOwnProperty('case') ? options.case : true; | ||
if (condition) throw ensure(error, options, data); | ||
} | ||
/** | ||
* Returns the result of `fn`; if it throws, it will call `ensure` on the thrown error and throw it. `fn` can be an *async* function -it will be automatically detected. | ||
@@ -132,8 +164,8 @@ */ | ||
try { | ||
const res = fn(); | ||
if (!promist.isPromise(res)) return res; // if it is a promise: | ||
const response = fn(); | ||
if (!promist.isPromise(response)) return response; // if it is a promise | ||
// in case res was a lazy promise | ||
return promist.lazy((resolve, reject) => { | ||
return res.then(resolve).catch(err => reject(ensure(err, options, data))); | ||
return promist.lazy.fn(() => { | ||
return response.catch(err => rejects(err, options, data)); | ||
}); | ||
@@ -145,11 +177,2 @@ } catch (err) { | ||
/** | ||
* Returns a promise rejection with `error`, having called `ensure` on it. | ||
*/ | ||
async function rejects(error, options, data) { | ||
const condition = options && options.hasOwnProperty('case') ? options.case : true; | ||
if (condition) throw ensure(error, options, data); | ||
} | ||
const root = { | ||
@@ -156,0 +179,0 @@ name: null, |
@@ -6,3 +6,3 @@ import normalize from "../normalize.js"; | ||
/** | ||
* Will return `error` if it is an instance of `options.Error` -by default, and instance of `Error`. Otherwise, it will instantiate and return an `Error` of class `options.Errorish` -`Errorish` by default. This newly created error will have: | ||
* Will return `error` if it is an instance of `options.Error` -by default, and instance of `Error`. Otherwise, it will instantiate and return an `Error` of class `options.Errorish` -`Errorish` by default. This newly created error -if created- would have: | ||
* - whatever you passed as an `error` as its `source` field. | ||
@@ -9,0 +9,0 @@ * - whatever you passed as `data` as its `data` field. |
@@ -6,3 +6,3 @@ import stringify from "./stringify.js"; | ||
message = options.allow.includes(typeof message) ? stringify(message, options) : options.message; | ||
error = new options.Errorish(message, data, error); | ||
error = new options.Errorish(message, error, data); | ||
@@ -9,0 +9,0 @@ if (options.capture && Error.captureStackTrace) { |
@@ -11,4 +11,4 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
*/ | ||
constructor(message, data, source) { | ||
super(message); | ||
constructor(message, source, data) { | ||
super(message || undefined); | ||
@@ -22,3 +22,7 @@ _defineProperty(this, "source", void 0); | ||
} | ||
/** | ||
* Custom Error name: 'Errorish' | ||
*/ | ||
get name() { | ||
@@ -28,3 +32,3 @@ return 'Errorish'; | ||
/** | ||
* References `source` if it's an instance of Error, otherwise it references itself. | ||
* References `source.root` is an instance of `Errorish`; references `source` if it is an instance of `Error`; otherwise it references itself. | ||
*/ | ||
@@ -34,5 +38,24 @@ | ||
get root() { | ||
if (this.source instanceof Errorish) return this.source.root; | ||
return this.source instanceof Error ? this.source : this; | ||
} | ||
/** | ||
* Sets the `data` field and returns itself. | ||
*/ | ||
set(data) { | ||
this.data = data; | ||
return this; | ||
} | ||
/** | ||
* Assigns `data` to the `data` field and returns itself. | ||
*/ | ||
assign(data) { | ||
Object.assign(this.data, data); | ||
return this; | ||
} | ||
} |
import ensure from "./ensure/index.js"; | ||
import rejects from "./rejects.js"; | ||
import { isPromise, lazy } from 'promist'; | ||
@@ -9,8 +10,8 @@ | ||
try { | ||
const res = fn(); | ||
if (!isPromise(res)) return res; // if it is a promise: | ||
const response = fn(); | ||
if (!isPromise(response)) return response; // if it is a promise | ||
// in case res was a lazy promise | ||
return lazy((resolve, reject) => { | ||
return res.then(resolve).catch(err => reject(ensure(err, options, data))); | ||
return lazy.fn(() => { | ||
return response.catch(err => rejects(err, options, data)); | ||
}); | ||
@@ -17,0 +18,0 @@ } catch (err) { |
import Errorish from "../Errorish"; | ||
import { ICoreOptions, IOfType } from "../types"; | ||
/** | ||
* Will return `error` if it is an instance of `options.Error` -by default, and instance of `Error`. Otherwise, it will instantiate and return an `Error` of class `options.Errorish` -`Errorish` by default. This newly created error will have: | ||
* Will return `error` if it is an instance of `options.Error` -by default, and instance of `Error`. Otherwise, it will instantiate and return an `Error` of class `options.Errorish` -`Errorish` by default. This newly created error -if created- would have: | ||
* - whatever you passed as an `error` as its `source` field. | ||
@@ -6,0 +6,0 @@ * - whatever you passed as `data` as its `data` field. |
import { IOfType } from './types'; | ||
export default class Errorish<T extends IOfType<any>> extends Error { | ||
export default class Errorish<S> extends Error { | ||
/** | ||
* An optional `source` -should reference the object that originated the `Errorish`. | ||
*/ | ||
source?: any; | ||
source?: S; | ||
/** | ||
* An optional `data` field. | ||
*/ | ||
data: T & IOfType<any>; | ||
constructor(message?: string, data?: T | null, source?: any); | ||
data: IOfType<any>; | ||
constructor(message?: string | null, source?: S, data?: IOfType<any>); | ||
/** | ||
* Custom Error name: 'Errorish' | ||
*/ | ||
readonly name: string; | ||
/** | ||
* References `source` if it's an instance of Error, otherwise it references itself. | ||
* References `source.root` is an instance of `Errorish`; references `source` if it is an instance of `Error`; otherwise it references itself. | ||
*/ | ||
readonly root: Error; | ||
/** | ||
* Sets the `data` field and returns itself. | ||
*/ | ||
set<T extends Errorish<S>>(this: T, data: IOfType<any>): T; | ||
/** | ||
* Assigns `data` to the `data` field and returns itself. | ||
*/ | ||
assign<T extends Errorish<S>>(this: T, data: IOfType<any>): T; | ||
} |
@@ -62,4 +62,4 @@ import { isPromise, lazy } from 'promist'; | ||
*/ | ||
constructor(message, data, source) { | ||
super(message); | ||
constructor(message, source, data) { | ||
super(message || undefined); | ||
@@ -73,3 +73,7 @@ _defineProperty(this, "source", void 0); | ||
} | ||
/** | ||
* Custom Error name: 'Errorish' | ||
*/ | ||
get name() { | ||
@@ -79,3 +83,3 @@ return 'Errorish'; | ||
/** | ||
* References `source` if it's an instance of Error, otherwise it references itself. | ||
* References `source.root` is an instance of `Errorish`; references `source` if it is an instance of `Error`; otherwise it references itself. | ||
*/ | ||
@@ -85,5 +89,24 @@ | ||
get root() { | ||
if (this.source instanceof Errorish) return this.source.root; | ||
return this.source instanceof Error ? this.source : this; | ||
} | ||
/** | ||
* Sets the `data` field and returns itself. | ||
*/ | ||
set(data) { | ||
this.data = data; | ||
return this; | ||
} | ||
/** | ||
* Assigns `data` to the `data` field and returns itself. | ||
*/ | ||
assign(data) { | ||
Object.assign(this.data, data); | ||
return this; | ||
} | ||
} | ||
@@ -135,3 +158,3 @@ | ||
message = options.allow.includes(typeof message) ? stringify(message, options) : options.message; | ||
error = new options.Errorish(message, data, error); | ||
error = new options.Errorish(message, error, data); | ||
@@ -146,3 +169,3 @@ if (options.capture && Error.captureStackTrace) { | ||
/** | ||
* Will return `error` if it is an instance of `options.Error` -by default, and instance of `Error`. Otherwise, it will instantiate and return an `Error` of class `options.Errorish` -`Errorish` by default. This newly created error will have: | ||
* Will return `error` if it is an instance of `options.Error` -by default, and instance of `Error`. Otherwise, it will instantiate and return an `Error` of class `options.Errorish` -`Errorish` by default. This newly created error -if created- would have: | ||
* - whatever you passed as an `error` as its `source` field. | ||
@@ -158,20 +181,2 @@ * - whatever you passed as `data` as its `data` field. | ||
/** | ||
* Returns the result of `fn`; if it throws, it will call `ensure` on the thrown error and throw it. `fn` can be an *async* function -it will be automatically detected. | ||
*/ | ||
function throws(fn, options, data) { | ||
try { | ||
const res = fn(); | ||
if (!isPromise(res)) return res; // if it is a promise: | ||
// in case res was a lazy promise | ||
return lazy((resolve, reject) => { | ||
return res.then(resolve).catch(err => reject(ensure(err, options, data))); | ||
}); | ||
} catch (err) { | ||
throw ensure(err, options, data); | ||
} | ||
} | ||
/** | ||
* Returns a promise rejection with `error`, having called `ensure` on it. | ||
@@ -192,2 +197,20 @@ */ | ||
/** | ||
* Returns the result of `fn`; if it throws, it will call `ensure` on the thrown error and throw it. `fn` can be an *async* function -it will be automatically detected. | ||
*/ | ||
function throws(fn, options, data) { | ||
try { | ||
const response = fn(); | ||
if (!isPromise(response)) return response; // if it is a promise | ||
// in case res was a lazy promise | ||
return lazy.fn(() => { | ||
return response.catch(err => rejects(err, options, data)); | ||
}); | ||
} catch (err) { | ||
throw ensure(err, options, data); | ||
} | ||
} | ||
const root = { | ||
@@ -194,0 +217,0 @@ name: null, |
{ | ||
"name": "errorish", | ||
"description": "For those times you have an error-ish but what you really want is an Error", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"license": "MIT", | ||
@@ -38,3 +38,3 @@ "files": [ | ||
"dependencies": { | ||
"promist": "^0.5.3" | ||
"promist": "^0.7.0" | ||
}, | ||
@@ -61,7 +61,5 @@ "devDependencies": { | ||
"commitizen": "^3.1.1", | ||
"concurrently": "^4.1.0", | ||
"conventional-changelog-cli": "^2.0.17", | ||
"conventional-recommended-bump": "^4.1.1", | ||
"conventional-recommended-bump": "^5.0.0", | ||
"coveralls": "^3.0.3", | ||
"cross-env": "^5.2.0", | ||
"cz-conventional-changelog": "^2.1.0", | ||
@@ -75,3 +73,3 @@ "eslint": "^5.16.0", | ||
"eslint-plugin-jest": "^22.5.1", | ||
"eslint-plugin-node": "^8.0.1", | ||
"eslint-plugin-node": "^9.0.0", | ||
"eslint-plugin-prettier": "^3.0.1", | ||
@@ -81,10 +79,8 @@ "eslint-plugin-promise": "^4.1.1", | ||
"eslint-restricted-globals": "^0.2.0", | ||
"husky": "1.3.0", | ||
"jake": "^8.1.1", | ||
"husky": "^2.2.0", | ||
"jest-cli": "^24.7.1", | ||
"markdownlint-cli": "^0.15.0", | ||
"nps": "^5.9.5", | ||
"kpo": "^0.5.2", | ||
"markdownlint-cli": "^0.16.0", | ||
"onchange": "^5.2.0", | ||
"prettier": "^1.17.0", | ||
"shx": "^0.3.2", | ||
"slimconf": "^0.9.0", | ||
@@ -91,0 +87,0 @@ "ttypescript": "^1.5.6", |
# errorish | ||
[](https://www.npmjs.com/package/errorish) | ||
[](https://travis-ci.org/rafamel/errorish) | ||
[](https://coveralls.io/github/rafamel/errorish) | ||
[](https://travis-ci.org/rafamel/errorish) | ||
[](https://coveralls.io/github/rafamel/errorish) | ||
[](https://david-dm.org/rafamel/errorish) | ||
@@ -158,2 +158,2 @@ [](https://snyk.io/test/npm/errorish) | ||
} | ||
``` | ||
``` |
43132
45
792
159
+ Addedpromist@0.7.0(transitive)
- Removedpromist@0.5.3(transitive)
Updatedpromist@^0.7.0