Handle exceptions, and prevent crashing from unhandled exceptions

This commit is contained in:
Ajay 2023-07-24 21:25:18 -04:00
parent 4e93a007c2
commit f63fa09605
17 changed files with 358 additions and 275 deletions

View file

@ -10,7 +10,11 @@ async function init() {
process.on("unhandledRejection", (error: any) => {
// eslint-disable-next-line no-console
console.dir(error?.stack);
process.exit(1);
});
process.on("uncaughtExceptions", (error: any) => {
// eslint-disable-next-line no-console
console.dir(error?.stack);
});
try {

View file

@ -4,6 +4,7 @@ import { config } from "../config";
import { Request, Response } from "express";
import { isUserVIP } from "../utils/isUserVIP";
import { HashedUserID } from "../types/user.model";
import { Logger } from "../utils/logger";
interface AddUserAsVIPRequest extends Request {
query: {
@ -34,6 +35,7 @@ export async function addUserAsVIP(req: AddUserAsVIPRequest, res: Response): Pro
// check to see if this user is already a vip
const userIsVIP = await isUserVIP(userID);
try {
if (enabled && !userIsVIP) {
// add them to the vip list
await db.prepare("run", 'INSERT INTO "vipUsers" VALUES(?)', [userID]);
@ -45,4 +47,9 @@ export async function addUserAsVIP(req: AddUserAsVIPRequest, res: Response): Pro
}
return res.sendStatus(200);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
}

View file

@ -6,6 +6,7 @@ import { ActionType, Category, Service, VideoID } from "../types/segments.model"
import { UserID } from "../types/user.model";
import { getService } from "../utils/getService";
import { config } from "../config";
import { Logger } from "../utils/logger";
interface DeleteLockCategoriesRequest extends Request {
body: {
@ -53,7 +54,12 @@ export async function deleteLockCategoriesEndpoint(req: DeleteLockCategoriesRequ
});
}
try {
await deleteLockCategories(videoID, categories, actionTypes, getService(service));
} catch (e) {
Logger.error(e as string);
return res.status(500);
}
return res.status(200).json({ message: `Removed lock categories entries for video ${videoID}` });
}

View file

@ -164,6 +164,7 @@ export default async function dumpDatabase(req: Request, res: Response, showPage
<hr/>
${updateQueued ? `Update queued.` : ``} Last updated: ${lastUpdate ? new Date(lastUpdate).toUTCString() : `Unknown`}`);
} else {
try {
res.send({
dbVersion: await getDbVersion(),
lastUpdated: lastUpdate,
@ -176,6 +177,10 @@ export default async function dumpDatabase(req: Request, res: Response, showPage
};
}),
});
} catch (e) {
Logger.error(e as string);
res.sendStatus(500);
}
}
await queueDump();

View file

@ -25,6 +25,7 @@ let lastFetch: DBStatsData = {
updateExtensionUsers();
export async function getBrandingStats(req: Request, res: Response): Promise<void> {
try {
const row = await getStats();
lastFetch = row;
@ -39,6 +40,10 @@ export async function getBrandingStats(req: Request, res: Response): Promise<voi
titles: row.titles,
thumbnails: row.thumbnails,
});
} catch (e) {
Logger.error(e as string);
res.sendStatus(500);
}
}
async function getStats(): Promise<DBStatsData> {

View file

@ -1,7 +1,9 @@
import { db } from "../databases/databases";
import { Request, Response } from "express";
import { Logger } from "../utils/logger";
export async function getDaysSavedFormatted(req: Request, res: Response): Promise<Response> {
try {
const row = await db.prepare("get", 'SELECT SUM(("endTime" - "startTime") / 60 / 60 / 24 * "views") as "daysSaved" from "sponsorTimes" where "shadowHidden" != 1', []);
if (row !== undefined) {
@ -14,4 +16,8 @@ export async function getDaysSavedFormatted(req: Request, res: Response): Promis
daysSaved: 0
});
}
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);
}
}

View file

@ -1,5 +1,6 @@
import { db } from "../databases/databases";
import { Request, Response } from "express";
import { Logger } from "../utils/logger";
async function generateTopUsersStats(sortBy: string) {
const rows = await db.prepare("all", `SELECT COUNT(distinct "titles"."UUID") as "titleCount", COUNT(distinct "thumbnails"."UUID") as "thumbnailCount", COALESCE("userName", "titles"."userID") as "userName"
@ -36,8 +37,13 @@ export async function getTopBrandingUsers(req: Request, res: Response): Promise<
return res.status(503).send("Disabled for load reasons");
}
try {
const stats = await generateTopUsersStats(sortBy);
//send this result
return res.send(stats);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
}

View file

@ -3,6 +3,7 @@ import { createMemoryCache } from "../utils/createMemoryCache";
import { config } from "../config";
import { Request, Response } from "express";
import { validateCategories } from "../utils/parseParams";
import { Logger } from "../utils/logger";
const MILLISECONDS_IN_MINUTE = 60000;
// eslint-disable-next-line @typescript-eslint/no-misused-promises
@ -74,8 +75,13 @@ export async function getTopCategoryUsers(req: Request, res: Response): Promise<
return res.sendStatus(400);
}
try {
const stats = await getTopCategoryUsersWithCache(sortBy, category);
//send this result
return res.send(stats);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
}

View file

@ -2,6 +2,7 @@ import { db } from "../databases/databases";
import { createMemoryCache } from "../utils/createMemoryCache";
import { config } from "../config";
import { Request, Response } from "express";
import { Logger } from "../utils/logger";
const MILLISECONDS_IN_MINUTE = 60000;
// eslint-disable-next-line @typescript-eslint/no-misused-promises
@ -92,8 +93,13 @@ export async function getTopUsers(req: Request, res: Response): Promise<Response
return res.status(503).send("Disabled for load reasons");
}
try {
const stats = await getTopUsersWithCache(sortBy, categoryStatsEnabled);
//send this result
return res.send(stats);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
}

View file

@ -30,6 +30,7 @@ let lastFetch: DBStatsData = {
updateExtensionUsers();
export async function getTotalStats(req: Request, res: Response): Promise<void> {
try {
const countContributingUsers = Boolean(req.query?.countContributingUsers == "true");
const row = await getStats(countContributingUsers);
lastFetch = row;
@ -55,6 +56,10 @@ export async function getTotalStats(req: Request, res: Response): Promise<void>
updateExtensionUsers();
}
} catch (e) {
Logger.error(e as string);
res.sendStatus(500);
}
}
function getStats(countContributingUsers: boolean): Promise<DBStatsData> {

View file

@ -1,6 +1,7 @@
import { db } from "../databases/databases";
import { Request, Response } from "express";
import { UserID } from "../types/user.model";
import { Logger } from "../utils/logger";
function getFuzzyUserID(userName: string): Promise<{userName: string, userID: UserID }[]> {
// escape [_ % \] to avoid ReDOS
@ -37,6 +38,8 @@ export async function getUserID(req: Request, res: Response): Promise<Response>
// invalid request
return res.sendStatus(400);
}
try {
const results = exactSearch
? await getExactUserID(userName)
: await getFuzzyUserID(userName);
@ -49,4 +52,8 @@ export async function getUserID(req: Request, res: Response): Promise<Response>
} else {
return res.send(results);
}
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
}

View file

@ -205,6 +205,7 @@ async function getUserInfo(req: Request, res: Response): Promise<Response> {
return res.status(400).send("Invalid userID or publicUserID parameter");
}
try {
const responseObj = {} as Record<string, string|SegmentUUID|number>;
for (const property of paramValues) {
responseObj[property] = await dbGetValue(hashedUserID, property);
@ -218,6 +219,10 @@ async function getUserInfo(req: Request, res: Response): Promise<Response> {
}
return res.send(responseObj);
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);
}
}
export async function endpoint(req: Request, res: Response): Promise<Response> {

View file

@ -515,6 +515,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
return;
}
try {
const isVIP = (await isUserVIP(userID));
const isTempVIP = (await isUserTempVIP(userID, videoID));
const rawIP = getIP(req);
@ -545,8 +546,6 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
//hash the ip 5000 times so no one can get it from the database
const hashedIP = await getHashCache(rawIP + config.globalSalt);
try {
//get current time
const timeSubmitted = Date.now();
// const rateLimitCheckResult = checkRateLimit(userID, videoID, service, timeSubmitted, hashedIP);
@ -620,18 +619,18 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
segment: segmentInfo.segment,
});
}
} catch (err) {
Logger.error(err as string);
lock.unlock();
return res.sendStatus(500);
}
for (let i = 0; i < segments.length; i++) {
sendWebhooks(apiVideoDetails, userID, videoID, UUIDs[i], segments[i], service).catch((e) => Logger.error(`call send webhooks ${e}`));
}
lock.unlock();
return res.json(newSegments);
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);
} finally {
lock.unlock();
}
}
// Takes an array of arrays:

View file

@ -42,6 +42,7 @@ export async function postWarning(req: Request, res: Response): Promise<Response
let resultStatus = "";
try {
if (enabled) {
const previousWarning = await db.prepare("get", 'SELECT * FROM "warnings" WHERE "userID" = ? AND "issuerUserID" = ? AND "type" = ?', [userID, issuerUserID, type]) as warningEntry;
@ -91,4 +92,8 @@ export async function postWarning(req: Request, res: Response): Promise<Response
return res.status(200).json({
message: `Warning ${resultStatus} user '${userID}'.`,
});
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
}

View file

@ -34,6 +34,7 @@ export async function setUsername(req: Request, res: Response): Promise<Response
// eslint-disable-next-line no-control-regex
userName = userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");
try {
// check privateID against publicID
if (!await checkPrivateUsername(userName, userID)) {
return res.sendStatus(400);
@ -56,7 +57,6 @@ export async function setUsername(req: Request, res: Response): Promise<Response
timings.push(Date.now());
try {
const row = await db.prepare("get", `SELECT count(*) as "userCount" FROM "userNames" WHERE "userID" = ? AND "locked" = 1`, [userID]);
if (adminUserIDInput === undefined && row.userCount > 0) {
return res.sendStatus(200);

View file

@ -7,6 +7,7 @@ import { UserID } from "../types/user.model";
import { QueryCacher } from "../utils/queryCacher";
import { isUserVIP } from "../utils/isUserVIP";
import { parseCategories, parseDeArrowTypes } from "../utils/parseParams";
import { Logger } from "../utils/logger";
export async function shadowBanUser(req: Request, res: Response): Promise<Response> {
const userID = req.query.userID as UserID;
@ -36,6 +37,7 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
return res.sendStatus(400);
}
try {
//hash the userID
const adminUserID = await getHashCache(adminUserIDInput);
@ -72,6 +74,10 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
}
}
return res.sendStatus(200);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
}
export async function banUser(userID: UserID, enabled: boolean, unHideOldSubmissions: boolean,

View file

@ -17,6 +17,7 @@ export const validateLicenseKeyRegex = (token: string) =>
export async function verifyTokenRequest(req: VerifyTokenRequest, res: Response): Promise<Response> {
const { query: { licenseKey } } = req;
try {
if (!licenseKey) {
return res.status(400).send("Invalid request");
} else if (!validateLicenseKeyRegex(licenseKey)) {
@ -62,6 +63,10 @@ export async function verifyTokenRequest(req: VerifyTokenRequest, res: Response)
}
}
} catch (e) {
Logger.error(e as string);
return res.status(500);
}
}
async function checkAllGumroadProducts(licenseKey: string): Promise<boolean> {