Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #36660 > unrolled thread

throw error in fetch with response text

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2019-05-29 10:23 +1000
Last post2019-05-29 10:33 +1000
Articles 2 — 1 participant

Back to article view | Back to comp.lang.javascript


Contents

  throw error in fetch with response text Andrew Poulos <ap_prog@hotmail.com> - 2019-05-29 10:23 +1000
    Re: throw error in fetch with response text Andrew Poulos <ap_prog@hotmail.com> - 2019-05-29 10:33 +1000

#36660 — throw error in fetch with response text

FromAndrew Poulos <ap_prog@hotmail.com>
Date2019-05-29 10:23 +1000
Subjectthrow error in fetch with response text
Message-ID<LMqdnXFmEY6FTHDBnZ2dnUU7-QXNnZ2d@westnet.com.au>
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

[toc] | [next] | [standalone]


#36661

FromAndrew Poulos <ap_prog@hotmail.com>
Date2019-05-29 10:33 +1000
Message-ID<Mc-dnf9Ldqf9TnDBnZ2dnUU7-Q-dnZ2d@westnet.com.au>
In reply to#36660
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web