1

Hello I have a base class, and I have other classes that extend this base class:

types and interfaces:

export type fooBar = {
  foo: string;
  bar: string;
};
/*UnprocessableEntityError */
export type missing_fields_error = {
  field: string;
  code: string;
};
/*Default Errors */
export type DefaultErrors = {
  type?: string;
  message?: string;
  code?: number;
  errors?: missing_fields_error[] | fooBar[];
  internalData?: object;
  options?: {
    showPath?: boolean;
    showLocations?: boolean;
  };
};

base class:

export class BaseError extends ExtendableError implements DefaultErrors {
  type: string;
  message: string;
  code: number;
  errors?: missing_fields_error[] | fooBar[];
  internalData: object;
  path: any;
  locations: any;
  _showLocations: boolean = false;
  _showPath: boolean = false;

  constructor(args: DefaultErrors) {
    super(args.message || '');
    const type = args.type;
    const message = args.message;
    const code = args.code;
    const internalData = args.internalData;
    const options = args.options;

    this.type = type;
    this.message = message;
    this.code = code;
    this.internalData = internalData;
    this._showLocations = !options.showLocations;
    this._showPath = !options.showPath;
  }

  serialize(): DefaultErrors {
    const { type, message, code, errors } = this;

    let error: DefaultErrors = {
      type,
      message,
      code,
    };

    return error;
  }
}

sub class:

/* Unprocessable Entity Error */
export class UnprocessableEntityERROR extends BaseError {
  constructor(errors: missing_fields_error[]) {
    super();
    this.type = 'Unprocessable Entity';
    this.message = 'Validation Failed';
    this.code = 422;
  }
}

but i got this error on my super()

BaseError.ts(41, 15): An argument for 'args' was not provided.

basically i have a class with defaults data and other classes that use this defaults data and have more attributes / types / interface I don't know if I have the right logic, if someone can give me a light with that

2 Answers 2

2

In your call to BaseErrors constructor using super(), you did not specify the error, basically change your constructor to something like:

/* Unprocessable Entity Error */
export class UnprocessableEntityERROR extends BaseError {
  constructor(errors: missing_fields_error[]) {
    super({
      type: 'Unprocessable Entity',
      message: 'Validation Failed',
      code: 422,
      errors
    });
  }
}

The interfaces (see comments for details):

export interface fooBar {
  foo: string;
  bar: string;
};

/*UnprocessableEntityError */
export interface missing_fields_error {
  field: string;
  code: string;
};

/*Default Errors */
export interface DefaultErrors {
  type?: string;
  message?: string;
  code?: number;
  errors?: missing_fields_error[] | fooBar[];
  internalData?: object;
  options?: {
    showPath?: boolean;
    showLocations?: boolean;
  };
};

interface fluxFooBar extends fooBar { // This is where it gets interesting since foo and bar are also members
  flux: number;
}
/*
  This is equivalent to:
  type fluxFooBar = {
    foo: string;
    bar: string;
    flux: number;
  }
*/
Sign up to request clarification or add additional context in comments.

4 Comments

I think I understand, do you believe that the way I did it is correct? is there any way to make the code smaller / maxized
Some things I would do are make your types interfaces, makes it a little easier to extend, etc. Also you can just set type, message, and code in the super call, which I missed, updated the answer.
could you edit the answer with these improvements so i can try to understand better? if possible i would be very grateful
could you edit what the interfaces and the extension you talked about would look like?
1

The BaseError constructor takes an args parameter, which you have not specified in your super() call.

You can specify a default value for the args parameter to resolve this.

constructor(args: DefaultErrors = ‘’) 

3 Comments

I think I understand, do you believe that the way I did it is correct? is there any way to make the code smaller / maxized
It could definitely be improved, in particular your base class has type, message and code properties, but also hold’s type, message and code of the sub class in internalData, is it intentional to have two sets?
i would give a console.log on internal data for internal games, could you help me how can i improve?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.