自定义错误类未正确扩展

时间:2018-03-01 04:17:19

标签: ecmascript-6 javascript-inheritance

我有几个自定义错误类

GenericError.js

class Factory
{
     Dictionary test = new Dictionary(string, ICreate);

     public FactoryMethod()
     {
        test.Add("classA",new a());
        test.Add("classB",new b());
        test.Add("classC",new c());
     }

     public ICreate Create(string toMatch)
     {
        return test[toMatch];  
     }
}

NotFoundError.js

import { errorCodes, errorMessages } from './errorConstants';

class GenericError extends Error {
    constructor(message = errorMessages.UNKNOWN, code = errorCodes.UNKNOWN, description) {
      super();
      Error.captureStackTrace(this, this.constructor);
      this.name = this.constructor.name;
      this.message = message;
      this.code = code;
      this.description = description;
    }

    stringify() {
        return JSON.stringify({
            error : {
                message: this.message || "Unknown Error.",
                code: this.code,
                description: this.description
            },
        })
    }
}

export default GenericError;

但是,当我使用此功能时

import GenericError from "./GenericError";
import {errorMessages, errorCodes} from "./errorConstants";

class NotFoundError extends GenericError {
    constructor(message = errorMessages.NOT_FOUND, code = errorCodes.NOT_FOUND, description = "Not Found.") {
        super(message, code, description);
        this.name = this.constructor.name;
     }

}

export default NotFoundError;

当我抛出新的NotFoundError()时,出于某种原因,error.name是firebaseUtils.getInfo(user) .then((info) => { const infoSnapshot = info || {}; if (//some logic here) { throw new NotFoundError(); } response.send(JSON.stringify(infoSnapshot)); }) .catch((error) => { if(error.name === "NotFoundError") { response.status(error.getCode).send(error.stringify()); return; } }) 而不是Error而stringify不是函数?

有人能告诉我什么错吗?

编辑:从我的代码中删除了打字稿并且它无效!

0 个答案:

没有答案