mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-04 13:01:10 +08:00
better errors
This commit is contained in:
parent
c64cc2d538
commit
367b104679
2 changed files with 11 additions and 3 deletions
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue