add clearer error status codes
This commit is contained in:
+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,
|
||||
|
||||
Reference in New Issue
Block a user