export class InvalidArg extends Error {
constructor(
message: string,
readonly argName?: string,
readonly actual?: unknown,
readonly expected?: unknown) {
super(message);
}
static expected(
argName: string,
actual: unknown,
expected: unknown,
additionalDetails = ''
): Error {
let exp;
let act;
if (typeof actual === 'object') {
act = actual?.toString() || null;
} else {
act = actual;
}
if (typeof expected === 'object') {
exp = expected?.toString() || null;
} else {
exp = expected;
}
const msg = `Invalid argument "${argName}! Expected "${exp}" but was "${act}! ${additionalDetails}"`
return new InvalidArg(
msg,
argName,
actual,
expected
);
}
} |