Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-ok query response does not throw error with response status if JSON is returned in the body #2238

Closed
lukeggchapman opened this issue Oct 3, 2017 · 3 comments
Milestone

Comments

@lukeggchapman
Copy link

Intended outcome:

I'm attempting to identify if a query request I'm making is failing due to an access token expiring.

Actual outcome:

I'm receiving this error:
Network error: Server response was missing for query 'undefined'.
Which does not contain the error.networkError.response.status I need to determine the actual response status returned from the fetchAPI

#1205 added the response to the error so that the status could be identified, but #1491 made it so that it would only add it if the response is missing a valid JSON body.

This means that any non-ok response status that contains a JSON body will throw an error that does not have the response or response.status returned from the fetch.

How to reproduce the issue:

it('should throw an error with the response body when request is forbidden', () => {
  const unauthorizedUrlWithBody = 'http://unauthorized.test.with.body/';
  const unauthorizedResponse = {
    status: 403,
    body: {message: 'Forbidden message'}
  }
  fetchMock.mock(unauthorizedUrlWithBody, unauthorizedResponse);
  const unauthorizedInterface = createNetworkInterface({
    uri: unauthorizedUrlWithBody,
  });

  return unauthorizedInterface.query(doomedToFail).catch(err => {
    assert.isOk(err.response); // Fails here, err.response is undefined
    assert.equal(err.response.status, unauthorizedResponse.status);
    assert.equal(
      err.message,
      'Network request failed with status 403 - "Forbidden"',
    );
  });
});

Version

  • apollo-client@1.9.3
@jbaxleyiii
Copy link
Contributor

This is merged for the latest branch and I will cut a release this week. I've added it to the 2.0 milestone to update apollo-link-http to handle this case as well. Thanks for the great work @lukeggchapman

@jbaxleyiii
Copy link
Contributor

Moving to apollo-link

@khorramk
Copy link

Hello,

I was coming across the same problem. On my case. it was a server response problem. solved that using "data" before the response.
So your response need to be like this from the server side:
{ data: { id, name, etc } }

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants