eletrotupi / tcc/ api/src/lib/errors/base.ts4e8e478f6d7cd313e333b3ca6793334f4c19c905
552 B | 14 lines Raw
1
// The shared shape all domain errors conform to.
2
// The type guard solves the `unknown` catch problem everywhere.
3

4
export class DomainError extends Error {
5
  constructor(public readonly message: string, public readonly status: number) {
6
    super(message);
7
    this.name = this.constructor.name; // makes logs say "DuplicateEmailError" not "Error"
8
    Object.setPrototypeOf(this, new.target.prototype); // fixes instanceof after transpile
9
  }
10
}
11

12
export function isDomainError(err: unknown): err is DomainError {
13
  return err instanceof DomainError;
14
}