fix: handle rate limiting&request errors correctly

This commit is contained in:
ThaUnknown 2022-08-12 16:19:09 +02:00
parent e36445de70
commit b1f2d87d74
2 changed files with 9 additions and 6 deletions

View file

@ -1,6 +1,6 @@
{
"name": "Miru",
"version": "3.0.3",
"version": "3.0.4",
"author": "ThaUnknown_ <ThaUnknown@users.noreply.github.com>",
"main": "src/index.js",
"homepage": "https://github.com/ThaUnknown/miru#readme",

View file

@ -87,18 +87,21 @@ const limit = limiter.wrap(handleRequest)
async function handleRequest (opts) {
const res = await fetch('https://graphql.anilist.co', opts)
if (!res.ok && res.status === 429) return await limit(opts)
let json = null
try {
json = await res.json()
} catch (error) {
if (res.ok) printError(error)
if (error.status === 429) return await limit(opts)
}
if (!res.ok && json) {
for (const error of json?.errors || []) {
printError(error)
if (!res.ok) {
if (json) {
for (const error of json?.errors || []) {
printError(error)
}
} else {
printError(res)
}
if (res.status === 429) return await limit(opts)
}
return json
}