more error catching, hope it works

This commit is contained in:
NK 2023-03-04 17:25:43 +00:00
parent 04ca5b3777
commit e603112606
2 changed files with 27 additions and 14 deletions

View file

@ -17,7 +17,7 @@
"axios": "^0.24.0", "axios": "^0.24.0",
"framer-motion": "^4", "framer-motion": "^4",
"mongodb": "^3.5.9", "mongodb": "^3.5.9",
"next": "^12.0.4", "next": "^12.3.4",
"next-auth": "^3.27.0", "next-auth": "^3.27.0",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",

View file

@ -5,22 +5,35 @@ async function decodeData(buffer) {
const parsedNbt = await parseNbt(Buffer.from(buffer, "base64")); const parsedNbt = await parseNbt(Buffer.from(buffer, "base64"));
return nbt.simplify(parsedNbt); return nbt.simplify(parsedNbt);
} }
let dat;
export default async function handler(req, res) { export default async function handler(req, res) {
if (req.method == "POST" || req.method == "GET" || req.method == "PUT") { if (req.method == "POST" || req.method == "GET" || req.method == "PUT") {
try { if (req.body == "") {
dat = JSON.parse(req.body).ByteData; res.status(400).json({error: "No body was provided"});
} catch (e) { return;
dat = req.body.ByteData;
} }
console.log(dat); console.log(req.body);
if (dat == undefined) let dat;
res.status(400).json({error: "ByteData is undefined"}); try {
dat = JSON.parse(req.body).ByteData || req.body.ByteData;
} catch (e) {
res.status(400).json({error: "ByteData is not a string or undefined"});
return;
}
if (typeof dat !== "string" || typeof dat == "undefined") {
res.status(400).json({error: "ByteData is not a string or undefined"});
return;
}
try {
const data = await getItemNetworth((await decodeData(dat)).i[0], { const data = await getItemNetworth((await decodeData(dat)).i[0], {
cache: true, cache: true,
}); });
res.status(200).json(data); res.status(200).json(data);
} catch {
res.status(400).json({error: "Invalid ByteData"});
}
} else { } else {
res.status(400).json({error: "This endpoint only accepts GET requests"}); res
.status(400)
.json({error: "This endpoint only accepts GET, POST and PUT requests"});
} }
} }