1
votes

I am using node.js (and discord.js) to make a discord bot.

I am using a GET request with the npm request module. Code works as expected when user types "!cat" it gets data from https://aws.random.cat/meow and posts a cat picture HOWEVER sometimes the server will give a 403 forbidden error which results in a HTML page instead of JSON and crashes the bot due to an unexpected token.

I am looking for a way to detect the HTML page and stop the code/post an error message either by detecting the endpoint is HTML instead of JSON, or the contents of the data sent back which would be:

  • JSON = { file: 'https://catlink.jpg' }
  • HTML 403 = <!DOCTYPE HTML PUBLIC...

IMG1: Error - HTML page response, IMG2: Expected responses 1 per request

My current code block is as follows:

//RANDOM CATS
if(command === "cat" || command === "meow"){
//Connection options
var catoptions = {
  url: "https://aws.random.cat/meow",
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application.json'
  }
};`

//Send the request
let request = require("request");
request(catoptions, function(err, response, body){
  if(err){
    console.log("error code #002");
  } else {
    //Receive the body of the JSON
    var catresult = JSON.stringify(body); //stringify incase page returns HTML 403 error - Will recieve "<!DOCTYPE HTML PUBLIC..." as first bit of data
    console.log(catresult) //Send to log to see if JSON "cat pic" data is returned or the HTML 403 error
    let meowdata = JSON.parse(body);

    //Responbse body
    let meowpic = meowdata.file;
    console.log(meowpic); //Send link to console
    message.channel.send(meowpic); //Send link to discord channel with discord.js
  }
});
} //END OF RANDOM CATS
1

1 Answers

0
votes

JSON.parse throws if an invalid JSON is passed, and HTML is not valid JSON, you have to catch the exception to avoid a crash.

From the docs:

Throws a SyntaxError exception if the string to parse is not valid JSON.

request(catoptions, function(err, response, body) {
    if (err) {
        console.log("error code #002");
    } else {

        try {
            //Receive the body of the JSON
            let catresult = JSON.stringify(body); //stringify incase page returns HTML 403 error - Will recieve "<!DOCTYPE HTML PUBLIC..." as first bit of data
            console.log(catresult) //Send to log to see if JSON "cat pic" data is returned or the HTML 403 error
            let meowdata = JSON.parse(body);

            //Responbse body
            let meowpic = meowdata.file;
            console.log(meowpic); //Send link to console
            message.channel.send(meowpic); //Send link to discord channel with discord.js

        } catch (e) {
            console.error(e);
        }
    }
});