I'm trying to personalize my error classes inside the application.
I have totally four classes; Exception, GenericError, BaseService, CarService two main and two inherited. GenericError extends ExceptionClass and CarService extends BaseService. I'm not be able to change constructor property of GenericError class. I'm getting this error;
This expression is not constructable.
What is wrong with my class inheritence logic?
// Exception.ts
class ExceptionClass extends Error {
status: number;
message: string;
errors?: object;
constructor(status: number, message: string, errors?: object) {
super(message);
this.status = status;
this.message = message;
this.errors = errors;
}
}
// GenericError.ts
class GenericException extends ExceptionClass {
constructor(message?: string) {
super(400, message || 'Internal Error');
}
}
// BaseService.ts
class BaseService {
genericError: GenericException;
constructor(message: string) {
this.genericError = new GenericException();
}
}
// CarService.ts
export default class CarService extends BaseService {
constructor() {
super('')
console.log("CarService started!");
}
async checkCar() {
return new Promise(async (resolve: any, reject: any) => {
//** Imagine some business logic that fails */
const passwordsNotMatchErr = new this.genericError('Fail Reason bla bla');
return reject(passwordsNotMatchErr);
})
}
}