From 7d431f0a74ddcb99a3b977ebd3798a5ea7b18446 Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Wed, 13 Dec 2023 17:15:52 -0800 Subject: [PATCH 1/5] Fix site wide 404, and pass along HTTP status to error handler for individual package pages --- src/handlers.js | 26 ++++++++++++++++++++++++-- src/main.js | 16 +++++++++++++++- src/utils.js | 6 +++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/handlers.js b/src/handlers.js index bf08bf6..9166f13 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -4,7 +4,7 @@ const server_version = require("../package.json").version; const { apiurl } = require("./config.js").getConfig(); const cache = require("./cache.js"); -const DEV = process.env.PULSAR_STATUS === "dev" ? true : false; +const DEV = process.env.PULSAR_STATUS === "dev" ? true : false; async function statusPage(req, res) { res.render('status', { message: `Server is up and running ${server_version}` }); @@ -65,6 +65,27 @@ async function singlePackageListing(req, res, timecop) { og_image_height: 600, }}); } catch(err) { + let status_to_display = false; // Since the status is ignored if not a number, + // we initialize as boolean to no-op in the case we don't find a proper status + + const validStatus = (val, key) => { + if (val?.response?.[key] && typeof val.response[key] === "boolean" && val.response[key]) { + return true; + } else { + return false; + } + }; + + if (validStatus(err, "notFound")) { + status_to_display = 404; + } else if (validStatus(err, "unauthorized")) { + status_to_display = 401; + } else if (validStatus(err, "forbidden")) { + status_to_display = 403; + } else if (validStatus(err, "badRequest")) { + status_to_display = 400; + } + utils.displayError(req, res, { error: utils.modifyErrorText(err), dev: DEV, @@ -75,7 +96,8 @@ async function singlePackageListing(req, res, timecop) { og_description: "The Pulsar Package Repository", og_image: "https://web.pulsar-edit.dev/public/pulsar_name.svg", og_image_type: "image/svg+xml" - } + }, + status_to_display: status_to_display }); } } diff --git a/src/main.js b/src/main.js index b5523ef..bca5a88 100644 --- a/src/main.js +++ b/src/main.js @@ -4,6 +4,8 @@ const path = require("path"); const handlers = require("./handlers.js"); const utils = require("./utils.js"); +const DEV = process.env.PULSAR_STATUS === "dev" ? true : false; + app.set("views", "./ejs-views/pages"); app.set("view engine", "ejs"); @@ -69,7 +71,19 @@ app.get("/logout", async (req, res) => { app.use(async (req, res) => { // 404 here, keep at last position - await utils.displayError(req, res, 404); + await utils.displayError(req, res, { + error: `The page '${req.url}' cannot be found.`, + dev: DEV, + timecop: false, + page: { + name: "PPR Error Page", + og_url: "https://web.pulsar-edit.dev/packages", + og_description: "The Pulsar Package Repository", + og_image: "https://web.pulsar-edit.dev/public/pulsar_name.svg", + og_image_type: "image/svg+xml" + }, + status_to_display: 404 + }); }); module.exports = app; diff --git a/src/utils.js b/src/utils.js index 34f9541..e31fc1d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -23,7 +23,11 @@ const reg = require("./reg.js"); async function displayError(req, res, details) { console.error(details); - res.status(500).render('error', details); + if (details?.status_to_display && typeof details.status_to_display === "number") { + res.status(details.status_to_display).render("error", details); + } else { + res.status(500).render("error", details); + } } function modifyErrorText(err) { From f6d2ce486c405b8e5df854ab73d75b8603ce018e Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Sun, 17 Dec 2023 18:36:49 -0800 Subject: [PATCH 2/5] Fix indentation --- src/main.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main.js b/src/main.js index bca5a88..b3cce77 100644 --- a/src/main.js +++ b/src/main.js @@ -72,18 +72,18 @@ app.get("/logout", async (req, res) => { app.use(async (req, res) => { // 404 here, keep at last position await utils.displayError(req, res, { - error: `The page '${req.url}' cannot be found.`, - dev: DEV, - timecop: false, - page: { - name: "PPR Error Page", - og_url: "https://web.pulsar-edit.dev/packages", - og_description: "The Pulsar Package Repository", - og_image: "https://web.pulsar-edit.dev/public/pulsar_name.svg", - og_image_type: "image/svg+xml" - }, - status_to_display: 404 - }); + error: `The page '${req.url}' cannot be found.`, + dev: DEV, + timecop: false, + page: { + name: "PPR Error Page", + og_url: "https://web.pulsar-edit.dev/packages", + og_description: "The Pulsar Package Repository", + og_image: "https://web.pulsar-edit.dev/public/pulsar_name.svg", + og_image_type: "image/svg+xml" + }, + status_to_display: 404 + }); }); module.exports = app; From beceefcfa3510fb0ee2e05c1af03c7a1e61ba893 Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Sun, 17 Dec 2023 18:38:04 -0800 Subject: [PATCH 3/5] `validStatus` => `validStatusIs` function name --- src/handlers.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/handlers.js b/src/handlers.js index 9166f13..5e55e88 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -68,7 +68,7 @@ async function singlePackageListing(req, res, timecop) { let status_to_display = false; // Since the status is ignored if not a number, // we initialize as boolean to no-op in the case we don't find a proper status - const validStatus = (val, key) => { + const validStatusIs = (val, key) => { if (val?.response?.[key] && typeof val.response[key] === "boolean" && val.response[key]) { return true; } else { @@ -76,13 +76,13 @@ async function singlePackageListing(req, res, timecop) { } }; - if (validStatus(err, "notFound")) { + if (validStatusIs(err, "notFound")) { status_to_display = 404; - } else if (validStatus(err, "unauthorized")) { + } else if (validStatusIs(err, "unauthorized")) { status_to_display = 401; - } else if (validStatus(err, "forbidden")) { + } else if (validStatusIs(err, "forbidden")) { status_to_display = 403; - } else if (validStatus(err, "badRequest")) { + } else if (validStatusIs(err, "badRequest")) { status_to_display = 400; } From 31b4bbb231a00885b8abb951668767f9d544e568 Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Sun, 17 Dec 2023 18:39:13 -0800 Subject: [PATCH 4/5] Streamline checking for variable validity and type --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index e31fc1d..90025e3 100644 --- a/src/utils.js +++ b/src/utils.js @@ -23,7 +23,7 @@ const reg = require("./reg.js"); async function displayError(req, res, details) { console.error(details); - if (details?.status_to_display && typeof details.status_to_display === "number") { + if (typeof details?.status_to_display === "number") { res.status(details.status_to_display).render("error", details); } else { res.status(500).render("error", details); From f00bf9747f9036ede5b6d3ef63e012071cfd546d Mon Sep 17 00:00:00 2001 From: confused_techie Date: Mon, 25 Dec 2023 15:09:34 -0800 Subject: [PATCH 5/5] Update src/handlers.js Co-authored-by: DeeDeeG --- src/handlers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers.js b/src/handlers.js index 5e55e88..cc183c0 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -69,7 +69,7 @@ async function singlePackageListing(req, res, timecop) { // we initialize as boolean to no-op in the case we don't find a proper status const validStatusIs = (val, key) => { - if (val?.response?.[key] && typeof val.response[key] === "boolean" && val.response[key]) { + if (typeof val?.response?.[key] === "boolean" && val.response[key]) { return true; } else { return false;