Understanding Console Error

I am just knowledgeable with JavaScript to be dangerous, so this may be a super basic question.

I’m receiving the following error message in the console of scriptable when running a script I’m working on and have no idea what it means.

Error on line 0:10: The data couldn’t be read because it isn’t in the correct format.

All other errors I’ve been getting as I debug have been on just live 15. What does line 0:10 mean? Maybe that will help me understand what data is wrong.

Could you share the script itself? It looks like it’s failing right at the beginning, but seeing the script will be helpful for debugging :slight_smile:

let req = new Request("http://micro.blog/posts/mentions");
req.headers = {"Authorization" : "Token  "}; // My token went here


let json = await req.loadJSON();

let table = new UITable()
for (i = 0; i < 4; i++) {
  let row = new UITableRow()
  let bodyHTML = json["_microblog"]["items"][i]["content_html"]
  let imageURL = json["_microblog"]["items"][i]["author"]["_microblog"]["avatar"]
  let user = json["_microblog"]["items"][i]["author"]["_microblog"]["username"]

  let body = decode(bodyHTML)
  let imageCell = row.addImageAtURL(imageURL)
  imageCell.widthWeight = 20
  titleCell.widthWeight = 80
  row.height = 60
  row.cellSpacing = 10
  table.addRow(row)
}
  

function decode(str) {
  let regex = /&#(\d+);/g
  return str.replace(regex, (match, dec) => {
    return String.fromCharCode(dec)
  })
}

QuickLook.present(table)

if (config.runsWithSiri) {
  Speech.speak("Here's the latest news.")
}

The confusion here is due to a bug / current limitation in Scriptable. When it’s unable to report a line number (for whatever reason) it’s reporting line number 0. However, I just tried your script and it seems to be your request that’s failing. It’s actually on this line:

let json = await req.loadJSON();

Make sure that the request succeeds and that the response is actually valid JSON.

As for the incorrect line number, I’ll look into it and your script is a great help as it I can easily reproduce the issue then.

2 Likes

That error appears in a lot of languages relating to the data set that the code is attempting to process.

Start by checking the data you get back. Do you get any data back? Is it definitely valid JSON? Are the references you are making to the data definitely accurate for the returned structure.

Hmm. This is what I get when I run the same request in Shortcuts using Get Contents of URL.

I can’t figure out any reason why Scriptable wouldn’t be getting the same thing.

Well Scriptable is in beta and it isn’t Workflow, so I have to ask, do you get an identical result in Scriptable?

The screenshot also doesn’t note if the JSON is valid. It actually doesn’t show all of it from what I can see. I’d recommend copying the content and using an online validator just to ensure it is 100% valid.

Also, a heavy handed but reliable approach on the line finding front is to try commenting the lines out in reverse order, functions aside, until the error occurs. This should help nail down where the error is occurring.

What do you see in the console log when you try this? (so without “JSON”)

let json = await req.load();

If I try this with a valid Micro.blog token I get some HTML code that informs me that processing the request fails:

<html>
    <style>
body {
	font-family: "Avenir Next", "Segoe UI", "Verdana";
	font-size: 16px;
	line-height: 25px;
}

p {
	padding-top: 4px;
}
</style>
    <body>
        <p>Error while processing the request. Please try again or email 
            <a href="mailto:help@micro.blog">help@micro.blog</a>.
        </p>
    </body>
</html>

However, if I do the same thing in Postman on my Mac I do get valid JSON…

I’m seeing the same thing.

I also tried it in Drafts using its JavaScript engine and got valid JSON data.

One thing I noticed in Drafts though was when I got the response text, it worked. When I got response data and then used JSON.stringify, it didn’t work. I logged the data response and instead of the normal [object Object], I saw [object OS_dispatch_data]. A quick google search let me know that this is something objective c related, but that’s way above my pay grade.

I should also add that I’m on a Drafts beta, so now the problem could be Drafts, Micro.blog, or Scriptable, and I’m more lost than ever. :man_shrugging:t2:

What you’re experiencing is a bug in Scriptable. I’ve tested your script in the latest build and have confirmed that it fixes the issue. Thanks for reporting the issue.

5 Likes

Ok cool, thanks. Well, I’m overall glad I had this problem if it helped the developer find a bug and found out another was an issue with the app instead of my JavaScript :joy:.

Thanks for the help everyone. And thanks for making Scriptable @simonbs! Can’t wait to dig in more.

1 Like

I thought I would update with my working script here for any Micro.blog users.

let req = new Request("https://micro.blog/posts/mentions");
req.headers = {"Authorization": "Token "}; // insert token here
req.method = "GET";


let json = await req.loadJSON();

let table = new UITable()
for (i = 0; i < 5; i++) {
  let row = new UITableRow()
  let bodyHTML = json['items'][i]['content_html']
	let imageURL = json["items"][i]["author"]["avatar"]
  let user = json["items"][i]["author"]["_microblog"]["username"]

  let body = decode(bodyHTML);
  let imageCell = row.addImageAtURL(imageURL)
  let titleCell =row.addText(user, body)
  imageCell.widthWeight = 15
  titleCell.widthWeight = 85
  row.height = 75
  row.cellSpacing = 5
  table.addRow(row)
}
  

function decode(str) {
  let regex = /<[^>]*>/g
  return str.replace(regex, "")
}

QuickLook.present(table)

if (config.runsWithSiri) {
  Speech.speak("Here's what people are saying to you on Micro.blog.")
}

Works like a charm in the newest build. Thanks everyone for the help!

1 Like