admin管理员组文章数量:1335371
in a scenario where I have multiple (nested, composed) functions that, with try catch blocks are rising exceptions, bubbling up the error from bottom up, I'd like to understand if there's a a way already included with NodeJs functionalities to achieve the possibility to clearly carry up with the final error all the errors instances raised, and the associated error payload.
So to be more explicit here's a code example: if I have a chain of throws from Error classes custom defined that are holding error related data:
class MyError extends Error {
cause: unknown;
constructor(message:string, cause?:unknown) {
super(message);
this.name = 'MyError';
this.cause = cause;
}
}
class InvalidParameterError extends MyError {
data: unknown;
constructor(message:string, data:unknown, cause?:unknown) {
super(message, cause);
this.name = 'InvalidParameterError';
this.data = data;
}
}
class InvalidRequestError extends MyError {
request: unknown;
constructor(message:string, request:unknown, cause?:unknown) {
super(message, cause);
this.name = 'InvalidRequestError';
this.request = request;
}
}
try {
try{
throw new InvalidParameterError('errorMsg foo bar', {parameterInput: 'abc'})
}catch(error){
throw new InvalidRequestError('request sent is invalid', { requestObject: {foo: 'bar'} }, error)
}
}catch(error){
console.log(error) // here std nodejs stack trace is not providing all needed info
}
The above will out something that is going to be useful (but I have to withHold some custom made lib):
InvalidRequestError: request sent is invalid
at Object.<anonymous> ([...]/test.ts:33:14)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
... 7 lines matching cause stack trace ...
at bootstrap ([...]/node_modules/ts-node/src/bin.ts:95:10) {
cause: InvalidParameterError: errorMsg foo bar
at Object.<anonymous> ([...]/test.ts:31:14)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Module.m._compile ([...]/node_modules/ts-node/src/index.ts:1618:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
at Object.require.extensions.<computed> [as .ts] ([...]/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Function.Module._load (node:internal/modules/cjs/loader:1019:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
at phase4 ([...]/node_modules/ts-node/src/bin.ts:649:14)
at bootstrap ([...]/node_modules/ts-node/src/bin.ts:95:10) {
cause: undefined,
data: { parameterInput: 'abc' }
},
request: { requestObject: { foo: 'bar' } }
}
I see that Nodejs 23 is supporting
new Error('message error', { cause });
which is something similar, but it doesn't seem to work properly to me.
I'd like to know if there are better practice for this or if there's some npm package that is widely supported and implementing a more advanced Error handling/debugging....
本文标签: nodejsNodejs trycatch how we are supposed to implement error causeStack Overflow
版权声明:本文标题:node.js - Nodejs trycatch how we are supposed to implement error cause? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374881a2462963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论