0

I have a couple of custom error classes

GenericError.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;

However, when I use this function

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


    })

When I throw new NotFoundError(), For some reason, error.name is Error instead of NotFoundError and stringify is not a function?

Can anybody tell me whats wrong?

EDIT: Removed typescript from my code and its not working!

ylin6
  • 63
  • 4
  • can you rename `PidNotFoundError ` to `NotFoundError`, or are they different, if they are different, where is `NotFoundError` class? – Bk Santiago Mar 01 '18 at 04:19
  • sorry that was a typo, ill edit – ylin6 Mar 01 '18 at 04:22
  • Looks like its an issue compiling es6 down to es5. Had to use this hack to get it to work https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801 – ylin6 Mar 01 '18 at 15:35

0 Answers0