Skip to content

Commit

Permalink
db-tabulator: robustify web-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Oct 5, 2023
1 parent e090366 commit 7d87b69
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions db-tabulator/web-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ router.get('/', async function (req, res, next) {
const [shutoffText, queries, revId] = await Promise.all([
checkShutoff(),
fetchQueriesForPage(page),
getLatestRevId(page),
getLatestRevId(page).catch(err => {
res.status(err.code || 500).render('oneline', { text: err.message });
}),
]);

if (revId === -1) {
return res.status(404).render('oneline', { text: `The page ${page} does not exist.` });
}
if (!revId) return;

if (shutoffText) {
log(`[E] Refused run on ${page} as task is shut off. Shutoff page content: ${shutoffText}`);
Expand Down Expand Up @@ -58,9 +58,12 @@ router.get('/', async function (req, res, next) {

async function getLatestRevId(page: string) {
let response = await bot.query({ prop: 'revisions', titles: page, rvprop: 'ids', rvlimit: 1 });
let pg = response.query.pages[0];
if (pg.missing) {
return -1;
let pg = response?.query?.pages?.[0];
if (!pg) {
throw new CustomError(500, 'Encountered error while fetching page content.');
}
if (pg.invalid || pg.missing) {
throw new CustomError(404, `The page ${page} does not exist.`);
}
return pg.revisions[0].revid;
}
Expand All @@ -70,4 +73,12 @@ function handleRedisError(e: Error) {
return false; // in sismember check, act as if value is not present
}

class CustomError extends Error {
code: number;
constructor(code: number, msg: string) {
super(msg);
this.code = code;
}
}

export default router;

0 comments on commit 7d87b69

Please sign in to comment.