From a32945bf6794f8a7cf6b82f5d8fcb841a03f42bf Mon Sep 17 00:00:00 2001 From: tdurieux Date: Wed, 21 Apr 2021 22:47:34 +0200 Subject: [PATCH] improve logs --- routes/file.js | 11 +-- routes/repository.js | 10 ++- routes/user.js | 168 +++++++++++++++++++++++++------------------ routes/webview.js | 2 +- 4 files changed, 114 insertions(+), 77 deletions(-) diff --git a/routes/file.js b/routes/file.js index ac9bfa9..b7a6f5e 100644 --- a/routes/file.js +++ b/routes/file.js @@ -40,7 +40,7 @@ async function anonymizeRepository(options) { try { } catch (error) { console.error("Error while updating the repository."); - console.error(repoConfig.repoId, error); + console.error(repoConfig.repoId, req.path, error); } await repoUtils.updateAnonymizedRepository(repoConfig); } @@ -61,6 +61,7 @@ router.get("/:repoId/files", async (req, res) => { const files = await fileUtils.getFileList({ repoConfig }); return res.json(files); } catch (error) { + console.error(req.path, error); return res.status(500).json({ error }); } }); @@ -83,7 +84,7 @@ router.get("/:repoId/stats", async (req, res) => { const stats = await fileUtils.getStats({ repoConfig }); return res.json(stats.languages); } catch (error) { - console.error(req.params.repoId, error); + console.error(req.path, error); return res.status(500).json({ error }); } }); @@ -98,7 +99,7 @@ router.get("/:repoId/options", async (req, res) => { await anonymizeRepository({ repoConfig }); } catch (error) { console.log("Error during the anonymization of the repository"); - console.error(req.params.repoId, error); + console.error(req.path, error); } if (repoConfig.status == "removed") { throw "repository_expired"; @@ -115,7 +116,7 @@ router.get("/:repoId/options", async (req, res) => { return res.json(repoConfig.options); } catch (error) { - console.error(req.params.repoId, error); + console.error(req.path, error); return res.status(500).json({ error }); } }); @@ -156,7 +157,7 @@ router.get("/:repoId/file/:path*", async (req, res) => { return res.status(404).json({ error: "file_not_found" }); } } catch (error) { - console.error(req.params.repoId, req.params.path, error); + console.error(req.path, error); return res.status(500).send({ error }); } }); diff --git a/routes/repository.js b/routes/repository.js index e7ab15c..4594f3e 100644 --- a/routes/repository.js +++ b/routes/repository.js @@ -27,6 +27,7 @@ router.get("/:repoId/", async (req, res) => { } res.status(404).send("repo_not_found"); } catch (error) { + console.error(req.path, error); res.status(500).send(error); } }); @@ -119,7 +120,7 @@ router.post("/:repoId/", async (req, res) => { await githubUtils.downloadRepoAndAnonymize(repoConfig); await repoUtils.updateStatus(repoConfig, "ready"); } catch (error) { - console.error(error); + console.error(req.path, error); await repoUtils.updateStatus(repoConfig, "error", error); return res.status(500).json({ error }); } @@ -139,6 +140,7 @@ router.post("/:repoId/refresh", async (req, res) => { await repoUtils.updateAnonymizedRepository(repoConfig); return res.send("ok"); } catch (error) { + console.error(req.path, error); return res.status(500).json({ error }); } }); @@ -159,6 +161,7 @@ router.delete("/:repoId/", async (req, res) => { console.log(`${req.params.repoId} is removed`); return res.json("ok"); } catch (error) { + console.error(req.path, error); return res.status(500).json({ error }); } }); @@ -193,6 +196,7 @@ router.post("/claim", async (req, res) => { ); return res.send("Ok"); } catch (error) { + console.error(req.path, error); return res.status(500).json({ error }); } }); @@ -210,6 +214,7 @@ router.get("/:owner/:repo/", async (req, res) => { } res.status(404).send("repo_not_found"); } catch (error) { + console.error(req.path, error); res.status(500).send(error); } }); @@ -227,6 +232,7 @@ router.get("/:owner/:repo/branches", async (req, res) => { } res.status(404).send("repo_not_found"); } catch (error) { + console.error(req.path, error); res.status(500).send(error); } }); @@ -337,7 +343,7 @@ router.post("/", async (req, res) => { await githubUtils.downloadRepoAndAnonymize(data); await repoUtils.updateStatus(repoConfig, "ready"); } catch (error) { - console.error(error); + console.error(req.path, error); await repoUtils.updateStatus(repoConfig, "error", "unable_to_anonymize"); return res .status(500) diff --git a/routes/user.js b/routes/user.js index 3fc3540..94fd7cb 100644 --- a/routes/user.js +++ b/routes/user.js @@ -11,93 +11,123 @@ const router = express.Router(); router.use(connection.ensureAuthenticated); router.get("/logout", async (req, res) => { - req.logout(); - res.redirect("/"); + try { + req.logout(); + res.redirect("/"); + } catch (error) { + console.error(req.path, error); + res.status(500).json({ error }); + } }); router.get("/", async (req, res) => { - const photo = req.user.profile.photos.length - ? req.user.profile.photos[0].value - : null; - res.json({ username: req.user.profile.username, photo }); + try { + const photo = req.user.profile.photos.length + ? req.user.profile.photos[0].value + : null; + res.json({ username: req.user.profile.username, photo }); + } catch (error) { + console.error(req.path, error); + res.status(500).json({ error }); + } }); router.get("/default", async (req, res) => { - const d = await db - .get("users") - .findOne({ username: req.user.username }, { projection: { default: 1 } }); - res.json(d.default); + try { + const d = await db + .get("users") + .findOne({ username: req.user.username }, { projection: { default: 1 } }); + res.json(d.default); + } catch (error) { + console.error(req.path, error); + res.status(500).json({ error }); + } }); router.post("/default", async (req, res) => { - const d = req.body; - await db - .get("users") - .updateOne({ username: req.user.username }, { $set: { default: d } }); - res.send("ok"); + try { + const d = req.body; + await db + .get("users") + .updateOne({ username: req.user.username }, { $set: { default: d } }); + res.send("ok"); + } catch (error) { + console.error(req.path, error); + res.status(500).json({ error }); + } }); router.get("/anonymized_repositories", async (req, res) => { - const repos = await db - .get("anonymized_repositories") - .find( - { - owner: req.user.username, - }, - { projection: { token: 0, files: 0, originalFiles: 0 } } - ) - .toArray(); - for (let repo of repos) { - if (repo.options.expirationDate) { - repo.options.expirationDate = new Date(repo.options.expirationDate); - } - if ( - repo.options.expirationMode != "never" && - repo.options.expirationDate != null && - repo.options.expirationDate < new Date() - ) { - await repoUtils.updateStatus({ repoId: repo.repoId }, "expired"); - repo.status = "expired"; + try { + const repos = await db + .get("anonymized_repositories") + .find( + { + owner: req.user.username, + }, + { projection: { token: 0, files: 0, originalFiles: 0 } } + ) + .toArray(); + for (let repo of repos) { + if (repo.options.expirationDate) { + repo.options.expirationDate = new Date(repo.options.expirationDate); + } + if ( + repo.options.expirationMode != "never" && + repo.options.expirationDate != null && + repo.options.expirationDate < new Date() + ) { + await repoUtils.updateStatus({ repoId: repo.repoId }, "expired"); + repo.status = "expired"; + } } + res.json(repos); + } catch (error) { + console.error(req.path, error); + res.status(500).json({ error }); } - res.json(repos); }); router.get("/all_repositories", async (req, res) => { - const user = await db - .get() - .collection("users") - .findOne( - { username: req.user.username }, - { projection: { repositories: 1 } } - ); - if (!user) { - res.status(401).send({ error: "user_not_found" }); - } - if (user.repositories && req.query.force !== "1") { - return res.json(user.repositories); - } else { - const octokit = new Octokit({ auth: req.user.accessToken }); - const repositories = await octokit.paginate( - octokit.repos.listForAuthenticatedUser, - { - visibility: "all", - sort: "pushed", - per_page: 100, - } - ); - try { - await db - .get() - .collection("users") - .updateOne( - { username: req.user.profile.username }, - { $set: { repositories } } - ); - res.json(repositories); - } catch (error) { - res.status(500).send(error); + try { + const user = await db + .get() + .collection("users") + .findOne( + { username: req.user.username }, + { projection: { repositories: 1 } } + ); + if (!user) { + res.status(401).send({ error: "user_not_found" }); } + if (user.repositories && req.query.force !== "1") { + return res.json(user.repositories); + } else { + const octokit = new Octokit({ auth: req.user.accessToken }); + const repositories = await octokit.paginate( + octokit.repos.listForAuthenticatedUser, + { + visibility: "all", + sort: "pushed", + per_page: 100, + } + ); + try { + await db + .get() + .collection("users") + .updateOne( + { username: req.user.profile.username }, + { $set: { repositories } } + ); + res.json(repositories); + } catch (error) { + res.status(500).send(error); + } + } + } catch (error) { + console.error(req.path, error); + res.status(500).json({ error }); } }); diff --git a/routes/webview.js b/routes/webview.js index 75d6827..6aea096 100644 --- a/routes/webview.js +++ b/routes/webview.js @@ -58,7 +58,7 @@ async function webView(req, res) { }); } } catch (error) { - console.error(error); + console.error(req.path, error); return res.status(500).send({ error }); } return res.status(404).send("file_not_found");