@blackglory/errors
Advanced tools
Comparing version 2.0.0 to 2.1.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Errors = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('iterable-operator'), require('@blackglory/types')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'iterable-operator', '@blackglory/types'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Errors = {}, global.iterableOperator, global.types)); | ||
})(this, (function (exports, iterableOperator, types) { 'use strict'; | ||
function* traverseErrorPrototypeChain(err) { | ||
let current = err; | ||
while ((current = Object.getPrototypeOf(current))) { | ||
yield current; | ||
if (current === Error.prototype) | ||
break; | ||
} | ||
} | ||
function* getErrorNames(err) { | ||
var _a; | ||
if (types.isError(err)) { | ||
for (const prototype of traverseErrorPrototypeChain(err)) { | ||
// 一些错误实现的原型可能不具有constructor成员 | ||
if ((_a = prototype.constructor) === null || _a === void 0 ? void 0 : _a.name) { | ||
yield prototype.constructor.name; | ||
} | ||
} | ||
} | ||
else { | ||
yield err.name; | ||
yield* err.ancestors; | ||
} | ||
} | ||
function isSerializableError(val) { | ||
return types.isObject(val) | ||
&& types.isString(val.name) | ||
&& types.isString(val.message) | ||
&& (types.isString(val.stack) || types.isNull(val.stack)) | ||
&& (types.isArray(val.ancestors) && val.ancestors.every(types.isString)); | ||
} | ||
class CustomError extends Error { | ||
get name() { | ||
return this.constructor.name; | ||
var _a, _b; | ||
return (_b = (_a = iterableOperator.first(getErrorNames(this))) !== null && _a !== void 0 ? _a : CustomError.name) !== null && _b !== void 0 ? _b : 'CustomError'; | ||
} | ||
static [Symbol.hasInstance](instance) { | ||
var _a; | ||
if (types.isError(instance) || isSerializableError(instance)) { | ||
const reversedClassNames = [ | ||
(_a = this.prototype.constructor.name) !== null && _a !== void 0 ? _a : this.name, | ||
...getErrorNames(this.prototype) | ||
].reverse(); | ||
const reversedInstanceNames = iterableOperator.toArray(getErrorNames(instance)).reverse(); | ||
return reversedClassNames.every((x, i) => x === reversedInstanceNames[i]); | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
} | ||
@@ -18,4 +66,6 @@ | ||
var _a; | ||
const [name, ...ancestors] = iterableOperator.toArray(getErrorNames(err)); | ||
return { | ||
name: err.name, | ||
name, | ||
ancestors, | ||
message: err.message, | ||
@@ -37,3 +87,6 @@ stack: (_a = err.stack) !== null && _a !== void 0 ? _a : null | ||
exports.assert = assert; | ||
exports.getErrorNames = getErrorNames; | ||
exports.isSerializableError = isSerializableError; | ||
exports.normalize = normalize; | ||
exports.traverseErrorPrototypeChain = traverseErrorPrototypeChain; | ||
@@ -40,0 +93,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).Errors={})}(this,(function(e){"use strict";class n extends Error{get name(){return this.constructor.name}}class t extends n{}e.AssertionError=t,e.CustomError=n,e.assert=function(e,n){if(!e)throw new t(n)},e.normalize=function(e){var n;return{name:e.name,message:e.message,stack:null!==(n=e.stack)&&void 0!==n?n:null}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("iterable-operator"),require("@blackglory/types")):"function"==typeof define&&define.amd?define(["exports","iterable-operator","@blackglory/types"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).Errors={},e.iterableOperator,e.types)}(this,(function(e,r,t){"use strict";function*o(e){let r=e;for(;(r=Object.getPrototypeOf(r))&&(yield r,r!==Error.prototype););}function*s(e){var r;if(t.isError(e))for(const t of o(e))(null===(r=t.constructor)||void 0===r?void 0:r.name)&&(yield t.constructor.name);else yield e.name,yield*e.ancestors}function n(e){return t.isObject(e)&&t.isString(e.name)&&t.isString(e.message)&&(t.isString(e.stack)||t.isNull(e.stack))&&t.isArray(e.ancestors)&&e.ancestors.every(t.isString)}class i extends Error{get name(){var e,t;return null!==(t=null!==(e=r.first(s(this)))&&void 0!==e?e:i.name)&&void 0!==t?t:"CustomError"}static[Symbol.hasInstance](e){var o;if(t.isError(e)||n(e)){const t=[null!==(o=this.prototype.constructor.name)&&void 0!==o?o:this.name,...s(this.prototype)].reverse(),n=r.toArray(s(e)).reverse();return t.every(((e,r)=>e===n[r]))}return!1}}class a extends i{}e.AssertionError=a,e.CustomError=i,e.assert=function(e,r){if(!e)throw new a(r)},e.getErrorNames=s,e.isSerializableError=n,e.normalize=function(e){var t;const[o,...n]=r.toArray(s(e));return{name:o,ancestors:n,message:e.message,stack:null!==(t=e.stack)&&void 0!==t?t:null}},e.traverseErrorPrototypeChain=o,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.umd.min.js.map |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Errors = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('iterable-operator'), require('@blackglory/types')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'iterable-operator', '@blackglory/types'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Errors = {}, global.iterableOperator, global.types)); | ||
})(this, (function (exports, iterableOperator, types) { 'use strict'; | ||
function* traverseErrorPrototypeChain(err) { | ||
let current = err; | ||
while ((current = Object.getPrototypeOf(current))) { | ||
yield current; | ||
if (current === Error.prototype) | ||
break; | ||
} | ||
} | ||
function* getErrorNames(err) { | ||
var _a; | ||
if (types.isError(err)) { | ||
for (const prototype of traverseErrorPrototypeChain(err)) { | ||
// 一些错误实现的原型可能不具有constructor成员 | ||
if ((_a = prototype.constructor) === null || _a === void 0 ? void 0 : _a.name) { | ||
yield prototype.constructor.name; | ||
} | ||
} | ||
} | ||
else { | ||
yield err.name; | ||
yield* err.ancestors; | ||
} | ||
} | ||
function isSerializableError(val) { | ||
return types.isObject(val) | ||
&& types.isString(val.name) | ||
&& types.isString(val.message) | ||
&& (types.isString(val.stack) || types.isNull(val.stack)) | ||
&& (types.isArray(val.ancestors) && val.ancestors.every(types.isString)); | ||
} | ||
class CustomError extends Error { | ||
get name() { | ||
return this.constructor.name; | ||
var _a, _b; | ||
return (_b = (_a = iterableOperator.first(getErrorNames(this))) !== null && _a !== void 0 ? _a : CustomError.name) !== null && _b !== void 0 ? _b : 'CustomError'; | ||
} | ||
static [Symbol.hasInstance](instance) { | ||
var _a; | ||
if (types.isError(instance) || isSerializableError(instance)) { | ||
const reversedClassNames = [ | ||
(_a = this.prototype.constructor.name) !== null && _a !== void 0 ? _a : this.name, | ||
...getErrorNames(this.prototype) | ||
].reverse(); | ||
const reversedInstanceNames = iterableOperator.toArray(getErrorNames(instance)).reverse(); | ||
return reversedClassNames.every((x, i) => x === reversedInstanceNames[i]); | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
} | ||
@@ -18,4 +66,6 @@ | ||
var _a; | ||
const [name, ...ancestors] = iterableOperator.toArray(getErrorNames(err)); | ||
return { | ||
name: err.name, | ||
name, | ||
ancestors, | ||
message: err.message, | ||
@@ -37,3 +87,6 @@ stack: (_a = err.stack) !== null && _a !== void 0 ? _a : null | ||
exports.assert = assert; | ||
exports.getErrorNames = getErrorNames; | ||
exports.isSerializableError = isSerializableError; | ||
exports.normalize = normalize; | ||
exports.traverseErrorPrototypeChain = traverseErrorPrototypeChain; | ||
@@ -40,0 +93,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).Errors={})}(this,(function(e){"use strict";class n extends Error{get name(){return this.constructor.name}}class t extends n{}e.AssertionError=t,e.CustomError=n,e.assert=function(e,n){if(!e)throw new t(n)},e.normalize=function(e){var n;return{name:e.name,message:e.message,stack:null!==(n=e.stack)&&void 0!==n?n:null}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("iterable-operator"),require("@blackglory/types")):"function"==typeof define&&define.amd?define(["exports","iterable-operator","@blackglory/types"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).Errors={},e.iterableOperator,e.types)}(this,(function(e,r,t){"use strict";function*o(e){let r=e;for(;(r=Object.getPrototypeOf(r))&&(yield r,r!==Error.prototype););}function*s(e){var r;if(t.isError(e))for(const t of o(e))(null===(r=t.constructor)||void 0===r?void 0:r.name)&&(yield t.constructor.name);else yield e.name,yield*e.ancestors}function n(e){return t.isObject(e)&&t.isString(e.name)&&t.isString(e.message)&&(t.isString(e.stack)||t.isNull(e.stack))&&t.isArray(e.ancestors)&&e.ancestors.every(t.isString)}class i extends Error{get name(){var e,t;return null!==(t=null!==(e=r.first(s(this)))&&void 0!==e?e:i.name)&&void 0!==t?t:"CustomError"}static[Symbol.hasInstance](e){var o;if(t.isError(e)||n(e)){const t=[null!==(o=this.prototype.constructor.name)&&void 0!==o?o:this.name,...s(this.prototype)].reverse(),n=r.toArray(s(e)).reverse();return t.every(((e,r)=>e===n[r]))}return!1}}class a extends i{}e.AssertionError=a,e.CustomError=i,e.assert=function(e,r){if(!e)throw new a(r)},e.getErrorNames=s,e.isSerializableError=n,e.normalize=function(e){var t;const[o,...n]=r.toArray(s(e));return{name:o,ancestors:n,message:e.message,stack:null!==(t=e.stack)&&void 0!==t?t:null}},e.traverseErrorPrototypeChain=o,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.umd.min.js.map |
export declare type CustomErrorConstructor<T extends CustomError = CustomError> = new (message?: string) => T; | ||
export declare class CustomError extends Error { | ||
get name(): string; | ||
static [Symbol.hasInstance](instance: unknown): boolean; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CustomError = void 0; | ||
const iterable_operator_1 = require("iterable-operator"); | ||
const get_error_names_1 = require("./get-error-names"); | ||
const types_1 = require("@blackglory/types"); | ||
const serializable_error_1 = require("./serializable-error"); | ||
class CustomError extends Error { | ||
get name() { | ||
return this.constructor.name; | ||
var _a, _b; | ||
return (_b = (_a = (0, iterable_operator_1.first)((0, get_error_names_1.getErrorNames)(this))) !== null && _a !== void 0 ? _a : CustomError.name) !== null && _b !== void 0 ? _b : 'CustomError'; | ||
} | ||
static [Symbol.hasInstance](instance) { | ||
var _a; | ||
if ((0, types_1.isError)(instance) || (0, serializable_error_1.isSerializableError)(instance)) { | ||
const reversedClassNames = [ | ||
(_a = this.prototype.constructor.name) !== null && _a !== void 0 ? _a : this.name, | ||
...(0, get_error_names_1.getErrorNames)(this.prototype) | ||
].reverse(); | ||
const reversedInstanceNames = (0, iterable_operator_1.toArray)((0, get_error_names_1.getErrorNames)(instance)).reverse(); | ||
return reversedClassNames.every((x, i) => x === reversedInstanceNames[i]); | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
} | ||
exports.CustomError = CustomError; | ||
//# sourceMappingURL=custom-error.js.map |
export * from './custom-error'; | ||
export * from './assertion-error'; | ||
export * from './serializable-error'; | ||
export * from './normalize'; | ||
export * from './assert'; | ||
export * from './get-error-names'; | ||
export * from './traverse-error-prototype-chain'; |
@@ -15,4 +15,7 @@ "use strict"; | ||
__exportStar(require("./assertion-error"), exports); | ||
__exportStar(require("./serializable-error"), exports); | ||
__exportStar(require("./normalize"), exports); | ||
__exportStar(require("./assert"), exports); | ||
__exportStar(require("./get-error-names"), exports); | ||
__exportStar(require("./traverse-error-prototype-chain"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,6 +0,2 @@ | ||
export interface SerializableError { | ||
name: string; | ||
message: string; | ||
stack: string | null; | ||
} | ||
import { SerializableError } from './serializable-error'; | ||
export declare function normalize(err: Error): SerializableError; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.normalize = void 0; | ||
const get_error_names_1 = require("./get-error-names"); | ||
const iterable_operator_1 = require("iterable-operator"); | ||
function normalize(err) { | ||
var _a; | ||
const [name, ...ancestors] = (0, iterable_operator_1.toArray)((0, get_error_names_1.getErrorNames)(err)); | ||
return { | ||
name: err.name, | ||
name, | ||
ancestors, | ||
message: err.message, | ||
@@ -9,0 +13,0 @@ stack: (_a = err.stack) !== null && _a !== void 0 ? _a : null |
export declare type CustomErrorConstructor<T extends CustomError = CustomError> = new (message?: string) => T; | ||
export declare class CustomError extends Error { | ||
get name(): string; | ||
static [Symbol.hasInstance](instance: unknown): boolean; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CustomError = void 0; | ||
const iterable_operator_1 = require("iterable-operator"); | ||
const get_error_names_1 = require("./get-error-names"); | ||
const types_1 = require("@blackglory/types"); | ||
const serializable_error_1 = require("./serializable-error"); | ||
class CustomError extends Error { | ||
get name() { | ||
return this.constructor.name; | ||
var _a, _b; | ||
return (_b = (_a = (0, iterable_operator_1.first)((0, get_error_names_1.getErrorNames)(this))) !== null && _a !== void 0 ? _a : CustomError.name) !== null && _b !== void 0 ? _b : 'CustomError'; | ||
} | ||
static [Symbol.hasInstance](instance) { | ||
var _a; | ||
if ((0, types_1.isError)(instance) || (0, serializable_error_1.isSerializableError)(instance)) { | ||
const reversedClassNames = [ | ||
(_a = this.prototype.constructor.name) !== null && _a !== void 0 ? _a : this.name, | ||
...(0, get_error_names_1.getErrorNames)(this.prototype) | ||
].reverse(); | ||
const reversedInstanceNames = (0, iterable_operator_1.toArray)((0, get_error_names_1.getErrorNames)(instance)).reverse(); | ||
return reversedClassNames.every((x, i) => x === reversedInstanceNames[i]); | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
} | ||
exports.CustomError = CustomError; | ||
//# sourceMappingURL=custom-error.js.map |
export * from './custom-error'; | ||
export * from './assertion-error'; | ||
export * from './serializable-error'; | ||
export * from './normalize'; | ||
export * from './assert'; | ||
export * from './get-error-names'; | ||
export * from './traverse-error-prototype-chain'; |
@@ -15,4 +15,7 @@ "use strict"; | ||
__exportStar(require("./assertion-error"), exports); | ||
__exportStar(require("./serializable-error"), exports); | ||
__exportStar(require("./normalize"), exports); | ||
__exportStar(require("./assert"), exports); | ||
__exportStar(require("./get-error-names"), exports); | ||
__exportStar(require("./traverse-error-prototype-chain"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,6 +0,2 @@ | ||
export interface SerializableError { | ||
name: string; | ||
message: string; | ||
stack: string | null; | ||
} | ||
import { SerializableError } from './serializable-error'; | ||
export declare function normalize(err: Error): SerializableError; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.normalize = void 0; | ||
const get_error_names_1 = require("./get-error-names"); | ||
const iterable_operator_1 = require("iterable-operator"); | ||
function normalize(err) { | ||
var _a; | ||
const [name, ...ancestors] = (0, iterable_operator_1.toArray)((0, get_error_names_1.getErrorNames)(err)); | ||
return { | ||
name: err.name, | ||
name, | ||
ancestors, | ||
message: err.message, | ||
@@ -9,0 +13,0 @@ stack: (_a = err.stack) !== null && _a !== void 0 ? _a : null |
{ | ||
"name": "@blackglory/errors", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Common errors", | ||
@@ -43,9 +43,11 @@ "files": [ | ||
"devDependencies": { | ||
"@commitlint/cli": "^16.0.2", | ||
"@blackglory/jest-matchers": "^0.2.1", | ||
"@blackglory/log": "^0.1.0", | ||
"@commitlint/cli": "^16.1.0", | ||
"@commitlint/config-conventional": "^16.0.0", | ||
"@rollup/plugin-typescript": "^8.3.0", | ||
"@types/jest": "^27.4.0", | ||
"@typescript-eslint/eslint-plugin": "^5.9.1", | ||
"@typescript-eslint/parser": "^5.9.1", | ||
"eslint": "^8.6.0", | ||
"@typescript-eslint/eslint-plugin": "^5.10.1", | ||
"@typescript-eslint/parser": "^5.10.1", | ||
"eslint": "^8.7.0", | ||
"husky": "^4.3.8", | ||
@@ -56,11 +58,14 @@ "jest": "^27.4.7", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.63.0", | ||
"rollup": "^2.66.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"standard-version": "^9.3.2", | ||
"ts-jest": "^27.0.7", | ||
"ts-jest": "^27.1.3", | ||
"tscpaths": "^0.0.9", | ||
"tsd": "^0.19.1", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.5.4" | ||
"typescript": "^4.5.5" | ||
}, | ||
"dependencies": { | ||
"iterable-operator": "^0.14.5" | ||
} | ||
} |
@@ -12,5 +12,5 @@ # errors | ||
## API | ||
### Interfaces | ||
```ts | ||
type CustomErrorConstructor<T extends CustomError = CustomError> = new (message?: string) => T | ||
type CustomErrorConstructor<T extends CustomError = CustomError> = | ||
new (message?: string) => T | ||
@@ -21,2 +21,3 @@ interface SerializableError { | ||
stack: string | null | ||
ancestors: string[] | ||
} | ||
@@ -30,2 +31,6 @@ ``` | ||
`CustomError` has better default behaviors than `Error`: | ||
- `console.error` prints the correct exception name, not `Error`. | ||
- `instanceof` operator matches based on names rather than inheritance relationships, which helps `SerializableError instanceof CustomError`. | ||
### AssertionError | ||
@@ -41,2 +46,7 @@ ```ts | ||
### isSerializableError | ||
```ts | ||
function isSerializableError(val: unknown): val is SerializableError | ||
``` | ||
### assert | ||
@@ -49,1 +59,11 @@ ```ts | ||
``` | ||
### getErrorNames | ||
```ts | ||
function getErrorNames(err: Error | SerializableError): Iterable<string> | ||
``` | ||
### traverseErrorPrototypeChain | ||
```ts | ||
function traverseErrorPrototypeChain(err: Error): Iterable<Error> | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
81472
67
642
65
1
22
3
1
+ Addediterable-operator@^0.14.5
+ Added@blackglory/errors@1.1.2(transitive)
+ Added@blackglory/go@0.1.1(transitive)
+ Added@blackglory/types@0.6.5(transitive)
+ Addediterable-operator@0.14.5(transitive)
+ Addedjustypes@0.1.7(transitive)
+ Addedlodash.isplainobject@4.0.6(transitive)