最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Nodejs trycatch how we are supposed to implement error cause? - Stack Overflow

programmeradmin0浏览0评论

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....

发布评论

评论列表(0)

  1. 暂无评论