feat: improve error message

This commit is contained in:
tdurieux
2021-09-06 22:34:58 +02:00
parent 36c1098d55
commit eaa9656815
16 changed files with 147 additions and 72 deletions
+21 -23
View File
@@ -1,9 +1,7 @@
import * as express from "express";
import config from "../../config";
import AnonymousError from "../AnonymousError";
import Conference from "../Conference";
import AnonymizedRepositoryModel from "../database/anonymizedRepositories/anonymizedRepositories.model";
import ConferenceModel from "../database/conference/conferences.model";
import Repository from "../Repository";
import { ensureAuthenticated } from "./connection";
import { handleError, getUser } from "./route-utils";
@@ -69,26 +67,26 @@ router.get("/", async (req: express.Request, res: express.Response) => {
});
function validateConferenceForm(conf) {
if (!conf.name) throw new Error("conf_name_missing");
if (!conf.conferenceID) throw new Error("conf_id_missing");
if (!conf.startDate) throw new Error("conf_start_date_missing");
if (!conf.endDate) throw new Error("conf_end_date_missing");
if (!conf.name) throw new AnonymousError("conf_name_missing");
if (!conf.conferenceID) throw new AnonymousError("conf_id_missing");
if (!conf.startDate) throw new AnonymousError("conf_start_date_missing");
if (!conf.endDate) throw new AnonymousError("conf_end_date_missing");
if (new Date(conf.startDate) > new Date(conf.endDate))
throw new Error("conf_start_date_invalid");
throw new AnonymousError("conf_start_date_invalid");
if (new Date() > new Date(conf.endDate))
throw new Error("conf_end_date_invalid");
throw new AnonymousError("conf_end_date_invalid");
if (plans.filter((p) => p.id == conf.plan.planID).length != 1)
throw new Error("invalid_plan");
throw new AnonymousError("invalid_plan");
const plan = plans.filter((p) => p.id == conf.plan.planID)[0];
if (plan.pricePerRepo > 0) {
const billing = conf.billing;
if (!billing) throw new Error("billing_missing");
if (!billing.name) throw new Error("billing_name_missing");
if (!billing.email) throw new Error("billing_email_missing");
if (!billing.address) throw new Error("billing_address_missing");
if (!billing.city) throw new Error("billing_city_missing");
if (!billing.zip) throw new Error("billing_zip_missing");
if (!billing.country) throw new Error("billing_country_missing");
if (!billing) throw new AnonymousError("billing_missing");
if (!billing.name) throw new AnonymousError("billing_name_missing");
if (!billing.email) throw new AnonymousError("billing_email_missing");
if (!billing.address) throw new AnonymousError("billing_address_missing");
if (!billing.city) throw new AnonymousError("billing_city_missing");
if (!billing.zip) throw new AnonymousError("billing_zip_missing");
if (!billing.country) throw new AnonymousError("billing_country_missing");
}
}
@@ -103,7 +101,7 @@ router.post(
conferenceID: req.params.conferenceID,
});
if (model.owners.indexOf(user.model.id) == -1)
throw new Error("not_authorized");
throw new AnonymousError("not_authorized");
}
validateConferenceForm(req.body);
model.name = req.body.name;
@@ -148,7 +146,7 @@ router.post(
res.send("ok");
} catch (error) {
if (error.message?.indexOf(" duplicate key") > -1) {
return handleError(new Error("conf_id_used"), res);
return handleError(new AnonymousError("conf_id_used"), res);
}
handleError(error, res);
}
@@ -163,10 +161,10 @@ router.get(
const data = await ConferenceModel.findOne({
conferenceID: req.params.conferenceID,
});
if (!data) throw new Error("conf_not_found");
if (!data) throw new AnonymousError("conf_not_found");
const conference = new Conference(data);
if (conference.ownerIDs.indexOf(user.model.id) == -1)
throw new Error("not_authorized");
throw new AnonymousError("not_authorized");
const o: any = conference.toJSON();
o.repositories = (await conference.repositories()).map((r) => r.toJSON());
res.json(o);
@@ -184,10 +182,10 @@ router.delete(
const data = await ConferenceModel.findOne({
conferenceID: req.params.conferenceID,
});
if (!data) throw new Error("conf_not_found");
if (!data) throw new AnonymousError("conf_not_found");
const conference = new Conference(data);
if (conference.ownerIDs.indexOf(user.model.id) == -1)
throw new Error("not_authorized");
throw new AnonymousError("not_authorized");
await conference.remove();
res.send("ok");
} catch (error) {
+12 -8
View File
@@ -11,6 +11,7 @@ import config from "../../config";
import { IAnonymizedRepositoryDocument } from "../database/anonymizedRepositories/anonymizedRepositories.types";
import Repository from "../Repository";
import ConferenceModel from "../database/conference/conferences.model";
import AnonymousError from "../AnonymousError";
const router = express.Router();
@@ -182,22 +183,22 @@ function validateNewRepo(repoUpdate) {
!repoUpdate.repoId.match(validCharacters) ||
repoUpdate.repoId.length < 3
) {
throw new Error("invalid_repoId");
throw new AnonymousError("invalid_repoId");
}
if (!repoUpdate.source.branch) {
throw new Error("branch_not_specified");
throw new AnonymousError("branch_not_specified");
}
if (!repoUpdate.source.commit) {
throw new Error("commit_not_specified");
throw new AnonymousError("commit_not_specified");
}
if (!repoUpdate.options) {
throw new Error("options_not_provided");
throw new AnonymousError("options_not_provided");
}
if (!Array.isArray(repoUpdate.terms)) {
throw new Error("invalid_terms_format");
throw new AnonymousError("invalid_terms_format");
}
if (!/^[a-f0-9]+$/.test(repoUpdate.source.commit)) {
throw new Error("invalid_commit_format");
throw new AnonymousError("invalid_commit_format");
}
}
@@ -283,7 +284,7 @@ router.post(
new Date() > conf.endDate ||
conf.status !== "ready"
) {
throw new Error("conf_not_activated");
throw new AnonymousError("conf_not_activated");
}
const f = conf.repositories.filter((r) => r.id == repo.model.id);
if (f.length) {
@@ -358,7 +359,7 @@ router.post("/", async (req: express.Request, res: express.Response) => {
conf.status !== "ready"
) {
await repo.remove();
throw new Error("conf_not_activated");
throw new AnonymousError("conf_not_activated");
}
conf.repositories.push({
id: repo.id,
@@ -371,6 +372,9 @@ router.post("/", async (req: express.Request, res: express.Response) => {
res.send("ok");
new Repository(repo).anonymize();
} catch (error) {
if (error.message?.indexOf(" duplicate key") > -1) {
return handleError(new AnonymousError("repoId_already_used", repoUpdate.repoId), res);
}
return handleError(error, res);
}
});
+1 -1
View File
@@ -58,9 +58,9 @@ router.get(
redirectURL = repo.source.url;
} else {
repo.check();
await repo.updateIfNeeded();
}
await repo.updateIfNeeded();
let download = false;
const conference = await repo.conference();
+36 -3
View File
@@ -1,6 +1,12 @@
import * as express from "express";
import AnonymizedFile from "../AnonymizedFile";
import AnonymousError from "../AnonymousError";
import * as db from "../database/database";
import UserModel from "../database/users/users.model";
import Repository from "../Repository";
import GitHubBase from "../source/GitHubBase";
import GitHubDownload from "../source/GitHubDownload";
import { GitHubRepository } from "../source/GitHubRepository";
import User from "../User";
export async function getRepo(
@@ -31,8 +37,35 @@ export async function getRepo(
}
}
function printError(error: any) {
if (error instanceof AnonymousError) {
let detail = error.value?.toString();
if (error.value instanceof Repository) {
detail = error.value.repoId;
} else if (error.value instanceof AnonymizedFile) {
detail = `/r/${error.value.repository.repoId}/${error.value.anonymizedPath}`;
} else if (error.value instanceof GitHubRepository) {
detail = `${error.value.fullName}`;
} else if (error.value instanceof User) {
detail = `${error.value.username}`;
} else if (error.value instanceof GitHubBase) {
detail = `${error.value.repository.repoId}`;
}
console.error(
"[ERROR]",
error.message,
`'${detail}'`,
error.stack.split("\n")[1].trim()
);
} else if (error instanceof Error) {
console.error(error);
} else {
console.error(error);
}
}
export function handleError(error: any, res: express.Response) {
console.log(error);
printError(error);
let message = error;
if (error instanceof Error) {
message = error.message;
@@ -52,12 +85,12 @@ export async function getUser(req: express.Request) {
const user = (req.user as any).user;
if (!user) {
req.logout();
throw new Error("not_connected");
throw new AnonymousError("not_connected");
}
const model = await UserModel.findById(user._id);
if (!model) {
req.logout();
throw new Error("not_connected");
throw new AnonymousError("not_connected");
}
return new User(model);
}
+4 -6
View File
@@ -3,6 +3,7 @@ import { getRepo, handleError } from "./route-utils";
import * as path from "path";
import AnonymizedFile from "../AnonymizedFile";
import GitHubDownload from "../source/GitHubDownload";
import AnonymousError from "../AnonymousError";
const router = express.Router();
@@ -10,18 +11,15 @@ async function webView(req: express.Request, res: express.Response) {
const repo = await getRepo(req, res);
if (!repo) return;
try {
if (!repo.options.page) {
throw new Error("page_not_activated");
}
if (!repo.options.pageSource) {
throw new Error("page_not_activated");
if (!repo.options.page || !repo.options.pageSource) {
throw new AnonymousError("page_not_activated");
}
if (
repo.options.pageSource?.branch !=
(repo.source as GitHubDownload).branch.name
) {
throw new Error("page_not_supported_on_different_branch");
throw new AnonymousError("page_not_supported_on_different_branch");
}
let requestPath = path.join(