From e6031126062a5aa4f3bcddb758e8b47db727c161 Mon Sep 17 00:00:00 2001 From: NK Date: Sat, 4 Mar 2023 17:25:43 +0000 Subject: [PATCH] more error catching, hope it works --- package.json | 2 +- pages/api/skyblock.js | 39 ++++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 894547d..cce44fa 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "axios": "^0.24.0", "framer-motion": "^4", "mongodb": "^3.5.9", - "next": "^12.0.4", + "next": "^12.3.4", "next-auth": "^3.27.0", "react": "^17.0.2", "react-dom": "^17.0.2", diff --git a/pages/api/skyblock.js b/pages/api/skyblock.js index 7dc640e..7ab0fda 100644 --- a/pages/api/skyblock.js +++ b/pages/api/skyblock.js @@ -5,22 +5,35 @@ async function decodeData(buffer) { const parsedNbt = await parseNbt(Buffer.from(buffer, "base64")); return nbt.simplify(parsedNbt); } -let dat; export default async function handler(req, res) { if (req.method == "POST" || req.method == "GET" || req.method == "PUT") { - try { - dat = JSON.parse(req.body).ByteData; - } catch (e) { - dat = req.body.ByteData; + if (req.body == "") { + res.status(400).json({error: "No body was provided"}); + return; + } + console.log(req.body); + let dat; + 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], { + cache: true, + }); + res.status(200).json(data); + } catch { + res.status(400).json({error: "Invalid ByteData"}); } - console.log(dat); - if (dat == undefined) - res.status(400).json({error: "ByteData is undefined"}); - const data = await getItemNetworth((await decodeData(dat)).i[0], { - cache: true, - }); - res.status(200).json(data); } 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"}); } }