0

我有几个自定义错误类

通用错误.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;

NotFoundError.js

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;

但是,当我使用此功能时

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;
        }


    })

当我抛出新的 NotFoundError() 时,由于某种原因,error.name 是Error而不是NotFoundErrorstringify 不是一个函数?

谁能告诉我怎么了?

编辑:从我的代码中删除了打字稿,但它不起作用!

4

0 回答 0