1

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.

TS Snippet

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);
    })
  }
}
1
  • 2
    Just a heads up, marking a function async and then returning a promise is pointless. Commented Oct 10, 2020 at 22:32

1 Answer 1

3

The problem is that in the following line:

const passwordsNotMatchErr = new this.genericError('Fail Reason bla bla');

You are trying to create an instance of a variable that has been instantiated in the constructor of your class. You are trying to do something like const myString = new "I am a string and I can not be instantiated"

When you do:

genericError: GenericException;

What you are doing is defining a class variable which is of GenericExpection type (Remember that only classes can be instantiated, not variables). If you'd like to follow your architecture you can do something like:

  async checkCar() {
    return new Promise(async (resolve: any, reject: any) => {
      //** Imagine some business logic that fails */
      this.genericError = new GenericException('Fail Reason bla bla');
      return reject(this.genericError);
    })
  }

or

  async checkCar() {
    return new Promise(async (resolve: any, reject: any) => {
      //** Imagine some business logic that fails */
      const passwordsNotMatchErr = new GenericException('Fail Reason bla bla');
      return reject(passwordsNotMatchErr);
    })
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I know I can import GenericException class on the top of the service file and use it as you suggested. But I think you are missing my point. I'm trying to gather all exceptions in class and I want to use them in extended classes with this.. If I do as you suggested I have to import genericException class in every file, haven't I?
I see what you want to do... What about creating a helper method in BaseService class that returns a new instantiate of GenericException? This you will only need to import GenericException on the BaseService class and just interact with the helper method. If this doesn't makes sense just let me know and I can update my answer with an example @Capan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.