set-error-class
Advanced tools
+14
-27
| export const normalizeArgs=function( | ||
| error, | ||
| ErrorClass, | ||
| currentName=error.name) | ||
| { | ||
| export const normalizeArgs=(error,ErrorClass,currentName=error.name)=>{ | ||
| validateErrorClass(ErrorClass); | ||
| if(typeof currentName!=="string"){ | ||
| throw new TypeError(`currentName must be a string: ${currentName}`); | ||
| throw new TypeError(`currentName must be a string: ${currentName}`) | ||
| } | ||
| return currentName; | ||
| return currentName | ||
| }; | ||
| const validateErrorClass=function(ErrorClass){ | ||
| const validateErrorClass=(ErrorClass)=>{ | ||
| if(!isClass(ErrorClass)){ | ||
| throw new TypeError(`ErrorClass must be a class: ${ErrorClass}`); | ||
| throw new TypeError(`ErrorClass must be a class: ${ErrorClass}`) | ||
| } | ||
| if(!isErrorClass(ErrorClass.prototype)){ | ||
| throw new TypeError(`ErrorClass must inherit from Error: ${ErrorClass}`); | ||
| throw new TypeError(`ErrorClass must inherit from Error: ${ErrorClass}`) | ||
| } | ||
@@ -28,27 +24,18 @@ | ||
| throw new TypeError( | ||
| `ErrorClass must be have a valid constructor: ${ErrorClass}`); | ||
| `ErrorClass must be have a valid constructor: ${ErrorClass}` | ||
| ) | ||
| } | ||
| }; | ||
| const isClass=function(ErrorClass){ | ||
| return( | ||
| const isClass=(ErrorClass)=> | ||
| typeof ErrorClass==="function"&& | ||
| typeof ErrorClass.prototype==="object"&& | ||
| ErrorClass.prototype!==null); | ||
| ErrorClass.prototype!==null; | ||
| }; | ||
| const isErrorClass=function(prototype){ | ||
| return( | ||
| const isErrorClass=(prototype)=> | ||
| prototype!==null&&( | ||
| prototype.name==="Error"|| | ||
| isErrorClass(Object.getPrototypeOf(prototype)))); | ||
| prototype.name==="Error"||isErrorClass(Object.getPrototypeOf(prototype))); | ||
| }; | ||
| const hasConstructor=function(ErrorClass){ | ||
| return typeof ErrorClass.prototype.constructor==="function"; | ||
| }; | ||
| //# sourceMappingURL=args.js.map | ||
| const hasConstructor=(ErrorClass)=> | ||
| typeof ErrorClass.prototype.constructor==="function"; |
| export const setNonEnumProp=function(error,propName,value){ | ||
| export const setNonEnumProp=(error,propName,value)=>{ | ||
@@ -9,4 +9,3 @@ Object.defineProperty(error,propName,{ | ||
| configurable:true | ||
| }); | ||
| }; | ||
| //# sourceMappingURL=enum.js.map | ||
| }) | ||
| }; |
@@ -8,3 +8,3 @@ import normalizeException from"normalize-exception"; | ||
| export default function setErrorClass(error,ErrorClass,currentName){ | ||
| const setErrorClass=(error,ErrorClass,currentName)=>{ | ||
| const errorA=normalizeException(error); | ||
@@ -14,4 +14,5 @@ const currentNameA=normalizeArgs(errorA,ErrorClass,currentName); | ||
| updateStack(errorA,currentNameA); | ||
| return errorA; | ||
| } | ||
| //# sourceMappingURL=main.js.map | ||
| return errorA | ||
| }; | ||
| export default setErrorClass; |
+18
-28
| import{setNonEnumProp}from"./enum.js"; | ||
| export const updatePrototype=function(error,ErrorClass){ | ||
| export const updatePrototype=(error,ErrorClass)=>{ | ||
| if(Object.getPrototypeOf(error)===ErrorClass.prototype){ | ||
| return; | ||
| return | ||
| } | ||
@@ -11,3 +11,3 @@ | ||
| deleteOwnProperty(error,"constructor"); | ||
| fixName(error,ErrorClass); | ||
| fixName(error,ErrorClass) | ||
| }; | ||
@@ -19,5 +19,5 @@ | ||
| const setPrototype=function(error,ErrorClass){ | ||
| const setPrototype=(error,ErrorClass)=>{ | ||
| Object.setPrototypeOf(error,ErrorClass.prototype); | ||
| Object.setPrototypeOf(error,ErrorClass.prototype) | ||
| }; | ||
@@ -34,3 +34,3 @@ | ||
| const fixName=function(error,ErrorClass){ | ||
| const fixName=(error,ErrorClass)=>{ | ||
| deleteOwnProperty(error,"name"); | ||
@@ -41,41 +41,31 @@ | ||
| if(error.name!==prototypeName){ | ||
| setNonEnumProp(error,"name",prototypeName); | ||
| setNonEnumProp(error,"name",prototypeName) | ||
| } | ||
| }; | ||
| const getClassName=function(prototype){ | ||
| return( | ||
| const getClassName=(prototype)=> | ||
| getPrototypeName(prototype)?? | ||
| getConstructorName(prototype)?? | ||
| getClassName(Object.getPrototypeOf(prototype))); | ||
| getClassName(Object.getPrototypeOf(prototype)); | ||
| }; | ||
| const getPrototypeName=function(prototype){ | ||
| return isOwn.call(prototype,"name")&&isDefinedString(prototype.name)? | ||
| const getPrototypeName=(prototype)=> | ||
| Object.hasOwn(prototype,"name")&&isDefinedString(prototype.name)? | ||
| prototype.name: | ||
| undefined; | ||
| }; | ||
| const getConstructorName=function(prototype){ | ||
| return typeof prototype.constructor==="function"&& | ||
| const getConstructorName=(prototype)=> | ||
| typeof prototype.constructor==="function"&& | ||
| isDefinedString(prototype.constructor.name)? | ||
| prototype.constructor.name: | ||
| undefined; | ||
| }; | ||
| const isDefinedString=function(value){ | ||
| return typeof value==="string"&&value!==""; | ||
| }; | ||
| const isDefinedString=(value)=>typeof value==="string"&&value!==""; | ||
| const deleteOwnProperty=function(error,propName){ | ||
| if(isOwn.call(error,propName)){ | ||
| const deleteOwnProperty=(error,propName)=>{ | ||
| if(Object.hasOwn(error,propName)){ | ||
| delete error[propName]; | ||
| delete error[propName] | ||
| } | ||
| }; | ||
| const{hasOwnProperty:isOwn}=Object.prototype; | ||
| //# sourceMappingURL=prototype.js.map | ||
| }; |
+12
-19
@@ -5,23 +5,20 @@ import{setNonEnumProp}from"./enum.js"; | ||
| export const updateStack=function(error,currentName){ | ||
| export const updateStack=(error,currentName)=>{ | ||
| if(!shouldUpdateStack(error,currentName)){ | ||
| return; | ||
| return | ||
| } | ||
| const stack=getStack(error,currentName); | ||
| setNonEnumProp(error,"stack",stack); | ||
| setNonEnumProp(error,"stack",stack) | ||
| }; | ||
| const shouldUpdateStack=function(error,currentName){ | ||
| return( | ||
| const shouldUpdateStack=(error,currentName)=> | ||
| currentName!==error.name&& | ||
| currentName!==""&& | ||
| error.stack.includes(currentName)&& | ||
| stackIncludesName()); | ||
| stackIncludesName(); | ||
| }; | ||
| const stackIncludesName=()=>{ | ||
| const stackIncludesName=function(){ | ||
| class StackError extends Error{} | ||
@@ -39,3 +36,3 @@ const descriptor={ | ||
| const{stack}=new StackError(""); | ||
| return typeof stack==="string"&&stack.includes(EXAMPLE_NAME); | ||
| return typeof stack==="string"&&stack.includes(EXAMPLE_NAME) | ||
| }; | ||
@@ -52,5 +49,5 @@ | ||
| const getStack=function({name,stack},currentName){ | ||
| const getStack=({name,stack},currentName)=>{ | ||
| if(stack.startsWith(`${currentName}: `)){ | ||
| return stack.replace(currentName,name); | ||
| return stack.replace(currentName,name) | ||
| } | ||
@@ -60,3 +57,3 @@ | ||
| const[fromA,to]=replacers.find(([from])=>stack.includes(from)); | ||
| return stack.replace(fromA,to); | ||
| return stack.replace(fromA,to) | ||
| }; | ||
@@ -66,10 +63,6 @@ | ||
| const getReplacers=function(currentName,newName){ | ||
| return[ | ||
| const getReplacers=(currentName,newName)=>[ | ||
| [`\n${currentName}: `,`\n${newName}: `], | ||
| [`${currentName}: `,`${newName}: `], | ||
| [`${currentName} `,`${newName} `], | ||
| [currentName,newName]]; | ||
| }; | ||
| //# sourceMappingURL=stack.js.map | ||
| [currentName,newName]]; |
+6
-7
| { | ||
| "name": "set-error-class", | ||
| "version": "1.6.0", | ||
| "version": "2.0.0", | ||
| "type": "module", | ||
@@ -30,15 +30,14 @@ "exports": { | ||
| "directories": { | ||
| "lib": "src", | ||
| "test": "test" | ||
| "lib": "src" | ||
| }, | ||
| "dependencies": { | ||
| "normalize-exception": "^2.11.0" | ||
| "normalize-exception": "^3.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@ehmicky/dev-tasks": "^2.0.52", | ||
| "test-each": "^5.6.0" | ||
| "@ehmicky/dev-tasks": "^2.0.80", | ||
| "test-each": "^5.7.1" | ||
| }, | ||
| "engines": { | ||
| "node": ">=14.18.0" | ||
| "node": ">=16.17.0" | ||
| } | ||
| } |
+6
-3
@@ -43,7 +43,10 @@ [](https://www.npmjs.com/package/set-error-class) | ||
| This package works in both Node.js >=14.18.0 and | ||
| This package works in both Node.js >=16.17.0 and | ||
| [browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/browserslist). | ||
| It is an ES module and must be loaded using | ||
| This is an ES module. It must be loaded using | ||
| [an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c), | ||
| not `require()`. | ||
| not `require()`. If TypeScript is used, it must be configured to | ||
| [output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html), | ||
| not CommonJS. | ||
@@ -50,0 +53,0 @@ # API |
149
2.05%13899
-2.24%150
-13.79%+ Added
+ Added
- Removed
- Removed
Updated