mic-bot/utils/errors.ts

57 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-10-19 10:00:35 +00:00
export enum ERR_CODES {
// General
UnknownErr,
Unimplemented,
Other,
// System/Timeout errors
Canceled,
Timeout,
RemoteServiceErr,
OutOfMemory,
// Validation/Input errors
InvalidAction,
BadConfig,
InvalidArgument,
OutOfRange,
// Access errors
PermissionDenied,
Unauthenticated,
// Entity errors
EntityExists,
EntityNotFound,
Outdated,
// Not error by itself, but adds some additional
// information to the error in error stack
ErrClarification,
};
export const getErrCode = (name: keyof typeof ERR_CODES): ERR_CODES | undefined => {
return ERR_CODES[name];
}
export abstract class Err extends Error {
abstract code: ERR_CODES;
get_name() { return this.constructor.name };
}
export const getErrStack = (err: Error): Error[] => {
const errStack: Error[] = []
let thisErr: Error = err
while (thisErr.cause) {
errStack.push(thisErr)
if (thisErr.cause instanceof Error)
thisErr = thisErr.cause
}
return errStack
}
// ===============================
// == Some useful error classes ==
// ===============================
export class BadConfigErr extends Err { code = ERR_CODES.BadConfig }