admin管理员组

文章数量:1278983

I created some custom Erros in my application and I wanted to check for them later using the constructor name. The problem is that when I extend Error in my classes, the constructor.name is always "Error", not the name I actually gave to it.

I was doing some tests and noticed that this happens with the Error class, but not with any other custom class I create. e.g.:

class CustomClass {
  constructor(msg) {}
}

class OtherClass extends CustomClass {
   constructor(msg) {
     super(msg);
}

class CustomError extends Error {
  constructor(msg) {
    super(msg);
  }
}

const e = new CustomError("There was an error");
const otherClass = new OtherClass("This is a class");

console.log(otherClass.constructor.name); // "OtherClass" <- ok!
console.log(e.constructor.name); // "Error" <- not ok! expected "CustomError"

Does anyone knows why this happens?

I thought I could do something like:

class CustomError extends Error {
  constructor(msg) {
    super(msg);
  }

  getName() {
    return "CustomError";
  }
}

const e = new CustomError("There was an error");
if(e.getName() == "CustomError") {
  // do stuff
}

But then I get: TypeError: e.getName is not a function

  • Does anyone know if there is a way to override the constructor name when extending Error?
  • Also, why can't I declare and call methods in my CustomError error class?

EDIT

Following @samanime suggestion, I updated my node version to 8.8.1 and was able to find a partial solution.

Changing the syntax a little bit I got:

const FileSystemException = module.exports = class FileSystemException extends Error {
    constructor(msg) {
        super(msg);
    }

    getName() {
        return "FileSystemException";
    }
}

const e = new FileSystemException("There was an error");

// Running node app.js
console.log(e.constructor.name); // "FileSystemException"
console.log(e.getName()); // "FileSystemException"

// Running babel-node app.js
console.log(e.constructor.name); // "Error"
console.log(e.getName()); // "TypeError: e.getName is not a function"

Still, it would be awesome if someone could make it work with babel so I can use import/export statements without having to wait for node v9.4 LTS.

Using:

  • node v8.8.1

  • babel-node v6.26.0 w/ "es2015" and "stage-0" presets

thanks!

I created some custom Erros in my application and I wanted to check for them later using the constructor name. The problem is that when I extend Error in my classes, the constructor.name is always "Error", not the name I actually gave to it.

I was doing some tests and noticed that this happens with the Error class, but not with any other custom class I create. e.g.:

class CustomClass {
  constructor(msg) {}
}

class OtherClass extends CustomClass {
   constructor(msg) {
     super(msg);
}

class CustomError extends Error {
  constructor(msg) {
    super(msg);
  }
}

const e = new CustomError("There was an error");
const otherClass = new OtherClass("This is a class");

console.log(otherClass.constructor.name); // "OtherClass" <- ok!
console.log(e.constructor.name); // "Error" <- not ok! expected "CustomError"

Does anyone knows why this happens?

I thought I could do something like:

class CustomError extends Error {
  constructor(msg) {
    super(msg);
  }

  getName() {
    return "CustomError";
  }
}

const e = new CustomError("There was an error");
if(e.getName() == "CustomError") {
  // do stuff
}

But then I get: TypeError: e.getName is not a function

  • Does anyone know if there is a way to override the constructor name when extending Error?
  • Also, why can't I declare and call methods in my CustomError error class?

EDIT

Following @samanime suggestion, I updated my node version to 8.8.1 and was able to find a partial solution.

Changing the syntax a little bit I got:

const FileSystemException = module.exports = class FileSystemException extends Error {
    constructor(msg) {
        super(msg);
    }

    getName() {
        return "FileSystemException";
    }
}

const e = new FileSystemException("There was an error");

// Running node app.js
console.log(e.constructor.name); // "FileSystemException"
console.log(e.getName()); // "FileSystemException"

// Running babel-node app.js
console.log(e.constructor.name); // "Error"
console.log(e.getName()); // "TypeError: e.getName is not a function"

Still, it would be awesome if someone could make it work with babel so I can use import/export statements without having to wait for node v9.4 LTS.

Using:

  • node v8.8.1

  • babel-node v6.26.0 w/ "es2015" and "stage-0" presets

thanks!

Share Improve this question edited Feb 25, 2018 at 15:16 matisetorm 8538 silver badges21 bronze badges asked Jan 24, 2018 at 14:46 Thiago LoddiThiago Loddi 2,3405 gold badges23 silver badges36 bronze badges 1
  • its working fine in mine – zabusa Commented Jan 24, 2018 at 14:54
Add a ment  | 

3 Answers 3

Reset to default 7

It seems to work just fine in this simple example:

class CustomError extends Error {
 constructor(msg) { 
   super(msg);
 }
}

const error = new CustomError()
console.log(error.constructor.name);
console.log(error instanceof CustomError);
console.log(error instanceof Error);

That is running with native class support (in Chrome).

It is possible that it isn't an issue with your syntax, but an issue with your transpiler. You probably want to dig through the transpiled code itself and see if it is doing anything special with Error that it doesn't with normal classes.

The fact that your getName() example didn't work also seems to indicate something funky is going on. Your examples you've posted look good. Double-check the code you are trying to run actually matches them.

A simple solution could be as follow

class CustomError extends Error {
  constructor(msg) {
    super(msg);
  }
  
  get name(){
    return 'CustomError'
  }
}

const e = new CustomError("There was an error");

console.log(e.constructor.name); // "CustomError"
console.log(e.name); // "CustomError"

This will also change the error name in the stack trace so when you throw your custom error object you will see: CustomError: some random stack trace

Instead of: Error: some random stack trace

For more information about using 'get' in js classes/objects, you can take a look at https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Functions/get

Hope I am not too late to the party and that you find this helpful.

To make error.name correctly set, you can create a base class for all your custom errors.

class MyAbstractError extends Error {
    constructor(message) { 
       super(message);
       this.name = this.constructor.name;
    }
}

class NotFoundError extends MyAbstractError {}
class BadParameterError extends MyAbstractError {}

console.log( (new BadParameter("x")).name) // => "BadParameter"
console.log( (new NotFoundError ("x")).name) // => "NotFoundError"

Or you can simply add a name() getter in your base class that returns this.constructor.name

本文标签: nodejsJavaScriptHow to get child class name when extending ErrorStack Overflow