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

Development proxy doesn't return response body for 400 (4xx) status code #33333

Closed
2 tasks done
dangkyokhoang opened this issue Sep 29, 2021 · 3 comments
Closed
2 tasks done
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@dangkyokhoang
Copy link

Preliminary Checks

Description

This happens after upgrading to Gatsby 3.14.0 which depends on got 11.8.2.

The error event no longer has response as the third parameter. It's now a property of the error object.

.on(`error`, (err, _, response) => {

The error.response has .body and .rawBody, which possibly means that response body has already been consumed so piping just doesn't work as well. The fix is to res.end(response.rawBody) on error.

Reproduction Link

https://codesandbox.io/s/infallible-phoebe-69w1t?file=/src/pages/index.js

Steps to Reproduce

  1. Set proxy in gatsby-config.js
proxy: [
  {
    prefix: "/v3",
    url: "https://run.mocky.io"
  }
]
  1. yarn develop
  2. Try fetching this https://run.mocky.io/v3/99fc1661-4a34-4334-a123-ae9c02b33db4 which has 400 status code and {"error":1} response body.

Expected Result

Should receive 400 {"error":1}

Actual Result

Received 500 Internal Server Error

Environment

System:
    OS: Linux 5.4 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 14.17.6 -
/tmp/yarn--1632937225269-0.21021899177501946/node
    Yarn: 1.22.11 -
/tmp/yarn--1632937225269-0.21021899177501946/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v14.17.6/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    gatsby: ^3.14.0 => 3.14.0
    gatsby-plugin-image: ^1.14.0 => 1.14.0
    gatsby-plugin-manifest: ^3.14.0 => 3.14.0
    gatsby-plugin-offline: ^4.14.0 => 4.14.0
    gatsby-plugin-react-helmet: ^4.14.0 => 4.14.0
    gatsby-plugin-sharp: ^3.14.0 => 3.14.0
    gatsby-source-filesystem: ^3.14.0 => 3.14.0
    gatsby-transformer-sharp: ^3.14.0 => 3.14.0

Config Flags

No response

@dangkyokhoang dangkyokhoang added the type: bug An issue or pull request relating to a bug in Gatsby label Sep 29, 2021
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 29, 2021
@wardpeet wardpeet removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 29, 2021
@wardpeet wardpeet self-assigned this Oct 29, 2021
@carlos-algms
Copy link

carlos-algms commented Dec 27, 2021

I'm having something similar here.

One of my APIs returns multiple status codes depending on the business logic validation, and everything is being converted to 500 regardless of what is being returned by the server.

Gatsby version v4.4.0

@dangkyokhoang
Copy link
Author

dangkyokhoang commented Dec 28, 2021

Gatsby previously depends on got v8. After upgrading to got 11, the error handler in start-server.js doesn't work as expected because of this sindresorhus/got#1898.

I'm currently using this post-install script as a temporary fix.

const { readFile, writeFile } = require('fs/promises')

// temporary fix for start-server.js
{
  const file = `${ __dirname }/../node_modules/gatsby/dist/utils/start-server.js`
  
  readFile(file, 'utf-8').then(content => writeFile(
    file,
    content
      .replace('if (response) {', 'if (response = err.response) {')
      .replace(
        'res.writeHead(response.statusCode || 400, response.headers);',
        `res.writeHead(response.statusCode || 400, response.headers), res.end(response.rawBody);`,
      ),
  ))
}

@LekoArts
Copy link
Contributor

Hi!

I'm closing this as a stale issue as in the meantime Gatsby 4 and related packages were released. You can check our Framework Version Support Page to see which versions currently receive active support. If this is a feature request, please create a discussion as we moved feature requests from issues to GitHub Discussions.

Please try the mentioned issue on the latest version (using the next tag) and if you still see this problem, open a new bug report. It must include a minimal reproduction.

Thanks!

jsalvata added a commit to jsalvata/gatsby that referenced this issue Sep 11, 2022
Transfer errors responses from the proxied server, including the status code, to the client.

Fixes gatsbyjs#36589, aka gatsbyjs#34244, aka gatsbyjs#33333.
jsalvata added a commit to jsalvata/gatsby that referenced this issue Oct 3, 2022
Transfer errors responses from the proxied server, including the status code, to the client.

Fixes gatsbyjs#36589, aka gatsbyjs#34244, aka gatsbyjs#33333.
jsalvata added a commit to jsalvata/gatsby that referenced this issue Oct 13, 2022
Transfer errors responses from the proxied server, including the status code, to the client.

Fixes gatsbyjs#36589, aka gatsbyjs#34244, aka gatsbyjs#33333.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

4 participants