eletrotupi / tcc/ api/src/lib/errors/base.ts6bdc28a3adcc45cf37288df8777dfa536ce0f07a
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
}