better errors

This commit is contained in:
Miodec 2021-08-18 01:41:29 +01:00
parent c64cc2d538
commit 367b104679
2 changed files with 11 additions and 3 deletions

View file

@ -11,7 +11,7 @@ class ResultDAO {
} catch (e) {
user = null;
}
if (!user) throw new MonkeyError(404, "User not found");
if (!user) throw new MonkeyError(404, "User not found", "add result");
if (result.uid === undefined) result.uid = uid;
let res = await mongoDB().collection("results").insertOne(result);
return {

View file

@ -3,12 +3,20 @@ const { verifyIdToken } = require("../handlers/auth");
module.exports = {
async authenticateRequest(req, res, next) {
console.log();
try {
const { authorization } = req.headers;
if (!authorization) throw new MonkeyError(401, "Unauthorized");
if (!authorization)
throw new MonkeyError(
401,
"Unauthorized",
`endpoint: ${req.baseUrl} no authrizaion header found`
);
const token = authorization.split(" ");
if (token[0].trim() !== "Bearer")
return next(new MonkeyError(400, "Invalid Token"));
return next(
new MonkeyError(400, "Invalid Token", "Incorrect token type")
);
req.decodedToken = await verifyIdToken(token[1]);
return next();
} catch (e) {