loadTable - how to handle missing file error?

Hi,

I have a web-page served by node.js. In the web-page is a loadTable which loads a csv file from disk. loadTable is async and the draw action is controlled by iLoadData. In the good callback, this is incremented and the next phase uses the data in the table. This all works fine. When the the file is missing it goes into the ‘bad’ callback, but then crashes. The bad callback sets iLoadData to zero and there’s no more action in draw, but it still crashes. How should I handle this error?

Partial code:

function loadTableGood()
{
 console.log("T Good");
 iLoadData++;
}

function loadTableBad()
{
 console.log("T Bad " + iLoadData);
 iLoadData = 0;
}


void draw()
{
...

  if (iLoadData == 2)
  {
    table = loadTable(sFilename, 'csv', "", loadTableGood, loadTableBad);
  }

  if (iLoadData == 3)
  {
      // read out of the table.
  }
...  
}
[sketch.js:573:11](http://192.168.1.14:8083/sketch.js)
T Bad 2 [sketch.js:408:10](http://192.168.1.14:8083/sketch.js)
Uncaught (in promise) Error: [object Object]
n http://192.168.1.14:8083/p5.min.js:3
promise callback*[46]</v.prototype.httpDo http://192.168.1.14:8083/p5.min.js:3
loadTable http://192.168.1.14:8083/p5.min.js:3
draw http://192.168.1.14:8083/sketch.js:506

@GoToLoop My notifications list is showing you’ve replied (? pencil) but I can’t see the reply and the topic summary shows no replies.

The :pencil2: icon means a post got edited. In your case I’ve merely changed its forum category. :wink:

However as long as iLoadData is still 2 loadTable() will keep being invoked each draw() iteration! :warning:

It takes a while until iLoadData is either increased by 1 or reset back to 0. :grimacing:

It’d be nice if you had an actual runnable sketch demoing that for us to test it. :disappointed:

Anyways I’ve ended up coding 1 here: :innocent:

In my version it logs 2 errors on the browser’s console when it attempts to load the non-existing filename “ab.csv”: :bug:

  1. GET https://bl.ocks.org/GoSubRoutine/raw/823e9f8b008b7481b11d5f4f12a80839/ab.csv 404
  2. Uncaught (in promise) Error: [object ReadableStream] at p5:3:607922

However it doesn’t crash the sketch, as it goes on loading the correct filename “xy.csv” after that 1st fail and the sketch’s canvas keeps on changing its background() color. :art:

1 Like

@gotoloop thanks for all that.

  • you moved it, fine.
  • the problem with the phases in the code wasn’t quite the same as I posted. (At the time I couldn’t think how to extract a minimum working example from my sprawling development.) There was a problem with testing iLoadData and I was sometimes reading the table before it was ready. I thought the ‘uncaught’ was the problem but it has to be ignored.

Your example is interesting, several new-to-me useful things:

  • Uint8Array.from - array construction.
  • use strict - (= ‘option explicit’ in VB 20 years ago)
  • console.info - coloured messages in the log.

Thanks again.

1 Like