express-mongoose-es6-rest-api icon indicating copy to clipboard operation
express-mongoose-es6-rest-api copied to clipboard

Error type is always 'Error'

Open odieatla opened this issue 9 years ago • 2 comments

Line 67 of code file ./config/express.js:

const err = new APIError('API not found', httpStatus.NOT_FOUND);

put following code after the above:

console.log(err instanceof APIError); // output false

To my understanding, the output should be true, since err is an instance of APIError?

node version 6.2.0

Thanks,

odieatla avatar Jun 14 '16 00:06 odieatla

I just ran into this as well.

https://stackoverflow.com/questions/33832646/extending-built-in-natives-in-es6-with-babel https://stackoverflow.com/questions/31089801/extending-error-in-javascript-with-es6-syntax

It's a problem with Babel. Node 6 and 7 by themselves allow you to extend Error without a problem.

hueter avatar Nov 28 '16 23:11 hueter

Running into this as well.

It's a problem with Babel. Node 6 and 7 by themselves allow you to extend Error without a problem.

So doesn't this make the following lines useless:

https://github.com/KunalKapadia/express-mongoose-es6-rest-api/blob/490b2850f36589ee31c3504c0c1cfabbec664e4f/config/express.js#L59-L61

If I understand correctly, err instanceof APIError will always return false as APIError has been converted into a function by Babel:

function APIError(message) {
    var status = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _httpStatus2.default.INTERNAL_SERVER_ERROR;
    var isPublic = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;

    _classCallCheck(this, APIError);

    return _possibleConstructorReturn(this, (APIError.__proto__ || Object.getPrototypeOf(APIError)).call(this, message, status, isPublic));
  }

I'm vurious why nobody else is having this issue. Do people either not notice or am I doing something wrong? I'm assuming the later is true.


Adding the following to package.json does the trick for me:

  "devDependencies": {
    // ...
    "babel-plugin-transform-builtin-extend": "^1.1.2",
    // ...
  },
  "babel": {
    // ...
    "plugins": [
      "add-module-exports",
      [
        "babel-plugin-transform-builtin-extend",
        {
          "globals": [
            "Error",
            "Array"
          ]
        }
      ]
    ]
    // ...

alukach avatar Nov 13 '17 23:11 alukach