aboutsummaryrefslogtreecommitdiff
path: root/apps/backend/src/errors/baseError.ts
blob: bccc54df5d2f9c8cbbed15d024381250fe3e4c3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
export class BaseHttpError extends Error {
    public status: number;
    public message: string;
  
    constructor(status: number, message: string) {
      super(message);
      this.status = status;
      this.message = message;
      Object.setPrototypeOf(this, new.target.prototype); // Restore prototype chain
    }   
  }
  
  
  export class BaseError extends Error {
    type: string;
    message: string;
    source: string;
    ignoreLog: boolean;
  
    constructor(
      type: string,
      message?: string,
      source?: string,
      ignoreLog = false
    ) {
      super();
  
      Object.setPrototypeOf(this, new.target.prototype);
  
      this.type = type;
      this.message =
        message ??
        "An unknown error occurred. If this persists, please contact us.";
      this.source = source ?? "unspecified";
      this.ignoreLog = ignoreLog;
    }
  
    toJSON(): Record<PropertyKey, string> {
      return {
        type: this.type,
        message: this.message,
        source: this.source,
      };
    }
  }