set-error-message
Advanced tools
Comparing version
export const normalizeArgs=function( | ||
export const normalizeArgs=( | ||
error, | ||
newMessage, | ||
currentMessage=error.message) | ||
currentMessage=error.message)=> | ||
{ | ||
if(typeof newMessage!=="string"){ | ||
throw new TypeError(`newMessage must be a string: ${newMessage}`); | ||
throw new TypeError(`newMessage must be a string: ${newMessage}`) | ||
} | ||
if(typeof currentMessage!=="string"){ | ||
throw new TypeError(`currentMessage must be a string: ${currentMessage}`); | ||
throw new TypeError(`currentMessage must be a string: ${currentMessage}`) | ||
} | ||
return currentMessage; | ||
}; | ||
//# sourceMappingURL=args.js.map | ||
return currentMessage | ||
}; |
@@ -7,3 +7,3 @@ import normalizeException from"normalize-exception"; | ||
export default function setErrorMessage(error,newMessage,currentMessage){ | ||
const setErrorMessage=(error,newMessage,currentMessage)=>{ | ||
const errorA=normalizeException(error); | ||
@@ -13,20 +13,22 @@ const currentMessageA=normalizeArgs(errorA,newMessage,currentMessage); | ||
updateStack(errorA,newMessage,currentMessageA); | ||
return errorA; | ||
} | ||
return errorA | ||
}; | ||
export default setErrorMessage; | ||
const updateStack=function(error,newMessage,currentMessage){ | ||
const updateStack=(error,newMessage,currentMessage)=>{ | ||
if(newMessage===currentMessage||!stackIncludesMessage()){ | ||
return; | ||
return | ||
} | ||
const stack=getStack(error,newMessage,currentMessage); | ||
setNonEnumProp(error,"stack",stack); | ||
setNonEnumProp(error,"stack",stack) | ||
}; | ||
const stackIncludesMessage=function(){ | ||
const stackIncludesMessage=()=>{ | ||
const{stack}=new Error(EXAMPLE_MESSAGE); | ||
return typeof stack==="string"&&stack.includes(EXAMPLE_MESSAGE); | ||
return typeof stack==="string"&&stack.includes(EXAMPLE_MESSAGE) | ||
}; | ||
@@ -36,3 +38,3 @@ | ||
const setNonEnumProp=function(error,propName,value){ | ||
const setNonEnumProp=(error,propName,value)=>{ | ||
@@ -43,5 +45,4 @@ Object.defineProperty(error,propName,{ | ||
writable:true, | ||
configurable:true}); | ||
}; | ||
//# sourceMappingURL=main.js.map | ||
configurable:true | ||
}) | ||
}; |
@@ -8,14 +8,13 @@ | ||
export const getStack=function({name,stack},newMessage,currentMessage){ | ||
return currentMessage!==""&&stack.includes(currentMessage)? | ||
export const getStack=({name,stack},newMessage,currentMessage)=> | ||
currentMessage!==""&&stack.includes(currentMessage)? | ||
replaceMessage({name,stack,newMessage,currentMessage}): | ||
insertMessage(name,stack,newMessage); | ||
}; | ||
const replaceMessage=function({name,stack,newMessage,currentMessage}){ | ||
const replaceMessage=({name,stack,newMessage,currentMessage})=>{ | ||
const replacers=getReplacers(name,newMessage,currentMessage); | ||
const[fromA,to]=replacers.find(([from])=>stack.includes(from)); | ||
return stack.replace(fromA,to); | ||
return stack.replace(fromA,to) | ||
}; | ||
@@ -25,4 +24,3 @@ | ||
const getReplacers=function(name,newMessage,currentMessage){ | ||
return[ | ||
const getReplacers=(name,newMessage,currentMessage)=>[ | ||
[`${name}: ${currentMessage}`,`${name}: ${newMessage}`], | ||
@@ -34,5 +32,4 @@ [`: ${currentMessage}`,`: ${newMessage}`], | ||
}; | ||
const insertMessage=function(name,stack,newMessage){ | ||
const insertMessage=(name,stack,newMessage)=>{ | ||
const nameAndColon=`${name}: `; | ||
@@ -42,3 +39,3 @@ const newMessageA=newMessage.trimEnd(); | ||
if(stack===name||stack.startsWith(`${name}\n`)){ | ||
return stack.replace(name,`${nameAndColon}${newMessageA}`); | ||
return stack.replace(name,`${nameAndColon}${newMessageA}`) | ||
} | ||
@@ -48,4 +45,3 @@ | ||
stack.replace(nameAndColon,`${nameAndColon}${newMessageA}\n`): | ||
`${nameAndColon}${newMessageA}\n${stack}`; | ||
}; | ||
//# sourceMappingURL=stack.js.map | ||
`${nameAndColon}${newMessageA}\n${stack}` | ||
}; |
{ | ||
"name": "set-error-message", | ||
"version": "1.6.0", | ||
"version": "2.0.0", | ||
"type": "module", | ||
"exports": { | ||
"types": "./build/types/main.d.ts", | ||
"types": "./build/src/main.d.ts", | ||
"default": "./build/src/main.js" | ||
}, | ||
"main": "./build/src/main.js", | ||
"types": "./build/types/main.d.ts", | ||
"types": "./build/src/main.d.ts", | ||
"files": [ | ||
"build/src/**/*.{js,json}", | ||
"build/types/**/*.d.ts" | ||
"build/src/**/*.{js,json,d.ts}", | ||
"!build/src/**/*.test.js", | ||
"!build/src/{helpers,fixtures}" | ||
], | ||
@@ -50,4 +51,3 @@ "sideEffects": false, | ||
"directories": { | ||
"lib": "src", | ||
"test": "test" | ||
"lib": "src" | ||
}, | ||
@@ -58,8 +58,8 @@ "dependencies": { | ||
"devDependencies": { | ||
"@ehmicky/dev-tasks": "^1.0.102", | ||
"test-each": "^5.6.0" | ||
"@ehmicky/dev-tasks": "^2.0.80", | ||
"test-each": "^6.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=14.18.0" | ||
"node": ">=16.17.0" | ||
} | ||
} |
[](https://www.npmjs.com/package/set-error-message) | ||
[](https://unpkg.com/set-error-message?module) | ||
[](/types/main.d.ts) | ||
[](/src/main.d.ts) | ||
[](https://codecov.io/gh/ehmicky/set-error-message) | ||
@@ -55,7 +55,10 @@ [](https://bundlephobia.com/package/set-error-message) | ||
This package works in both Node.js >=14.18.0 and | ||
[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/tasks/build/browserslist). | ||
It is an ES module and must be loaded using | ||
This package works in both Node.js >=16.17.0 and | ||
[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/browserslist). | ||
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. | ||
@@ -76,4 +79,4 @@ # API | ||
- [`modern-errors`](https://github.com/ehmicky/modern-errors): Handle errors | ||
like it's 2022 🔮 | ||
- [`modern-errors`](https://github.com/ehmicky/modern-errors): Handle errors in | ||
a simple, stable, consistent way | ||
- [`error-custom-class`](https://github.com/ehmicky/error-custom-class): Create | ||
@@ -93,2 +96,4 @@ one error class | ||
update an error's class | ||
- [`wrap-error-message`](https://github.com/ehmicky/wrap-error-message): | ||
Properly wrap an error's message | ||
- [`set-error-props`](https://github.com/ehmicky/set-error-props): Properly | ||
@@ -104,2 +109,6 @@ update an error's properties | ||
some ❤ to Node.js process errors | ||
- [`error-http-response`](https://github.com/ehmicky/error-http-response): | ||
Create HTTP error responses | ||
- [`winston-error-format`](https://github.com/ehmicky/winston-error-format): Log | ||
errors with Winston | ||
@@ -106,0 +115,0 @@ # Support |
11440
2.89%138
6.98%88
-4.35%