add clearer error status codes
This commit is contained in:
+1
-1
@@ -61,7 +61,7 @@ export const setup = function (router, con, server) {
|
||||
next();
|
||||
}
|
||||
else {
|
||||
res.status(400);
|
||||
res.status(402);
|
||||
res.json({ "error": "you cannot access the api without being logged in" });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ export const setup = function (router, con, server) {
|
||||
res.set("Access-Control-Allow-Origin", "");
|
||||
let otherperson = encodeURIComponent(req.query.otherperson || "");
|
||||
if (typeof otherperson != "string" || otherperson.length > 100 || otherperson == "") {
|
||||
res.status(400).json({ "error": "invalid otherperson given" });
|
||||
res.status(410).json({ "error": "invalid otherperson given" });
|
||||
return;
|
||||
}
|
||||
const columns = [
|
||||
@@ -14,7 +14,7 @@ export const setup = function (router, con, server) {
|
||||
//dms_user_name = sender
|
||||
//dms_receiver = receiver
|
||||
//if (sender == current and receiver == other) or (receiver == current and sender == other)
|
||||
let sql = `select ${columns.join(",")} from ipost.dms where ((dms_receiver = ? and dms_user_name = ?) or (dms_receiver = ? and dms_user_name = ?)) order by dms_id desc;`;
|
||||
let sql = `select ${columns.join(",")} from ipost.dms where ((dms_receiver = ? and dms_user_name = ?) or (dms_receiver = ? and dms_user_name = ?)) order by dms_id desc limit 50;`;
|
||||
con.query(sql, [otherperson, encodeURIComponent(res.locals.username), encodeURIComponent(res.locals.username), otherperson], function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
|
||||
+15
-3
@@ -22,18 +22,22 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.post("/api/dms/post", function (req, res) {
|
||||
if (!req.body.message) {
|
||||
res.status(410)
|
||||
res.json({ "error": "no message to post" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.message) != "string") {
|
||||
res.status(411)
|
||||
res.json({ "error": "no message to post" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.pid) != "string") {
|
||||
res.status(412)
|
||||
res.json({ "error": "no pid given" });
|
||||
return;
|
||||
}
|
||||
if (req.body.pid.length != 10 || PIDS[req.body.pid] !== true) {
|
||||
res.status(413)
|
||||
res.json({ "error": "invalid pid given" });
|
||||
return;
|
||||
}
|
||||
@@ -46,33 +50,41 @@ export const setup = function (router, con, server) {
|
||||
reply_id = req.body.reply_id;
|
||||
}
|
||||
if ((typeof reply_id) != "number") {
|
||||
res.status(414)
|
||||
res.json({ "error": "no valid reply id given" });
|
||||
return;
|
||||
}
|
||||
if (req.body.message.length > 1000) {
|
||||
res.status(415)
|
||||
res.json({ "error": "message too long" });
|
||||
return;
|
||||
}
|
||||
req.body.message = encodeURIComponent(req.body.message.trim());
|
||||
if (req.body.message.length > 3000) {
|
||||
res.status(416)
|
||||
res.json({ "error": "message too long" }); //check again after URI encoding it
|
||||
return;
|
||||
}
|
||||
req.body.receiver = encodeURIComponent(req.body.receiver || "");
|
||||
if (req.body.receiver == "" || req.body.receiver == encodeURIComponent(res.locals.username) || req.body.receiver.length > 100) {
|
||||
res.status(400).json({ "error": "invalid receiver given" });
|
||||
res.status(417).json({ "error": "invalid receiver given" });
|
||||
return;
|
||||
}
|
||||
let otherperson = req.body.receiver;
|
||||
if (!req.body.message) {
|
||||
res.status(418)
|
||||
res.json({ "error": "no message to post" });
|
||||
return;
|
||||
}
|
||||
let sql = `insert into ipost.dms (dms_user_name,dms_text,dms_time,dms_receiver,dms_from_bot,dms_reply_id) values (?,?,?,?,?,?);`;
|
||||
let values = [encodeURIComponent(res.locals.username), req.body.message, Date.now(), otherperson, res.locals.isbot, reply_id];
|
||||
con.query(sql, values, function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
if (err) {
|
||||
res.status(500)
|
||||
res.json({"error":"there's been an internal error"})
|
||||
console.error(err)
|
||||
return;
|
||||
}
|
||||
// let post_obj = {
|
||||
// post_user_name: encodeURIComponent(res.locals.username),
|
||||
// post_text: req.body.message,
|
||||
|
||||
+15
-2
@@ -20,18 +20,22 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.post("/api/post", function (req, res) {
|
||||
if (!req.body.message) {
|
||||
res.status(410)
|
||||
res.json({ "error": "no message to post" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.message) != "string") {
|
||||
res.status(411)
|
||||
res.json({ "error": "no message to post" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.pid) != "string") {
|
||||
res.status(412)
|
||||
res.json({ "error": "no pid given" });
|
||||
return;
|
||||
}
|
||||
if (req.body.pid.length != 10 || PIDS[req.body.pid] !== true) {
|
||||
res.status(413)
|
||||
res.json({ "error": "invalid pid given" });
|
||||
return;
|
||||
}
|
||||
@@ -46,20 +50,24 @@ export const setup = function (router, con, server) {
|
||||
if(typeof reply_id == "string") {
|
||||
reply_id = parseInt(reply_id)
|
||||
if(isNaN(reply_id)) {
|
||||
res.status(414)
|
||||
res.json({ "error": "no valid reply id given" });
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ((typeof reply_id) != "number") {
|
||||
res.status(415)
|
||||
res.json({ "error": "no valid reply id given" });
|
||||
return;
|
||||
}
|
||||
if (req.body.message.length > 1000) {
|
||||
res.status(416)
|
||||
res.json({ "error": "message too long" });
|
||||
return;
|
||||
}
|
||||
req.body.message = encodeURIComponent(req.body.message.trim());
|
||||
if (req.body.message.length > 3000) {
|
||||
res.status(417)
|
||||
res.json({ "error": "message too long" }); //check again after URI encoding it
|
||||
return;
|
||||
}
|
||||
@@ -67,14 +75,19 @@ export const setup = function (router, con, server) {
|
||||
if (req.body.receiver == "")
|
||||
req.body.receiver = "everyone";
|
||||
if (!req.body.message) {
|
||||
res.status(418)
|
||||
res.json({ "error": "no message to post" });
|
||||
return;
|
||||
}
|
||||
let sql = `insert into ipost.posts (post_user_name,post_text,post_time,post_receiver_name,post_from_bot,post_reply_id) values (?,?,?,?,?,?);`;
|
||||
let values = [encodeURIComponent(res.locals.username), req.body.message, Date.now(), req.body.receiver, res.locals.isbot, reply_id];
|
||||
con.query(sql, values, function (err, result) {
|
||||
if (err)
|
||||
throw err;
|
||||
if (err){
|
||||
res.status(500)
|
||||
res.json({"error":"there's been an interal error"})
|
||||
console.error(err)
|
||||
return;
|
||||
}
|
||||
let post_obj = {
|
||||
post_user_name: encodeURIComponent(res.locals.username),
|
||||
post_text: req.body.message,
|
||||
|
||||
@@ -7,10 +7,12 @@ export const setup = function (router, con, server) {
|
||||
});
|
||||
router.post("/api/settings", function (req, res) {
|
||||
if (!req.body.setting) {
|
||||
res.status(410)
|
||||
res.json({ "error": "no setting to change" });
|
||||
return;
|
||||
}
|
||||
if ((typeof req.body.setting) != "string") {
|
||||
res.status(411)
|
||||
res.json({ "error": "no setting to change" });
|
||||
return;
|
||||
}
|
||||
@@ -25,6 +27,7 @@ export const setup = function (router, con, server) {
|
||||
}
|
||||
if (!allowed) {
|
||||
console.log(5, "incorrect type given, received, expected", typeof req.body.value, allowed_settings[req.body.setting]);
|
||||
res.status(412)
|
||||
res.json({ "error": "no new setting value given" });
|
||||
return;
|
||||
}
|
||||
@@ -36,6 +39,7 @@ export const setup = function (router, con, server) {
|
||||
let values = [JSON.stringify(res.locals.settings), res.locals.username];
|
||||
con.query(sql, values, function (err, result) {
|
||||
if (err) {
|
||||
res.status(500)
|
||||
res.json({ "status": "error", "code": err });
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user