summaryrefslogtreecommitdiff
path: root/src/misc/errors.ts
blob: ec12b6a487be1a404078c6d1b1a184dc31f431b7 (plain)
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
export const DOES_NOT_SUPPORT_FETCH = 0;

export class YacdError extends Error {
  constructor(public message: string, public code?: string | number) {
    super(message);
    Error.captureStackTrace(this, this.constructor);
  }
}

export const errors = {
  [DOES_NOT_SUPPORT_FETCH]: {
    message: 'Browser not supported!',
    detail: 'This browser does not support "fetch", please choose another one.',
  },
  default: {
    message: 'Oops, something went wrong!',
  },
};

export type Err = { code: number };

export function deriveMessageFromError(err: Err) {
  const { code } = err;
  if (typeof code === 'number') {
    return errors[code];
  }
  return errors.default;
}