Skip to content

Commit

Permalink
Fix site wide 404, and pass along HTTP status to error handler for in…
Browse files Browse the repository at this point in the history
…dividual package pages
  • Loading branch information
confused-Techie committed Dec 14, 2023
1 parent b2e719f commit 7d431f0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}` });
Expand Down Expand Up @@ -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,
Expand All @@ -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
});
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
6 changes: 5 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7d431f0

Please sign in to comment.