AlexAtHome / random-reddit

Random Reddit posts and images
MIT License
6 stars 1 forks source link

invalid json response body: Unexpected token < in JSON at position 0 #6

Closed Tormidal closed 2 years ago

Tormidal commented 3 years ago

Hi!

I've noticed this happens on occasion: if this is an issue with node-fetch, feel free to call me and idiot and close this issue. :)

They mostly all seem to have non-standard characters in the URL; however the webpages and JSON source pages themselves are still accessible.

FetchError: invalid json response body at https://www.reddit.com/r/wholesomehentai/comments/ntzp4r/mikuni_mizuki_the_country_virgin_fiancée/.json?utm_campaign=redirect&utm_medium=desktop&utm_source=reddit&utm_name=random_link reason: Unexpected token < in JSON at position 0
    at D:\Desktop\discord bot\node_modules\node-fetch\lib\index.js:272:32
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Object.makeRequest (D:\Desktop\discord bot\node_modules\random-reddit\dist\utils.js:38:18)
    at async Object.getPost (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:12:22)
    at async getImage (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:27:16)
    at async getpic (D:\Desktop\discord bot\bot2.js:74:17)
    at async D:\Desktop\discord bot\bot2.js:241:15 {
  type: 'invalid-json'
}
FetchError: invalid json response body at https://www.reddit.com/r/Nekomimi/comments/o7l5f6/schwarz_鈴éやつみ_arknights/.json?utm_campaign=redirect&utm_medium=desktop&utm_source=reddit&utm_name=random_link reason: Unexpected token < in JSON at position 0
    at D:\Desktop\discord bot\node_modules\node-fetch\lib\index.js:272:32
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Object.makeRequest (D:\Desktop\discord bot\node_modules\random-reddit\dist\utils.js:38:18)
    at async Object.getPost (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:12:22)
    at async getImage (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:27:16)
    at async getpic (D:\Desktop\discord bot\bot2.js:74:17)
    at async D:\Desktop\discord bot\bot2.js:241:15 {
  type: 'invalid-json'
}
FetchError: invalid json response body at https://www.reddit.com/r/AzurLewd/comments/obfzll/ägir/.json?utm_campaign=redirect&utm_medium=desktop&utm_source=reddit&utm_name=random_link reason: Unexpected token < in JSON at position 0
    at D:\Desktop\discord bot\node_modules\node-fetch\lib\index.js:272:32
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Object.makeRequest (D:\Desktop\discord bot\node_modules\random-reddit\dist\utils.js:38:18)
    at async Object.getPost (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:12:22)
    at async getImage (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:27:16)
    at async getpic (D:\Desktop\discord bot\bot2.js:81:17)
    at async D:\Desktop\discord bot\bot2.js:248:15 {
  type: 'invalid-json'
}
FetchError: invalid json response body at https://www.reddit.com/r/Nekomimi/comments/o6fqoz/beidou_マサキナナヤ_genshin_impact/.json?utm_campaign=redirect&utm_medium=desktop&utm_source=reddit&utm_name=random_link reason: Unexpected token < in JSON at position 0
    at D:\Desktop\discord bot\node_modules\node-fetch\lib\index.js:272:32
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Object.makeRequest (D:\Desktop\discord bot\node_modules\random-reddit\dist\utils.js:38:18)
    at async Object.getPost (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:12:22)
    at async getImage (D:\Desktop\discord bot\node_modules\random-reddit\dist\functions.js:27:16)
    at async getpic (D:\Desktop\discord bot\bot2.js:49:17)
    at async D:\Desktop\discord bot\bot2.js:168:15 {
  type: 'invalid-json'
}
AlexAtHome commented 3 years ago

Hello! Can you please send the code example that causes this errors? Also, please, clarify, what do you use as an argument for getImage.

Tormidal commented 3 years ago

Of course!

the arguments are always in the form of the subreddit name(minus the r/; e.g. ecchi or nekomimi)

My nested calls looks like this:

async function getpic(fromSubreddit) {
    console.log(`received ${fromSubreddit}`);
    try {
        const image = await getImage(fromSubreddit);
        console.log(`retrieved pic at ${image}`);
        return image;
    } catch (error) {
        console.log(error);
        return error;
    }
}
let image = '';
        (async() => {
            image = await getpic(categories[command][0]);
                message.reply(image);
        })();
        console.log(`{LOG}User: ` + message.author.username + `, just used the ${command} command`);
        return;

It works flawlessly until it hits these posts - which all seem to have non-standard characters.

Here's an example of this faulty result vs. a normal/working results(warning potentially nsfw: https://i.gyazo.com/46c93eaf5c01c30be540cba938d44f76.png)

AlexAtHome commented 3 years ago

@Tormidal, can you check out the 2.0.2-2 version (from #5) and see if the problem still persists?

Tormidal commented 3 years ago

@Tormidal, can you check out the 2.0.2-2 version (from #5) and see if the problem still persists?

Still persists on 2.0.2-2:

post was titled "Ägir in a Semi Bodysuit" but link shows as below "ägir_in_a_semi_bodysuit"

{ERROR}: caught error at FetchError: invalid json response body at https://www.reddit.com/r/AzurLewd/comments/ok4590/ägir_in_a_semi_bodysuit/.json?utm_campaign=redirect&utm_medium=desktop&utm_source=reddit&utm_name=random_link reason: Unexpected token < in JSON at position 0
AlexAtHome commented 2 years ago

Can't reproduce the bug in version 2.0.3. I consider it fixed since I don't use random.json approach anymore and I just pick a random post from a list, therefore there should not be any problems with parsing a HTML as a JSON.