From db7b640ae4ff36669c48597c7a07c809f4e79561 Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 10 Nov 2021 17:26:46 -0800 Subject: [PATCH] feat: better error handling 1/4 + add status codes to all of the errors. ? I understand this is similar to robtop so I didn't change any of the very cryptic responses, i.e. -1, -2, -3. --- api/analyze.js | 6 +++--- api/comments.js | 8 ++++---- api/download.js | 8 ++++---- api/gauntlets.js | 8 ++++---- api/icon.js | 6 +++--- api/level.js | 6 +++--- api/mappacks.js | 8 ++++---- api/profile.js | 8 ++++---- api/search.js | 14 +++++++------- api/song.js | 6 +++--- 10 files changed, 39 insertions(+), 39 deletions(-) diff --git a/api/analyze.js b/api/analyze.js index 2f21031..3816b8d 100644 --- a/api/analyze.js +++ b/api/analyze.js @@ -13,14 +13,14 @@ module.exports = async (app, req, res, level) => { const raw_data = level.data; const response_data = analyze_level(level, raw_data); - return res.send(response_data); + return res.status(200).send(response_data); } else { zlib.unzip(levelString, (err, buffer) => { - if (err) { return res.send("-2"); } + if (err) { return res.status(500).send("-2"); } const raw_data = buffer.toString(); const response_data = analyze_level(level, raw_data); - return res.send(response_data); + return res.status(200).send(response_data); }); } } diff --git a/api/comments.js b/api/comments.js index 4e56a11..8889991 100644 --- a/api/comments.js +++ b/api/comments.js @@ -1,6 +1,6 @@ module.exports = async (app, req, res) => { - if (req.offline) return res.send("-1") + if (req.offline) return res.status(500).send("-1") let count = +req.query.count || 10 if (count > 1000) count = 1000 @@ -20,14 +20,14 @@ module.exports = async (app, req, res) => { req.gdRequest(path, req.gdParams(params), function(err, resp, body) { - if (err) return res.send("-1") + if (err) return res.status(500).send("-1") comments = body.split('|') comments = comments.map(x => x.split(':')) comments = comments.map(x => x.map(x => app.parseResponse(x, "~"))) if (req.query.type == "profile") comments.filter(x => x[0][2]) else comments = comments.filter(x => x[0] && x[0][2]) - if (!comments.length) return res.send("-1") + if (!comments.length) return res.status(204).send("-1") let pages = body.split('#')[1].split(":") let lastPage = +Math.ceil(+pages[0] / +pages[2]); @@ -79,7 +79,7 @@ module.exports = async (app, req, res) => { }) - return res.send(commentArray) + return res.status(200).send(commentArray) }) } \ No newline at end of file diff --git a/api/download.js b/api/download.js index 55fe092..4a3c283 100644 --- a/api/download.js +++ b/api/download.js @@ -6,7 +6,7 @@ module.exports = async (app, req, res, api, ID, analyze) => { function rejectLevel() { if (!api) return res.redirect('search/' + req.params.id) - else return res.send("-1") + else return res.status(500).send("-1") } if (req.offline) { @@ -22,7 +22,7 @@ module.exports = async (app, req, res, api, ID, analyze) => { req.gdRequest('downloadGJLevel22', { levelID }, function (err, resp, body) { if (err) { - if (analyze && api && req.server.downloadsDisabled) return res.send("-3") + if (analyze && api && req.server.downloadsDisabled) return res.status(403).send("-3") else if (!api && levelID < 0) return res.redirect(`/?daily=${levelID * -1}`) else return rejectLevel() } @@ -69,7 +69,7 @@ module.exports = async (app, req, res, api, ID, analyze) => { if (analyze) return app.run.analyze(app, req, res, level) function sendLevel() { - if (api) return res.send(level) + if (api) return res.status(200).send(level) else return fs.readFile('./html/level.html', 'utf8', function (err, data) { let html = data; @@ -78,7 +78,7 @@ module.exports = async (app, req, res, api, ID, analyze) => { let regex = new RegExp(`\\[\\[${x.toUpperCase()}\\]\\]`, "g") html = html.replace(regex, app.clean(level[x])) }) - return res.send(html) + return res.status(200).send(html) }) } diff --git a/api/gauntlets.js b/api/gauntlets.js index 3e3b01a..5a911cc 100644 --- a/api/gauntlets.js +++ b/api/gauntlets.js @@ -3,19 +3,19 @@ let gauntletNames = ["Fire", "Ice", "Poison", "Shadow", "Lava", "Bonus", "Chaos" module.exports = async (app, req, res) => { - if (req.offline) return res.send("-1") + if (req.offline) return res.status(500).send("-1") let cached = cache[req.id] - if (app.config.cacheGauntlets && cached && cached.data && cached.indexed + 2000000 > Date.now()) return res.send(cached.data) // half hour cache + if (app.config.cacheGauntlets && cached && cached.data && cached.indexed + 2000000 > Date.now()) return res.status(200).send(cached.data) // half hour cache req.gdRequest('getGJGauntlets21', {}, function (err, resp, body) { - if (err) return res.send("-1") + if (err) return res.status(500).send("-1") let gauntlets = body.split('#')[0].split('|').map(x => app.parseResponse(x)).filter(x => x[3]) let gauntletList = gauntlets.map(x => ({ id: +x[1], name: gauntletNames[+x[1] - 1] || "Unknown", levels: x[3].split(",") })) if (app.config.cacheGauntlets) cache[req.id] = {data: gauntletList, indexed: Date.now()} - res.send(gauntletList) + res.status(200).send(gauntletList) }) diff --git a/api/icon.js b/api/icon.js index a426751..1d15c47 100644 --- a/api/icon.js +++ b/api/icon.js @@ -86,7 +86,7 @@ module.exports = async (app, req, res) => { let iconCode = `${req.query.form == "cursed" ? "cursed" : form}${topless ? "top" : ""}-${iconID}-${col1}-${col2}-${colG || "x"}-${colW || "x"}-${outline ? 1 : 0}` - if (!sizeParam && (!isSpecial || drawLegs) && cache[iconCode]) return res.end(cache[iconCode].value) + if (!sizeParam && (!isSpecial || drawLegs) && cache[iconCode]) return res.status(200).end(cache[iconCode].value) let useExtra = false let originalOffset = icons[icon].spriteOffset; @@ -301,7 +301,7 @@ module.exports = async (app, req, res) => { cache[iconCode] = { value: buffer, timeoutID: setTimeout(function() {delete cache[iconCode]}, 10000000) } // 3 hour cache if (usercode) cache[usercode] = { value: buffer, timeoutID: setTimeout(function() {delete cache[usercode]}, 300000) } // 5 min cache for player icons } - return res.end(buffer, 'base64') + return res.status(200).end(buffer, 'base64') }) } @@ -363,7 +363,7 @@ module.exports = async (app, req, res) => { else if (app.config.cachePlayerIcons && !Object.keys(req.query).filter(x => !["form", "forceGD"].includes(x)).length) { userCode = `${req.id}u-${username.toLowerCase()}-${forms[req.query.form] ? req.query.form : 'cube'}` - if (cache[userCode]) return res.end(cache[userCode].value) + if (cache[userCode]) return res.status(200).end(cache[userCode].value) } let accountMode = !req.query.hasOwnProperty("player") && Number(req.params.id) diff --git a/api/level.js b/api/level.js index ad138a0..c97a45c 100644 --- a/api/level.js +++ b/api/level.js @@ -6,7 +6,7 @@ module.exports = async (app, req, res, api, analyze) => { function rejectLevel() { if (!api) return res.redirect('search/' + req.params.id) - else return res.send("-1") + else return res.status(500).send("-1") } if (req.offline) return rejectLevel() @@ -37,7 +37,7 @@ module.exports = async (app, req, res, api, analyze) => { function sendLevel() { - if (api) return res.send(level) + if (api) return res.status(200).send(level) else return fs.readFile('./html/level.html', 'utf8', function (err, data) { let html = data; @@ -50,7 +50,7 @@ module.exports = async (app, req, res, api, analyze) => { }) if (req.server.downloadsDisabled) html = html.replace('id="additional" class="', 'id="additional" class="downloadDisabled ') .replace('analyzeBtn"', 'analyzeBtn" style="filter: opacity(30%)"') - return res.send(html) + return res.status(200).send(html) }) } diff --git a/api/mappacks.js b/api/mappacks.js index 7477b1c..eca93f4 100644 --- a/api/mappacks.js +++ b/api/mappacks.js @@ -3,17 +3,17 @@ let cache = {} module.exports = async (app, req, res) => { - if (req.offline) return res.send("-1") + if (req.offline) return res.status(500).send("-1") let cached = cache[req.id] - if (app.config.cacheMapPacks && cached && cached.data && cached.indexed + 5000000 > Date.now()) return res.send(cached.data) // 1.5 hour cache + if (app.config.cacheMapPacks && cached && cached.data && cached.indexed + 5000000 > Date.now()) return res.status(200).send(cached.data) // 1.5 hour cache let params = { count: 250, page: 0 } let packs = [] function mapPackLoop() { req.gdRequest('getGJMapPacks21', params, function (err, resp, body) { - if (err) return res.send("-1") + if (err) return res.status(500).send("-1") let newPacks = body.split('#')[0].split('|').map(x => app.parseResponse(x)).filter(x => x[2]) packs = packs.concat(newPacks) @@ -36,7 +36,7 @@ module.exports = async (app, req, res) => { })) if (app.config.cacheMapPacks) cache[req.id] = {data: mappacks, indexed: Date.now()} - return res.send(mappacks) + return res.status(200).send(mappacks) }) } mapPackLoop() diff --git a/api/profile.js b/api/profile.js index 55e0589..30ce2f1 100644 --- a/api/profile.js +++ b/api/profile.js @@ -4,7 +4,7 @@ module.exports = async (app, req, res, api, getLevels) => { if (req.offline) { if (!api) return res.redirect('/search/' + req.params.id) - else return res.send("-1") + else return res.status(500).send("-1") } let username = getLevels || req.params.id @@ -42,7 +42,7 @@ module.exports = async (app, req, res, api, getLevels) => { if (err2 || dumbGDPSError) { if (!api) return res.redirect('/search/' + req.params.id) - else return res.send("-1") + else return res.status(500).send("-1") } if (!foundID) app.userCache(req.id, account[16], account[2], account[1]) @@ -78,7 +78,7 @@ module.exports = async (app, req, res, api, getLevels) => { glow: account[28] == "1", } - if (api) return res.send(userData) + if (api) return res.status(200).send(userData) else fs.readFile('./html/profile.html', 'utf8', function(err, data) { let html = data; @@ -87,7 +87,7 @@ module.exports = async (app, req, res, api, getLevels) => { let regex = new RegExp(`\\[\\[${x.toUpperCase()}\\]\\]`, "g") html = html.replace(regex, app.clean(userData[x])) }) - return res.send(html) + return res.status(200).send(html) }) }) diff --git a/api/search.js b/api/search.js index 74591cc..6bcd36d 100644 --- a/api/search.js +++ b/api/search.js @@ -5,17 +5,17 @@ let demonList = {} module.exports = async (app, req, res) => { - if (req.offline) return res.send(req.query.hasOwnProperty("err") ? "err" : "-1") + if (req.offline) return res.status(500).send(req.query.hasOwnProperty("err") ? "err" : "-1") let demonMode = req.query.hasOwnProperty("demonlist") || req.query.hasOwnProperty("demonList") || req.query.type == "demonlist" || req.query.type == "demonList" if (demonMode) { - if (!req.server.demonList) return res.send('-1') + if (!req.server.demonList) return res.status(400).send('-1') let dList = demonList[req.id] if (!dList || !dList.list.length || dList.lastUpdated + 600000 < Date.now()) { // 10 minute cache return request.get(req.server.demonList + 'api/v2/demons/listed/?limit=100', function (err1, resp1, list1) { - if (err1) return res.send("-1") + if (err1) return res.status(500).send("-1") else return request.get(req.server.demonList + 'api/v2/demons/listed/?limit=100&after=100', function (err2, resp2, list2) { - if (err2) return res.send("-1") + if (err2) return res.status(500).send("-1") demonList[req.id] = {list: JSON.parse(list1).concat(JSON.parse(list2)).map(x => String(x.level_id)), lastUpdated: Date.now()} return app.run.search(app, req, res) }) @@ -85,7 +85,7 @@ module.exports = async (app, req, res) => { filters.str = demonMode ? demonList[req.id].list : filters.str.split(",") listSize = filters.str.length filters.str = filters.str.slice(filters.page*amount, filters.page*amount + amount) - if (!filters.str.length) return res.send("-1") + if (!filters.str.length) return res.status(400).send("-1") filters.str = filters.str.map(x => String(Number(x) + (+req.query.l || 0))).join() filters.page = 0 } @@ -95,7 +95,7 @@ module.exports = async (app, req, res) => { req.gdRequest('getGJLevels21', req.gdParams(filters), function(err, resp, body) { - if (err) return res.send("-1") + if (err) return res.status(500).send("-1") let splitBody = body.split('#') let preRes = splitBody[0].split('|') let authorList = {} @@ -154,7 +154,7 @@ module.exports = async (app, req, res) => { }) if (filters.type == 10) parsedLevels = parsedLevels.slice((+filters.page) * amount, (+filters.page + 1) * amount) - return res.send(parsedLevels) + return res.status(200).send(parsedLevels) }) } \ No newline at end of file diff --git a/api/song.js b/api/song.js index 0dc6573..3668ae3 100644 --- a/api/song.js +++ b/api/song.js @@ -4,15 +4,15 @@ module.exports = async (app, req, res) => { // temporary solution until song api is re-enabled - if (req.offline) return res.send('-1') + if (req.offline) return res.status(500).send('-1') let songID = req.params.song req.gdRequest('getGJSongInfo', {songID: songID}, function(err, resp, body) { - if (err) return res.send('-1') + if (err) return res.status(400).send('-1') else if (body < 0) return res.send(false) request.get('https://www.newgrounds.com/audio/listen/' + songID, function(err2, resp2, song) { console.log(resp2.statusCode) - return res.send(resp2.statusCode == 200) + return res.status(200).send(resp2.statusCode == 200) }) }) }