diff --git a/src/handlers.js b/src/handlers.js index bf08bf6..cc183c0 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 validStatusIs = (val, key) => { + if (typeof val?.response?.[key] === "boolean" && val.response[key]) { + return true; + } else { + return false; + } + }; + + if (validStatusIs(err, "notFound")) { + status_to_display = 404; + } else if (validStatusIs(err, "unauthorized")) { + status_to_display = 401; + } else if (validStatusIs(err, "forbidden")) { + status_to_display = 403; + } else if (validStatusIs(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..b3cce77 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..90025e3 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 (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) {