Domo Connector trap errors

I have created a connector which works fine in Dev and has no problems when creating a dataset from Dev - however when I publish it I get error

'An error occurred during processing: Domo is ready, but the script has errors: "SyntaxError: Unexpected token < in JSON at position 0"; at line number 7; '

There is no problem with the code but apparently there is something in the returned data which DOMO Dev has no problem with however DOMO Prod cannot handle.

It seems to occur when I try to start Parsing the data "var jsonRes = JSON.parse(res);".

I there a good way to catch this error?

I'm pulling the data in pages of 10 so this section is:

for(i=0;i<1000;++i) {

var res = httprequest.get(baseUrl + "?cursor=" + Cursor);
var jsonRes = JSON.parse(res);
allData = allData.concat(jsonRes.data);
Cursor = jsonRes.pagination.nextCursor;

if(Cursor == null) {
break;
}
}

Best Answer

  • ArborRose
    ArborRose Coach
    Answer ✓

    The seems to be pointing to an invalid JSON response. It indicates the response might be HTML…such as when an API call returns an error message. You could try to capture what is happening by adding some error handling code.

    for (var i = 0; i < 1000; ++i) {
    var res = httprequest.get(baseUrl + "?cursor=" + Cursor);

    try {
    // Check if the response is valid JSON
    var jsonRes = JSON.parse(res);

    // If parsing was successful, process the data
    allData = allData.concat(jsonRes.data);
    Cursor = jsonRes.pagination.nextCursor;

    // Break the loop if there is no next cursor
    if (Cursor == null) {
    break;
    }
    } catch (e) {
    // Log the error and response for debugging
    console.log("Error parsing JSON at iteration " + i + ": " + e.message);
    console.log("Response: " + res);

    // Handle different scenarios depending on what the response contains
    if (res.includes("<html>")) {
    console.log("Received HTML instead of JSON. Possible server error or wrong endpoint.");
    } else {
    console.log("Unexpected response format.");
    }

    // Optionally, you can choose to break or continue based on the error
    break; // Stop execution or continue to the next iteration
    }
    }

    ** Was this post helpful? Click Agree or Like below. **
    ** Did this solve your problem? Accept it as a solution! **

Answers

  • ArborRose
    ArborRose Coach
    Answer ✓

    The seems to be pointing to an invalid JSON response. It indicates the response might be HTML…such as when an API call returns an error message. You could try to capture what is happening by adding some error handling code.

    for (var i = 0; i < 1000; ++i) {
    var res = httprequest.get(baseUrl + "?cursor=" + Cursor);

    try {
    // Check if the response is valid JSON
    var jsonRes = JSON.parse(res);

    // If parsing was successful, process the data
    allData = allData.concat(jsonRes.data);
    Cursor = jsonRes.pagination.nextCursor;

    // Break the loop if there is no next cursor
    if (Cursor == null) {
    break;
    }
    } catch (e) {
    // Log the error and response for debugging
    console.log("Error parsing JSON at iteration " + i + ": " + e.message);
    console.log("Response: " + res);

    // Handle different scenarios depending on what the response contains
    if (res.includes("<html>")) {
    console.log("Received HTML instead of JSON. Possible server error or wrong endpoint.");
    } else {
    console.log("Unexpected response format.");
    }

    // Optionally, you can choose to break or continue based on the error
    break; // Stop execution or continue to the next iteration
    }
    }

    ** Was this post helpful? Click Agree or Like below. **
    ** Did this solve your problem? Accept it as a solution! **

  • Thanks :)