0

I've had this script working for over 3 months with no issues, however, last week it started throwing out this:

Error   
Exception: Request failed for https://hooks.slack.com returned code 400. Truncated server response: invalid_blocks (use muteHttpExceptions option to examine full response)

Here's the relevant part of the script i think.

  for (let i = 0; i in vistasVencimiento; i++){
    if (vistasVencimiento[i][0] instanceof Date == true){
      let differenceInMs = dateToday - vistasVencimiento[i][0];
      let differenceInDaysRaw = differenceInMs / (1000 * 60 * 60 * 24);

      if(differenceInDaysRaw >= 0 && statusColumn[i] !== "Pendiente_de_despacho"){
        //agregar a array de vencidos
        result.push(sumariantesVistas[i], causasCaratulas[i], '\n');
      }
    }
  };
  console.log(result);
  if (result === undefined || result.length === 0 ){
    let payload ={
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": ":bell: *Cuales Vistas estan vencidas?* :bell:"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text":"Aparentemente, ninguna"
            }
        },
    ]
  }
      let headers = {
      'Content-type': 'application/json'
    };
    let options = {
      headers : headers,
      method: 'POST',
      payload: JSON.stringify(payload)
    };
  UrlFetchApp.fetch(url, options);

  }else{
    let result_string = result.join('\n');
    let payload ={
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": ":bell: *Cuales Vistas estan vencidas?* :bell:"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text":"Estas: "
            }
        },
        { 
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": result_string
            }
        },      
    ]
  }
      let headers = {
      'Content-type': 'application/json'
    };
    let options = {
      muteHttpExceptions: false,
      headers : headers,
      method: 'POST',
      payload: JSON.stringify(payload)
    };
  UrlFetchApp.fetch(url, options);

  };

Now, I know its related to "text": result_string because ive tried replacing that with something like "text": "test" and it works, but the whole point of the notifications is to send that formatted string to a slack workplace channel, and thats where ive basically no clue on why it would start failing out of a sudden like that

edit: Theres something genuinely very weird going on here, first of all going causasCaratulas[i].replace(/"/g, "'") doesnt fix anything, second, causasCaratulas has two columns inside, if i load only one column (doesnt matter which) the script works, ive even tried splitting the array into two sorta-lists, in that case, it still throws out the error. So the error happens somewhere between trying to send out those two columns to the slack api at the same time? im clueless as to whats happening here

edit 2: the data is structured something like this. the problem seems to be that whenever i try sending both Case Number and Case Name at the same time i get an error, however, this seems to be dependant on some kind of mystery symbol idk how to identify, if it even is a symbol.enter image description here

IFritzler
  • 57
  • 10
  • so, what is the result of `console.log(result);` when the error occurs? – Bravo Jun 21 '22 at 12:49
  • @Bravo, a bunch of names and court id numbers basically, that part is working fine, in fact i just checked, it used to work fine until about 10 days ago, and nothing changed afaik – IFritzler Jun 21 '22 at 13:01
  • so, in short, it's an Array of strings? – Bravo Jun 21 '22 at 13:08
  • @Bravo, yes, but before trying to send that result i do let result_string = result.join('\n'); to send it as a string, cause afaik you cant send arrays thru blocks – IFritzler Jun 21 '22 at 13:13
  • I just wonder if the `\n` is suddenly an issue on the server - to test, join with something else - or instead of `"text": "test"` send `"text": "test\ntest"` and see if the issue happens – Bravo Jun 21 '22 at 13:17
  • I've now tried both doing `"text" : "test\ntest"` and doing a sort of test string thing `let test_string = "test"`, and both work, so uhhhh, hm, i still got no clue – IFritzler Jun 21 '22 at 13:20
  • Can you explain what the contents for `sumariantesVistas[i], causasCaratulas[i]` are? – Emel Jun 21 '22 at 13:28
  • @Emel, yes, theyre both strings. – IFritzler Jun 21 '22 at 13:32
  • Can you check, via `console.log` that the contents of the result var are the expected ones? If everything is working until now, something should have changed. Also, where do these values come from? A fetched resource or data from sheet? – Emel Jun 21 '22 at 13:40
  • @Emel yes. the contents of `result` are expected, the values come from arrays im loading up with data from a sheet. and no, thats the issue, nothing has changed, thats whats confusing me rn – IFritzler Jun 21 '22 at 13:47
  • @IlanFritzler That's odd. Can you share a sanitized copy of the sheet? Also, you can try making a copy of the Spreadsheet and check if the error persists? Is it a bounded script or a standalone? – Emel Jun 21 '22 at 13:50
  • I just tried to run the script on a copy of the sheet (where i replaced all the case numbers with a simple "censored" and it works fine???? SIGH what the heck – IFritzler Jun 21 '22 at 14:03
  • @IlanFritzler Check the answer posted. Consider accepting or up-voting it. – Emel Jun 21 '22 at 14:03
  • @Emel, im not sure if its that either since i just tried it on an identical copy and it failed. so it must be something else. – IFritzler Jun 21 '22 at 14:04
  • @IlanFritzler What do you mean with `replaced all the case numbers with a simple "censored"` ? – Emel Jun 21 '22 at 14:07
  • @Emel, sorry, im panicking a bit and i misspoke, in the sheets doc theres a column called "Caratula" in that column there are the names of the cases currently being worked on by the court, when i replaced all the values there with "censored" it worked, so im guessing something must've happened or some symbol was used and now thats causing the error. – IFritzler Jun 21 '22 at 14:11
  • @IlanFritzler Can you share a sample of the `Caratula` values? What kind of characters are included ? If you don't share a copy of your sanitized copy of the sheets it is very difficult for the community to provide help. – Emel Jun 21 '22 at 14:21
  • @Emel, here's a midly sanitized sheet, https://docs.google.com/spreadsheets/d/11bZ-fHy3cXbHY4IDgQmiRoBTJyq4s40p0ZDznrAvxxo/edit?usp=sharing – IFritzler Jun 21 '22 at 14:28
  • Seems there is some sensible data in your sheet, be careful on what you share. – Emel Jun 21 '22 at 14:31
  • @Emel, i know, im gonna delete the file as soon as ive sorted the issue, but for now i kinda need that data there because whats causing the error seems to be in there especifically. – IFritzler Jun 21 '22 at 14:34
  • `JSON.stringify` should clean all Unicode characters except for `"` and `/`, so try to replace the ones present in the Caratulas. Try this, `causasCaratulas[i].replace(/"/g, "'")` I strongly recommend you to sanitize before sharing. – Emel Jun 21 '22 at 14:40
  • @Emel, ive deleted the gdocs file already so thanks for the warning and also for the (Yet to be tested) solution – IFritzler Jun 21 '22 at 14:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245794/discussion-between-ilan-fritzler-and-emel). – IFritzler Jun 21 '22 at 14:53
  • @IlanFritzler Also clean the `\` chars if there's any. I made a mistake in my previous comment, you can check this [answer](https://stackoverflow.com/questions/12271547/shouldnt-json-stringify-escape-unicode-characters) for full explanation. – Emel Jun 21 '22 at 14:58
  • Side note: Use [tables](https://webapps.stackexchange.com/a/161855/) to show your data structure. If you share spreadsheets, do note that [your email address can be accessed by the public](https://meta.stackoverflow.com/questions/394304/). – TheMaster Jun 21 '22 at 15:38
  • @TheMaster, thank you, ill see what i can do – IFritzler Jun 21 '22 at 15:46
  • @TheMaster tried doing the markdown table thing but even tho it showed properly when previewing it didnt rly work when actually editing, so i added an image of it as a sort of workaround – IFritzler Jun 21 '22 at 15:54
  • You just need to add a empty line before the top of the table. – TheMaster Jun 21 '22 at 16:20
  • Have you managed to solve the issue? – Emel Jun 28 '22 at 08:18
  • @Emel, i havent, i managed to sort of avoid it by sending two columns instead of three but it still throws out an error about 50% of the time, its really weird and im not sure anymore if its a me error or something wrong with the slack api. – IFritzler Jun 29 '22 at 12:48
  • Try to update and reduce your question to refer to what the actual problem is. Or formulate a new one – Emel Jun 30 '22 at 08:35
  • @Emel thats not an entirely bad idea. – IFritzler Jul 01 '22 at 12:25

1 Answers1

1

Perhaps your text data contains &<>? Try this:

    let result_string = result
      .join('\n')
      .replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');

See Escaping text at Slack API reference.

You should also search your data for markdown symbols that could throw a spanner in the works.

doubleunary
  • 13,842
  • 3
  • 18
  • 51
  • just tried this, does not seem to work either, im guessing theres something wrong with the array per se? but ive 2 other scripts that do basically the same thing and ive gotten 0 errors with those. – IFritzler Jun 21 '22 at 13:24
  • You could well have other issues with your markdown syntax. It is unlikely that you will get a solution here unless you share sample data, or at minimum the output of `console.log(result_string)`, that reproduces the issue. – doubleunary Jun 21 '22 at 13:48
  • it must be something like this, im checking the sheet data, yet i cannot figure out what symbol is causing the issue – IFritzler Jun 21 '22 at 14:14