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!