Skip to content

ddd-ts-kit/errors-constructor-not-public

Architecture id: errors/constructor-not-public. Default severity: error in recommended, error in strict.

Applies to: the production files of domain/errors/ of a context, in a project whose config says "stack.refusals": "factories".

A public constructor lets any caller build a refusal with a code and a message of its own. ConceptError of @ddd-ts-kit/kernel has a protected constructor, and a class that extends it declares none: a refusal then exists only through a static factory.

  • a class that extends another one and declares a public constructor
  • a class that extends DomainError and declares no constructor: it inherits a public one

An abstract class is not judged.

Incorrect:

export class SlotTakenError extends DomainError {
readonly code = 'SLOT_TAKEN';
constructor(calendarId: string) {
super(`The slot on calendar ${calendarId} is already held.`);
}
}

Correct:

export class AppointmentError extends ConceptError {
static slotTaken(calendarId: string) {
return AppointmentError.refusal('SLOT_TAKEN', `The slot on calendar ${calendarId} is already held.`, { calendarId });
}
}

None.

rules."errors/constructor-not-public" in ddd-ts-kit.config.json set to off silences the rule, and // ddd-ts-kit-disable-next-line errors/constructor-not-public -- <reason> silences one line, exactly as for ddd-ts-kit check.

  • A class that extends a base of the project and declares no constructor is trusted: the visibility it inherits is in another file.
  • Nothing is fixed: making a constructor private breaks every new of the class. ddd-ts-kit error-factories moves a project to factories as a whole.

Held by this plugin alone: the shape of a class needs a syntax tree.