cal.pub0.org/packages/lib/redactError.ts

27 lines
815 B
TypeScript
Raw Normal View History

2023-08-03 15:22:38 +00:00
import { Prisma } from "@prisma/client";
import logger from "@calcom/lib/logger";
const log = logger.getChildLogger({ prefix: [`[[redactError]`] });
function shouldRedact<T extends Error>(error: T) {
return (
error instanceof Prisma.PrismaClientInitializationError ||
error instanceof Prisma.PrismaClientKnownRequestError ||
error instanceof Prisma.PrismaClientUnknownRequestError ||
error instanceof Prisma.PrismaClientValidationError
);
}
export const redactError = <T extends Error | unknown>(error: T) => {
if (!(error instanceof Error)) {
return error;
}
log.debug("Type of Error: ", error.constructor);
if (shouldRedact(error)) {
log.error("Error: ", JSON.stringify(error));
return new Error("An error occured while querying the database.");
}
return error;
};