• throw error in fetch with response text

    From Andrew Poulos@ap_prog@hotmail.com to comp.lang.javascript on Wed May 29 10:23:48 2019
    From Newsgroup: comp.lang.javascript

    With this

    fetch(url)
    .then (function(response) {
    let contentType = response.headers.get("content-type");
    if (contentType && contentType.indexOf("text/html") !== -1) {
    throw response.text();
    } else {
    return response.blob();
    }
    })
    .then (blob => window.URL.createObjectURL(blob))
    .then (ourl => {
    // do stuff
    })
    .catch(function(error) {
    window.alert(err);
    });

    when there's an error the server returns some text describing the error (instead of a blob when there's no error).

    The alert though only displays [object Promise]. How do I get the 'PromiseValue' text into the alert?

    Andrew Poulos
    --- Synchronet 3.17c-Linux NewsLink 1.110
  • From Andrew Poulos@ap_prog@hotmail.com to comp.lang.javascript on Wed May 29 10:33:33 2019
    From Newsgroup: comp.lang.javascript

    On 29/05/2019 10:23 am, Andrew Poulos wrote:
    With this

    fetch(url)
    .then (function(response) {
      let contentType = response.headers.get("content-type");
      if (contentType && contentType.indexOf("text/html") !== -1) {
        throw response.text();
      } else {
        return response.blob();
      }
    })
    .then (blob => window.URL.createObjectURL(blob))
    .then (ourl => {
      // do stuff
    })
    .catch(function(error) {
      window.alert(err);
    });

    when there's an error the server returns some text describing the error (instead of a blob when there's no error).

    The alert though only displays [object Promise]. How do I get the 'PromiseValue' text into the alert?

    Of course 2 minutes after I posted I found another post that solved my
    issue:

    fetch(url)
    .then (function(response) {
    let contentType = response.headers.get("content-type");
    if (contentType && contentType.indexOf("text/html") !== -1) {
    throw response;
    } else {
    return response.blob();
    }
    })
    .then (blob => window.URL.createObjectURL(blob))
    .then (ourl => {
    // do stuff
    })
    .catch((error) => {
    error.text().then( msg => window.alert(msg));
    });


    Andrew Poulos
    --- Synchronet 3.17c-Linux NewsLink 1.110