Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix site-wide 404 & Pass along HTTP status info to error handler #123

Merged
merged 5 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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,
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 (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