question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling response.json() when processing an error request (40x) returns a promise, not the data in the body

See original GitHub issue

When I send an error from my restful api back to the server, I want to include a message. Flask has nice support for this and I can say:

if not token:
    return {‘message’: ‘Missing token’}, 401

I can see the data in the body of the response and content-type is application/json.

In my app I’m using isomorphic-fetch and I’m saying:

fetch('...url...')
    .then(response => {
        if (!response.ok) {
            const data = response.json();
            ...

Trouble is, data always comes out being an unresolved promise. If I poke around inside using the debugger I can see my data (it’s in [[PromiseValue]]) but I can’t get it. I’ve tried calling .then() on it to try to resolve it but that just seems to generate another Promise.

How is this supposed to work? This code works fine when the response isn’t an error.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
trixn86commented, Jun 29, 2018

@bggolding response.json() returns a Promise according to the specs that resolves with the deserialized data.

It is specified in the Body Mixin that is implemented by the Request as well as the Response object. See https://developer.mozilla.org/en-US/docs/Web/API/Body/json and https://fetch.spec.whatwg.org/#body-mixin for details.

How is this supposed to work? This code works fine when the response isn’t an error.

I would expect data to always be a Promise. Could you show the code that runs when response.ok evaluates to true? It should not work if you handle it the same way.

A working example would be:

fetch('...url...')
    .then(response => {
        if (!response.ok) {
            response.json().then(data => {
                // handle data
            });
            ...
0reactions
matthew-andrewscommented, Sep 14, 2020

I agree with @trixn86’s diagnosis …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch JS retreive eror response - javascript - Stack Overflow
I am calling an API which could potentially return a 40x response with an associated error body which I want to use. I've...
Read more >
Response.json() - Web APIs - MDN Web Docs
It returns a promise which resolves with the result of parsing the body text as JSON . Note that despite the method being...
Read more >
How to get the response json on HTTP error 400+ ? · Issue #203
Suppose I create a request to http://example.com/404 , and the response is 404 status code with a json response like this: { "type":...
Read more >
The JavaScript fetch() method | Go Make Things
The Body.text() method gets the body data as a text string, while the Body.json() method gets it as a JSON object. Both return...
Read more >
Async JavaScript with Promises - Turing: Front-End Curriculum
Promises allow us to kick off an asynchronous process in the background and ... the body to a JSON data structure with response.json()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found