0

Please see the code herein under:

function binanceOrderBook() {
  try {
    muteHttpExceptions = true;    
    var workSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var mySheet = workSpreadsheet.getSheetByName('Order Books');
    
    if(mySheet == 'Sheet'){
      mySheet.activate();
    } else {
      mySheet = workSpreadsheet.insertSheet('Order Books', 1).activate();
    }
    
    var ui = SpreadsheetApp.getUi();
    var string = 'https://api.binance.com/api/v3/depth?';
    var symbolResponse = ui.prompt('Pair Name', 'Please enter the pair symbol.\n\nExamples: BTCUSDT or ETHBTC:', ui.ButtonSet.OK_CANCEL);
    var symbolButton = symbolResponse.getSelectedButton();
    if(symbolButton == ui.Button.CANCEL){return}
    var mySymbol = symbolResponse.getResponseText();
    mySymbol = mySymbol.toUpperCase();
    string = string + "symbol=" + mySymbol;
    
    var limitResponse = ui.prompt('Limit:', 'Please enter Limit (Period Quantity).\nValid limits are:5, 10, 20, 50, 100, 500, 1000. \n Default limit is 100.\n You can leave it blank and simply click OK.', ui.ButtonSet.OK_CANCEL);    
    if(limitResponse.getSelectedButton() == ui.Button.CANCEL){return}
    var myLimit = Number(limitResponse.getResponseText());
    
    if(myLimit != 5 && myLimit != 10 && myLimit != 20 && myLimit != 50 && myLimit != 100 && myLimit != 500 && myLimit != 1000){myLimit = 100;}
    
    string = string + "&limit=" + myLimit;
    
    var myDate = new Date().toUTCString();
    var jsonOrderBookData = JSON.parse(UrlFetchApp.fetch('https://api.binance.com/api/v3/depth?symbol=' + mySymbol + '&limit=' + myLimit));
    reporter(jsonOrderBookData);
  } catch (e){
    exceptionHandler(e)
  }
}

The problem I have is to run UrlFetchApp.fetch again when it encounters an error. I need to run it several times to get the result. So, I need to prevent the script from stopping when an error (code -1003) occurs, but how can I do that?

EDIT: There is a function windows.onerror in javascript which can be set to prevent the program from stopping. Is it useable in GAS? if yes, how? if No, is there a similar solution for GAS?

Community
  • 1
  • 1
user9830671
  • 13
  • 1
  • 6

1 Answers1

1

You could call binanceOrderBook() from within your catch statement. E.g.

...
} catch (e){
  binanceOrderBook()
  exceptionHandler(e)
}

Of course you probably should have some condition that exits the function if a certain error occurs, or if you know that the function needs to run no more than x number of times you could check that it has run less than x times before executing. For example,

const maxValue = 10 // whatever the max number of executions should be

function binanceOrderBook(executions) {
  if (executions >= maxValue) return;
  try {
   ...
  } catch(e) {
   binanceOrderBook((executions || 0) + 1));
   exceptionHandler(e); // note that I am including this here because it's in your original example, but as it is written now, exception handler won't be called until binanceOrderBook executes without an error.
  }
  
}

[Edit] To answer your second question, there is no equivalent to window.onerror that I know of in GAS. However, window.onerror is a global event handler and so would affect errors thrown by any functions defined in your project. To address a concern with a single function call like this, you are better off using a try catch statement as you have.

RayGun
  • 431
  • 2
  • 9