Extended Errors do not have message or stack trace

后端 未结 2 771
时光说笑
时光说笑 2020-12-10 13:27

When running this snippet through BabelJS:

class FooError extends Error {
  constructor(message) {
    super(message);
  }
}

let error = new FooError(\'foo\         


        
2条回答
  •  时光说笑
    2020-12-10 13:59

    This is a limitation due to the downlevel compilation of ES6 to ES5. Find more about this in TypeScript's explanation.

    As a workaround you can do:

    class QueryLimitError extends Error {
      __proto__: QueryLimitError;
    
      constructor(message) {
        const trueProto = new.target.prototype;
        super(message);
        this.__proto__ = trueProto;
      }
    }
    

    In some cases cases Object.setPrototypeOf(this, FooError.prototype); might be enough.

    You'll find more information in the corresponding Github issue and the

提交回复
热议问题