| 21 | } |
| 22 | |
| 23 | const Err = (name: string, Class: IHydroError, ...info: Array<(() => string) | string | number>) => { |
| 24 | let msg: () => string; |
| 25 | let code: number; |
| 26 | for (const item of info) { |
| 27 | if (typeof item === 'number') { |
| 28 | code = item; |
| 29 | } else if (typeof item === 'string') { |
| 30 | msg = function () { return item; }; |
| 31 | } else if (typeof item === 'function') { |
| 32 | msg = item; |
| 33 | } |
| 34 | } |
| 35 | // eslint-disable-next-line ts/no-shadow |
| 36 | return class HydroError extends Class { |
| 37 | name = name; |
| 38 | constructor(...args: any[]) { |
| 39 | super(...args); |
| 40 | if (msg) this.msg = msg; |
| 41 | if (code) this.code = code; |
| 42 | } |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | export const UserFacingError = Err('UserFacingError', HydroError, 'UserFacingError', 400); |
| 47 | export const SystemError = Err('SystemError', HydroError, 'SystemError', 500); |